gpt4 book ai didi

java - ParseQuery NullPointer 异常悖论

转载 作者:行者123 更新时间:2023-11-30 01:58:21 25 4
gpt4 key购买 nike

这是我之前问题的后续问题:Parse RecyclerView Image Query

我现在有一个矛盾的空指针异常。我正在尝试按照下面概述的方式从内部方法中提取列表。

public List<ParseObject> objectList;

public List<String> getData(String filter){

ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
query.findInBackground(new FindCallback<ParseObject>() {

public void done(List<ParseObject> downloads, com.parse.ParseException e) {
if (e == null) {
objectList=downloads;
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});

int size = objectList.size();

for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}

return urlList;

}

我该如何解决这个问题?有哪些方法比我现在使用的更好?

编辑:

这是它正在做的事情。目前没有异常,但是recyclerview没有显示任何东西。

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_events, container, false);
setList("sport", rootView);
return rootView;
}

public void set() {

}

public void setList(String filter, View view){
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(getData(filter));
recyclerView.setAdapter(myAdapter);
}

编辑 2:更多更新代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_events, container, false);
setList("sport", rootView);
return rootView;
}

public void set() {

}

public void setList(String filter, View view){
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(getData(filter));
recyclerView.setAdapter(myAdapter);
}


private List<String> urlList = new ArrayList<>();



public List<ParseObject> objectList = new ArrayList<>();
public int size;

public List<String> getData(String filter){


ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
query.findInBackground(new FindCallback<ParseObject>() {

public void done(List<ParseObject> downloads, com.parse.ParseException e) {
if (e == null) {
Toast.makeText(getActivity(), "Downloads retrieved", Toast.LENGTH_SHORT).show();
objectList.addAll(downloads);
} else {
Log.d("score", "Error: " + e.getMessage());
}

size = objectList.size();
}
});



for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}

return urlList;

}

编辑 3:更新,我添加了我的适配器代码。

    View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_events, container, false);
new GetUrlListTask(this).execute("sport");
return rootView;
}

public void set() {

}



private List<String> urlList = new ArrayList<>();



public List<ParseObject> objectList = new ArrayList<>();
public int size;

public List<String> getData(String filter){


ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
query.findInBackground(new FindCallback<ParseObject>() {

public void done(List<ParseObject> downloads, com.parse.ParseException e) {
if (e == null) {
Toast.makeText(getActivity(), "Downloads retrieved", Toast.LENGTH_SHORT).show();
objectList.addAll(downloads);
} else {

}

size = objectList.size();
}
});



for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}

return urlList;

}

private class GetUrlListTask extends AsyncTask<String,Void,List<String>>
{
private WeakReference<Fragment> weakRef;

public GetUrlListTask(Fragment frag){
weakRef = new WeakReference< Fragment >(frag);
}

@Override
protected List<String> doInBackground(String... filters) {
return getData(filters[0]);
}

@Override
protected void onPostExecute(List<String> urlList) {
Fragment frag = weakRef.get();
if (frag != null){
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(urlList);
recyclerView.setAdapter(myAdapter);
}
}
}

还有我的适配器:

   List<String> urls;
ViewGroup parent;

public MyAdapter(List<String> urls){
this.urls=urls;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_entry, parent);
MyViewHolder myViewHolder = new MyViewHolder(view);
this.parent=parent;
return myViewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Picasso.with(parent.getContext())
.load(urls.get(position))
.into(holder.photo);
Toast.makeText(parent.getContext(), urls.get(position), Toast.LENGTH_LONG).show();
}

@Override
public int getItemCount() {
return urls.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder{

ImageView photo;

public MyViewHolder(View itemView) {
super(itemView);
photo = (ImageView) itemView.findViewById(R.id.listImage);

}
}

最佳答案

所以我假设您想要做的是一旦您获得 urlList,您想要更新一些 View 。如果是这样的话,那么我想你想做的是在后台任务中运行整个方法,而不仅仅是像这样的查询:

首先更改您的 getData() 方法:

public List<String> getData(String filter){

ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);

objectList = query.find(); //I think you have to surround this by a try catch or have the method throw an exception

int size = objectList.size();
for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}

return urlList;
}

请注意,由于您正在进行网络调用,因此上述方法现在必须从后台线程运行。

然后您可以使用类似 AsyncTask 的东西来更新 View :

private class GetUrlListTask extends AsyncTask<String,Void,List<String>>
{
private WeakReference<Fragment> weakRef;

public PhotoTagsFetchTask(Fragment frag){
weakRef = new WeakReference< Fragment >(frag);
}

@Override
protected Void doInBackground(String... filters) {
return getData(filters[0]);
}

@Override
protected void onPostExecute(List<String> urlList) {
Fragment frag = weakRef.get();
if (frag != null){
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(urlList);
recyclerView.setAdapter(myAdapter);
}
}
}

注意上面使用弱引用是为了避免上下文泄漏(基本上避免了不必要的内存泄漏)。

你也可以这样做:

public void getData(String filter){

ParseQuery<ParseObject> query = ParseQuery.getQuery("Images");
query.whereEqualTo("category", filter);
query.findInBackground(new FindCallback<ParseObject>() {

public void done(List<ParseObject> downloads, com.parse.ParseException e) {
if (e == null) {
objectList=downloads;
int size = objectList.size();

for (int i = 0; i<size; i++){
ParseFile file = (ParseFile) objectList.get(i).get("Image");
urlList.add(file.getUrl());
}

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter = new MyAdapter(urlList);
recyclerView.setAdapter(myAdapter);
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});



}

关于java - ParseQuery NullPointer 异常悖论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31840590/

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