首页 > 解决方案 > 无法解释的 NullPointerException

问题描述

调用方法:

Log.i("MY_TAG 0 =", String.valueOf(findViewById(R.id.listView)));
allUsers(host, json, login, this);

我的方法:

private static void allUsers(String host, String json, String login, Activity activity) {
    ListView lv1 = activity.findViewById(R.id.listView);

    Log.i("MY_TAG 1 =", String.valueOf(activity));
    Log.i("MY_TAG 2 =", String.valueOf(lv1));
    Log.i("MY_TAG 3 =", String.valueOf(activity.findViewById(R.id.listView)));

    new AsyncTask<Void, String, String>() {
        @Override
        protected String doInBackground(Void... params) {
            return Http.sendMsg(host, json);
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            AllUsersJson allUsersJson = new Gson().fromJson(s, AllUsersJson.class);
            GetAllUsersAdapter myAdapter = new GetAllUsersAdapter(allUsersJson, activity.getApplicationContext(), login);
            ListView lv2 = activity.findViewById(R.id.listView);
            Log.i("MY_TAG 4 =", String.valueOf(lv1));
            Log.i("MY_TAG 5 =", String.valueOf(lv2));
            lv2.setAdapter(myAdapter);
        }
    }.execute();
}

日志:

MY_TAG 0 =: android.widget.ListView{1255189 VFED.VC.. .F....ID 0,0-1080,1584 #7f080071 app:id/listView}
MY_TAG 1 =: myzabbix.sadrutdin.zaynukov.com.testzabbix.NavDrawerActivity@231f1a
MY_TAG 2 =: android.widget.ListView{1255189 VFED.VC.. .F....ID 0,0-1080,1584 #7f080071 app:id/listView}
MY_TAG 3 =: android.widget.ListView{1255189 VFED.VC.. .F....ID 0,0-1080,1584 #7f080071 app:id/listView}
MY_TAG 4 =: android.widget.ListView{1255189 VFED.VC.. ......ID 0,0-1080,1584 #7f080071 app:id/listView}
MY_TAG 5 =: null

替换lv2.setAdapter(myAdapter);lv1.setAdapter(myAdapter);

结果:

MY_TAG 0 =: null
MY_TAG 1 =: myzabbix.sadrutdin.zaynukov.com.testzabbix.NavDrawerActivity@ce89be3
MY_TAG 2 =: null
MY_TAG 3 =: null
MY_TAG 4 =: null
MY_TAG 5 =: null

最奇怪的是,相同的方法可以毫无问题地工作。我尝试了所有选项,但显然我不了解Android中的某些内容......

标签: javaandroidnullpointerexception

解决方案


从该方法中删除所有活动引用。
使用回调重写

public interface OnUsersJson() {
    void onUsers(AllUsersJson users);
}

重构 AsyncTask 方法以接受该接口,对调用它的 Activity 一无所知

public static void allUsers(String host, String json, String login, final OnUsersJson listener) {
    new AsyncTask<Void, String, String>() {
        @Override
        protected String doInBackground(Void... params) {
            return Http.sendMsg(host, json);
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            AllUsersJson allUsersJson = new Gson().fromJson(s, AllUsersJson.class);
            if (listener!=null) listener.onUsers(allUsersJson);
        }
    }.execute();
}

将所有视图使用移回 Activity 并调用您的 API 类,传入 Callback 以填充该Activity的列表

在 onCreate

this.lv1 = findViewById(R.id.listView);
 // Passing an Arraylist to this constructor should be optional 
this.myAdapter = new GetAllUsersAdapter(MainActivity.this, login);
lv1.setAdapter(myAdapter);
API.allUsers(host, json, login, new OnUsersJson() {
    @Override public void onUsers(AllUsersJson users) {
         myAdapter.add(); // Add users here 
         // Do not assign users to a field of this Activity 
     } 
});
// if you did assign a field of users, or added to an arraylist, it'll be null/empty here 

顺便说一句,“所有用户”对于 JSON 对象来说是一个糟糕的名称。List<User>您应该使用Gson 创建一个


推荐阅读