原因不明的NullPointerException

时间:2018-04-28 14:54:14

标签: java android 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中的东西......

1 个答案:

答案 0 :(得分:1)

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

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();
}

将所有视图用法移回活动并调用您的API类,传入回调以填充该活动

列表

在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 

顺便说一下,&#34;所有用户&#34;是一个JSON对象的糟糕名称。您应该使用Gson来创建List<User>而不是