简体   繁体   English

尝试在空对象引用上调用虚拟方法'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

I've been looking for a similar problem to mine in order to find a solution, but I seriously couldn't find anything like that. 我一直在寻找类似的问题来寻找解决方案,但是我真的找不到类似的问题。

I was trying to download from parse an array of posts with an asynctask class, and after it gets the posts, it suppose to set the posts array in my page, and perform the setAdapter function in order to set my new posts array. 我试图从解析中下载带有asynctask类的帖子数组,并且在获取帖子之后,它想在我的页面中设置posts数组,并执行setAdapter函数以设置新的posts数组。

the problem is, after I've initialized listView and listAdapter in my home fragment,and then I perform the postArray taking from parse function, after it finishes taking the posts array from parse, it cannot update listAdapter because it says the listAdapter and my listView "haven't initialized yet", even though they have. 问题是,在我的家庭片段中初始化了listView和listAdapter之后,然后我执行了parse from parse函数,在完成了从解析中获取posts数组之后,它无法更新listAdapter,因为它说listAdapter和我的listView即使尚未初始化,仍“尚未初始化”。

ps sorry for not posting my code in a convenient way, I don't tend to post my code problems that often. ps抱歉,我没有以便利的方式发布我的代码,因此我不会经常发布我的代码问题。

here's my code: 这是我的代码:

my home fragment: 我的家庭片段:

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{//....}

my home model: 我的家庭模型:

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

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

} }

Try to move your method setPostsImHomePage(...) from MainActivity to HomeFragment and call it in OnCreateView before return root; 尝试将方法setPostsImHomePage(...)MainActivity移到HomeFragment并在return root;之前在OnCreateView调用它return root; .

Try initializing homeFragment in onCreate before your method call. 在调用方法之前,尝试在onCreate初始化homeFragment It's also helpful to know which line(s) are giving you errors. 知道哪些行会给您带来错误也很有帮助。

Obviously your fragment has no View when the result arrives. 显然,结果到达时您的片段没有View。

You should properly add the fragment to the Activity using the FragmentManager, then in the Fragment's onActivityCreated() callback (which is called by the system after the Fragment has its view properly set), start your AsyncTask. 您应该使用FragmentManager将片段正确地添加到Activity中,然后在Fragment的onActivityCreated()回调(在Fragment正确设置其视图后由系统调用)中启动AsyncTask。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Listview: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference - Android Listview: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference FacebookSdk尝试在空对象引用上调用虚拟方法&#39;android.view.View android.view.View.findViewById(int)&#39; - FacebookSdk Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法&#39;android.view.View android.view.View.findViewById(int)&#39; - java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference - Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference 尝试在空对象引用上调用虚拟方法“android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)” - Attempt to invoke virtual method 'android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)' on a null object reference 如何修复在 null object 参考上调用虚拟方法 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' 的尝试? - How to fix attempt to invoke virtual method 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' on a null object reference? java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.View .MainActivity.findViewById(int)' - java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View .MainActivity.findViewById(int)' on a null object reference 从ListView获取子对象会导致在null对象引用上出现android.view.View android.view.View.findViewById(int) - Getting child from ListView causes android.view.View android.view.View.findViewById(int) on a null object reference java.lang.NullPointerException:尝试在 null object 参考上调用虚拟方法“android.view.View android.app.Activity.findViewById(int)” - java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference 我收到此错误:尝试在 null ZA8CFDE6331BD59EB2AC96F8911C4B66Z 参考上调用虚拟方法 'android.view.View android.widget.Button.findViewById(int)' - I'm getting this error: Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM