Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法解释的NullPointerException_Java_Android_Nullpointerexception - Fatal编程技术网

Java 无法解释的NullPointerException

Java 无法解释的NullPointerException,java,android,nullpointerexception,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

调用该方法:

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();
}
替换
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方法以接受该接口,而不知道调用该接口的活动

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

顺便说一句,“所有用户”对于JSON对象来说是个糟糕的名字。您应该使用Gson创建一个
列表

此方法在哪里调用?为什么只有在异步任务完成时,才膨胀
列表视图(执行
findViewById
)?如果在您的
活动中调用了您的方法
allUsers
,请在`setContentView`之后的
onCreate
中膨胀您的视图。简单地用lv1替换lv2不会导致错误。MY_标记1到3应该具有相同的值。是否在代码中进行了更多更改?或仅更换lv2.setAdapter(myAdapter);通过lv1.setAdapter(myAdapter);-您是否在代码方面做了更多更改不是。这里的主要问题是将活动传递到AsyncTask。使用的模式错误
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();
}
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