gpt4 book ai didi

java - 尝试在空对象引用上调用接口(interface)方法

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:02 25 4
gpt4 key购买 nike

我有类LoadJSONTask。在这个类中,我收到以下错误。我的应用程序正在运行,但是当我尝试在代码中添加进度条时,它不起作用。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.kalakaaristudios.json.listviewmenu, PID: 3809
java.lang.NullPointerException: Attempt to invoke interface method 'void in.kalakaaristudios.json.listviewmenu.LoadJSONTask$Listener.onLoaded(java.util.List)' on a null object reference
at in.kalakaaristudios.json.listviewmenu.LoadJSONTask$override.onPostExecute(LoadJSONTask.java:83)
at in.kalakaaristudios.json.listviewmenu.LoadJSONTask$override.access$dispatch(LoadJSONTask.java)
at in.kalakaaristudios.json.listviewmenu.LoadJSONTask.onPostExecute(LoadJSONTask.java:0)
at in.kalakaaristudios.json.listviewmenu.LoadJSONTask.onPostExecute(LoadJSONTask.java:21)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

LoadJSONTask 类

package in.kalakaaristudios.json.listviewmenu;

import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class LoadJSONTask extends AsyncTask<String, Void, Response> {

private MainActivity activity;
private ProgressBar dwBar;

public LoadJSONTask(MainActivity activity) {
this.activity = activity;
dwBar = (ProgressBar) activity.findViewById(R.id.progress_bar);
}

public LoadJSONTask(Listener listener) {
mListener = listener;
}

public interface Listener {
void onLoaded(List<AndroidVersion> androidList);
void onError();
}

private Listener mListener;

@Override
protected Response doInBackground(String... strings) {
try {

String stringResponse = loadJSON(strings[0]);
Gson gson = new Gson();

return gson.fromJson(stringResponse, Response.class);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JsonSyntaxException e) {
e.printStackTrace();
return null;
}
}


@Override
protected void onPreExecute() {
super.onPreExecute();
MainActivity mn = new MainActivity();
dwBar.setVisibility(View.VISIBLE);
}


@Override
protected void onPostExecute(Response response) {

dwBar.setVisibility(View.GONE);

if (response != null) {

mListener.onLoaded(response.getAndroid());//getting error here

} else {

mListener.onError();
}



}

private String loadJSON(String jsonURL) throws IOException {

URL url = new URL(jsonURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder response = new StringBuilder();

while ((line = in.readLine()) != null) {

response.append(line);
}

in.close();
return response.toString();
}
}

MainActivity.java

package in.kalakaaristudios.json.listviewmenu;

import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class LoadJSONTask extends AsyncTask<String, Void, Response> {

private MainActivity activity;
private ProgressBar dwBar;

public LoadJSONTask(MainActivity activity) {
this.activity = activity;
dwBar = (ProgressBar) activity.findViewById(R.id.progress_bar);
}


public LoadJSONTask(Listener listener) {
mListener = listener;
}

public interface Listener {
void onLoaded(List<AndroidVersion> androidList);
void onError();
}

private Listener mListener;

@Override
protected Response doInBackground(String... strings) {
try {

String stringResponse = loadJSON(strings[0]);
Gson gson = new Gson();

return gson.fromJson(stringResponse, Response.class);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JsonSyntaxException e) {
e.printStackTrace();
return null;
}
}


@Override
protected void onPreExecute() {
super.onPreExecute();
MainActivity mn = new MainActivity();
dwBar.setVisibility(View.VISIBLE);
}


@Override
protected void onPostExecute(Response response) {

dwBar.setVisibility(View.GONE);

if (response != null) {

mListener.onLoaded(response.getAndroid());

} else {

mListener.onError();
}
}

private String loadJSON(String jsonURL) throws IOException {

URL url = new URL(jsonURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder response = new StringBuilder();

while ((line = in.readLine()) != null) {

response.append(line);
}

in.close();
return response.toString();
}
}

最佳答案

我认为你的问题在这一行

dwBar = (ProgressBar) activity.findViewById(R.id.progress_bar);

因为您正在使用 Activity 类实例来获取对 ProgressBar View 小部件的引用

您应该膨胀包含 ProgressBar 小部件的布局,并使用查找来获取 ProgressBar 的实例

View view = inflater.inflate(R.Layout_that_contains_progress_bar, null);
dwBar = (ProgressBar) activity.findViewById(R.id.progress_bar);

或者,您可以像下面这样以编程方式创建 ProgressBar

dwBar = new ProgressBar(activity);

关于java - 尝试在空对象引用上调用接口(interface)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40049680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com