java - 无法解释的 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中的某些内容......
解决方案
从该方法中删除所有活动引用。
使用回调重写
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 创建一个
推荐阅读
- c++ - 可以使用 if-with-initializer (C++17) 格式化格式吗?
- ios - 在 Xcode 中测试“API_BASE_URL”的最佳方法?
- r - 使用逗号小数分隔符创建函数 my.render.cat 而不是 r 中的点
- c# - C#如何获取网页的图标
- javascript - 从数组中的对象动态创建逻辑 OR (||) 运算符
- c# - 后台工作人员完成WinForm后如何启用所有控件
- reactjs - 我想在用户 isAuthenticated 时将状态设置为 true ,当初始状态为 false 时,用户将看到登录注册链接
- javascript - 更改位置或隐藏 div
- reactjs - 如何从快速后端服务器返回用户 ID
- docker - 清单列表条目中没有与 linux/386 匹配的清单