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

时间:2015-07-02 23:53:16

标签: java android listview listadapter

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

我试图通过使用asynctask类解析一系列帖子来下载,在获得帖子之后,它假设在我的页面中设置posts数组,并执行setAdapter函数以设置我的新帖子数组

问题是,在我的home片段中初始化了listView和listAdapter之后,然后我从parse函数执行postArray,在完成从parse获取posts数组后,它无法更新listAdapter,因为它说listAdapter和我的listView"还没有初始化",即使它们有。

P.S。 抱歉没有以方便的方式发布我的代码,我不会经常发布我的代码问题。

这是我的代码:

我的家庭片段:

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

主要活动:

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

}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试在方法调用之前初始化homeFragment中的onCreate。知道哪些行给你错误也很有帮助。

答案 2 :(得分:0)

显然,当结果到来时,你的片段没有View。

您应该使用FragmentManager将片段正确添加到Activity中,然后在Fragment的onActivityCreated()回调中(在Fragment正确设置其视图后由系统调用),启动AsyncTask。 / p>