尝试在空对象引用上调用接口方法

Attempt to invoke interface method on a null object reference

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

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.access0(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 class

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 class 实例来获取对 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);