使用接口时获取null

时间:2015-02-18 06:15:28

标签: java android interface

在我的项目中,我使用界面获取Arraylist的值。我已经为此编写了代码,但每当我的应用程序运行时,我都会得到NullPointerException。我也初步了它

代码

public class MyResourceCustomAdaper extends BaseAdapter {

    private Context context;
    ArrayList<HashMap<String, String>> data;
    static JSONObject jsonObject;
    private static JSONObject NewDataSet;
    private static ProgressDialog pDialog;
    public static ArrayList<HashMap<String, String>> arrayMatchingJobs;
    GetMatchingJobs matchingJobs;
    GetMatchingJobsArray getMatchingJobsArray;

    public MyResourceCustomAdaper(Context context, ArrayList<HashMap<String, String>> data) {
        this.context = context;
        this.data = data;


    }

    public static void setInterface(GetMatchingJobsArray getMatchingJobsArray) {
        getMatchingJobsArray = getMatchingJobsArray;
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        things done here....


        return view;
    }



    public class GetMatchingJobs extends AsyncTask<String, String, String> {
        String response;
        Context c;


        public GetMatchingJobs(Context c) {
            this.c = c;

        }

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


            calling the web service

        }



        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            arrayMatchingJobs = new ArrayList<HashMap<String, String>>();
pasrsing the json and setting the value of arraylist in the interface

getMatchingJobsArray.getMatchingJobs(true, arrayMatchingJobs);//getting null here



                            }


                        }

                    Intent i = new Intent(c, MyResourceMatchingJobs.class);
                    c.startActivity(i);


                }


            }
        }


    }

    public interface GetMatchingJobsArray {

        public void getMatchingJobs(boolean value, ArrayList<HashMap<String, String>> arrayMatchingJobs);
    }

} 

使用界面的类

public class MyResourceMatchingJobs extends Activity implements MyResourceCustomAdaper.GetMatchingJobsArray {
    private ListView listView;
    private MyResourceMatchingJobsCustomList adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_resource_matching_jobs_list);
        listView = (ListView) findViewById(R.id.lv_my_resource_matching_jobs);
        MyResourceCustomAdaper.setInterface(this);


    }


    @Override
    public void getMatchingJobs(boolean value, ArrayList<HashMap<String, String>> arrayMatchingJobs) {

        if (value) {
            adapter = new MyResourceMatchingJobsCustomList(MyResourceMatchingJobs.this, arrayMatchingJobs);
            listView.setAdapter(adapter);
        }

    }
}

logcat的

5338-5338/com.example.jobs_on_call.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.jobs_on_call_adapter.MyResourceCustomAdaper$GetMatchingJobs.onPostExecute(MyResourceCustomAdaper.java:253)
            at com.example.jobs_on_call_adapter.MyResourceCustomAdaper$GetMatchingJobs.onPostExecute(MyResourceCustomAdaper.java:139)
            at android.os.AsyncTask.finish(AsyncTask.java:602)
            at android.os.AsyncTask.access$600(AsyncTask.java:156)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:4624)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

您的成员变量getMatchingJobsArray可能为null。如果是这种情况,则在调用其getMatchingJobs方法时会出现NullPointerException。

答案 1 :(得分:0)

下面:

public static void setInterface(GetMatchingJobsArray getMatchingJobsArray) {
        getMatchingJobsArray = getMatchingJobsArray;
}

setInterface方法为static,但getMatchingJobsArray不是static

关于静态方法:

静态方法中只能访问静态字段

所以而不是

getMatchingJobsArray = getMatchingJobsArray;

如果使用

MyResourceCustomAdaper.this.getMatchingJobsArray = getMatchingJobsArray;

然后代码编辑器将显示:

无法对非静态字段dfnsdks进行静态引用

  

为什么要获取NullPointerException?

因为班级对象getMatchingJobsArraynull

要解决此问题,请将getMatchingJobsArray声明为static并使用类名访问它:

static GetMatchingJobsArray getMatchingJobsArray;
public static void setInterface(GetMatchingJobsArray getMatchingJobsArray) {
    MyResourceCustomAdaper.getMatchingJobsArray = getMatchingJobsArray;
}

或者不使用静态方法和变量向MyResourceCustomAdaper类构造函数添加一个参数:

 public MyResourceCustomAdaper(Activity mActivity,Context context,
                         ArrayList<HashMap<String, String>> data) {
        this.context = context;
        this.data = data;
        this.getMatchingJobsArray=(GetMatchingJobsArray)mActivity;
    }

并通过将Activity Context作为第一个参数传递来创建MyResourceCustomAdaper类对象:

MyResourceCustomAdaper adapter=new MyResourceCustomAdaper(
                                  MyResourceMatchingJobs.this,.....);