gpt4 book ai didi

android - 带有 fragment 的异步任务,关闭 VM 时出错

转载 作者:行者123 更新时间:2023-11-30 03:18:25 26 4
gpt4 key购买 nike

我正在尝试使用 AsyncTask 咨询解析对象并在列表中为我提供图片和文本,但是在上面运行时显示以下错误:

    10-27 16:29:18.335: D/AndroidRuntime(28453): Shutting down VM
10-27 16:29:18.335: W/dalvikvm(28453): threadid=1: thread exiting with uncaught exception (group=0x40018578)
10-27 16:29:18.343: E/AndroidRuntime(28453): FATAL EXCEPTION: main
10-27 16:29:18.343: E/AndroidRuntime(28453): java.lang.NullPointerException
10-27 16:29:18.343: E/AndroidRuntime(28453): at com.johanseetlabs.panamaviajero.A_su_alrededor$RemoteDataTask.onPostExecute(A_su_alrededor.java:101)
10-27 16:29:18.343: E/AndroidRuntime(28453): at com.johanseetlabs.panamaviajero.A_su_alrededor$RemoteDataTask.onPostExecute(A_su_alrededor.java:1)
10-27 16:29:18.343: E/AndroidRuntime(28453): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-27 16:29:18.343: E/AndroidRuntime(28453): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-27 16:29:18.343: E/AndroidRuntime(28453): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-27 16:29:18.343: E/AndroidRuntime(28453): at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 16:29:18.343: E/AndroidRuntime(28453): at android.os.Looper.loop(Looper.java:130)
10-27 16:29:18.343: E/AndroidRuntime(28453): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-27 16:29:18.343: E/AndroidRuntime(28453): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 16:29:18.343: E/AndroidRuntime(28453): at java.lang.reflect.Method.invoke(Method.java:507)
10-27 16:29:18.343: E/AndroidRuntime(28453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-27 16:29:18.343: E/AndroidRuntime(28453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-27 16:29:18.343: E/AndroidRuntime(28453): at dalvik.system.NativeStart.main(Native Method)

我的 fragment :

public class A_su_alrededor extends Fragment {
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ListViewAdapter adapter;
public List<ObtenerData> ObtenerDatalist = null;
View view;
private WeakReference<RemoteDataTask> asyncTaskWeakRef;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_su_alrededor, container, false);
startNewAsyncTask();
return view;
}

// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
private WeakReference<A_su_alrededor> fragmentWeakRef;

private RemoteDataTask(
A_su_alrededor a_su_alrededor) {
this.fragmentWeakRef = new WeakReference<A_su_alrededor>(
a_su_alrededor);
}

@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(true);
// Show progressdialog
mProgressDialog.show();
// lockScreenOrientation();
}

@Override
protected Void doInBackground(Void... params) {
// Create the array
ObtenerDatalist = new ArrayList<ObtenerData>();
try {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Hotel");
query.orderByAscending("hotel");
ob = query.find();
for (ParseObject hotel : ob) {
// localizar imagenes en la columna
ParseFile image = (ParseFile) hotel.get("image");

ObtenerData map = new ObtenerData();
map.setHotel((String) hotel.get("hotel"));
map.setImage(image.getUrl());
ObtenerDatalist.add(map);
}
} catch (ParseException e) {
Log.d("TAG", "AsynTask no se ejecuto");
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) view.findViewById(R.id.listview_a_su_alrededor);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(getActivity(), ObtenerDatalist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// unlockScreenOrientation();
}
}

private void startNewAsyncTask() {
RemoteDataTask asyncTask = new RemoteDataTask(this);
this.asyncTaskWeakRef = new WeakReference<RemoteDataTask>(asyncTask);
asyncTask.execute();
}
}

标签上使用了相同的代码,但我决定删除该标签。该 fragment 放置在抽屉布局的 FrameLayout 上。

最后请原谅我的英语。

最佳答案

onCreateView 方法中有这一行:

View view = inflater.inflate(R.layout.a_su_alrededor, container, false);

我认为你应该删除类型名称:

view = inflater.inflate(R.layout.a_su_alrededor, container, false);

乍一看,您似乎在失败的 onPostExecute 方法中使用了对名为 view 的变量的类范围引用。

关于android - 带有 fragment 的异步任务,关闭 VM 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624028/

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