gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:21:18 28 4
gpt4 key购买 nike

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

我试图使用 asynctask 类从解析中下载帖子数组,在获取帖子后,它应该在我的页面中设置帖子数组,并执行 setAdapter 函数以设置我的新帖子数组.

问题是,在我的 home fragment 中初始化 listView 和 listAdapter 后,然后我执行从解析函数获取的 postArray,在它完成从解析中获取 posts 数组后,它无法更新 listAdapter,因为它说 listAdapter和我的 listView “尚未初始化”,即使它们已经初始化。

附:很抱歉没有以方便的方式发布我的代码,我不经常发布我的代码问题。

这是我的代码:

我的主页 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{//....}

我家的型号:

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

}

最佳答案

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

关于java - 尝试在空对象引用上调用虚拟方法 'android.view.View android.view.View.findViewById(int)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31196436/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com