尝试在空对象引用上调用虚拟方法 'android.view.View android.view.View.findViewById(int)'

Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

我一直在寻找与我类似的问题以找到解决方案,但我真的找不到类似的东西。

我正在尝试使用异步任务 class 从解析 post 的数组中下载,在它获得 post 之后,它假设设置 posts 数组,并执行 setAdapter 函数以设置我的新 posts 数组。

问题是,在我的主片段中初始化了 listView 和 listAdapter 之后,然后我执行了从解析函数中获取的 post 数组,在它完成获取 posts 数组之后从解析中,它无法更新 listAdapter,因为它说 listAdapter 和我的 listView "haven't initialized yet",即使它们有。

p.s。 很抱歉没有 post 以方便的方式编写我的代码,我不经常 post 我的代码问题。

这是我的代码:

我的家片段:

public class HomeFragment extends Fragment {
View root;
ArrayList<PostClass> postsArrayList = new ArrayList<>();

static boolean isPostsArrayUpdated = false;

ListAdapter listAdapter;
PullToRefreshListView listView;

public void updatePostsArrayList(ArrayList<PostClass> postsArrayList){
    if(!isPostsArrayUpdated){
        // First time updating posts array list
        listAdapter = new ListAdapter(getActivity(), root);
        listView = (PullToRefreshListView) root.findViewById(R.id.list_container);

        this.postsArrayList = postsArrayList;
        listView.setAdapter(listAdapter);
        isPostsArrayUpdated = true;
        root.findViewById(R.id.homeFragmentLoadingPanel).setVisibility(View.GONE);
    }else{
        // Have updated posts before
        this.postsArrayList = postsArrayList;
        listAdapter.notifyDataSetChanged();
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    root = inflater.inflate(R.layout.fragment_home, container, false);
    listView = (PullToRefreshListView) root.findViewById(R.id.list_container);
    listAdapter = new ListAdapter(getActivity(), root);

    Home_Model.getInstance().setPostsArrayList();

    return root;
}

public class ListAdapter extends BaseAdapter implements View.OnClickListener{//....}

我家的型号:

public class Home_Model {

Home_Model(){}

static final Home_Model instance = new Home_Model();

public static Home_Model getInstance() {
    return instance;
}

public void setPostsArrayList(){
    new setHomePostsArray().execute();
}

public class setHomePostsArray extends AsyncTask<Void, ArrayList<PostClass>, Void>{

    ArrayList<String> followersList;
    ArrayList<PostClass> postsArrayList;

    @Override
    protected Void doInBackground(Void... params) {

        // Getting posts from parse
        String userName = Parse_model.getInstance().getUserClass().get_userName();
        followersList = Parse_model.getInstance().getFollowersByUserNameToString(userName);
        followersList.add(userName);


        postsArrayList = Parse_model.getInstance().getAllUsersPostsByFollowings(followersList);

        for (PostClass currPost : postsArrayList) {
            for (PostClass currLocalDBPost : LocalDBPostsArray) {
                if (currPost.getObjectID().equals(currLocalDBPost.getObjectID())) {
                    currPost.set_postPicture(currLocalDBPost.get_postPicture());

                }
            }
        }
        //Updating home page
        onProgressUpdate(postsArrayList);


        // Updating local data base in new posts
        //checking in local DB if there are any new posts from parse and update them
        for (PostClass currPost : postsArrayList) {
            boolean isPostExists = false;
            for (PostClass currLocalPost : LocalDBPostsArray) {
                if (currPost.getObjectID().equals(currLocalPost.getObjectID())) {
                    isPostExists = true;
                }
            }
            if (!isPostExists) {
                ModelSql.getInstance().addPost(currPost);
                Log.e("post not exist", "adding local DB");
            }
        }

        //updating followers list in local DB
        Parse_model.getInstance().getUserClass().setFollowersArray(followersList);
        ModelSql.getInstance().updateFollowersArray(currUser);

        return null;
    }

    @Override
    protected void onProgressUpdate(ArrayList<PostClass>... values) {
        //pass the updated postsArrayList to home fragment
        if(setPostsInHomePageDelegate!= null){
            setPostsInHomePageDelegate.setPosts(values[0]);
        }
    }
}


public interface SetPostsInHomePage {
    public void setPosts(ArrayList<PostClass> postsArrayList);
}

SetPostsInHomePage setPostsInHomePageDelegate;

public void setSetPostsInHomePageDelegate(SetPostsInHomePage setPostsInHomePageDelegate) {
    this.setPostsInHomePageDelegate = setPostsInHomePageDelegate;
}

主要activity:

public class MainActivity extends Activity {

static HomeFragment homeFragment = new HomeFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

// the home fragment has already been opened during the app opening 

//...

setPostsImHomePage();
 }

//...

public void setPostsImHomePage(){
    Home_Model.getInstance().setSetPostsInHomePageDelegate(new Home_Model.SetPostsInHomePage() {
        @Override
        public void setPosts(ArrayList<PostClass> postsArrayList) {
            homeFragment.updatePostsArrayList(postsArrayList);
        }
    });
}

}

尝试在调用方法之前在 onCreate 中初始化 homeFragment。了解哪一行给您带来错误也很有帮助。

尝试将您的方法 setPostsImHomePage(...)MainActivity 移动到 HomeFragment 并在 return root; 之前的 OnCreateView 中调用它。

很明显当结果到达时你的fragment没有View。

您应该使用 FragmentManager 将片段正确添加到 Activity,然后在片段的 onActivityCreated() 回调中(在片段正确显示后由系统调用)设置),启动你的 AsyncTask。