gpt4 book ai didi

android - 应用程序退出后崩溃?

转载 作者:行者123 更新时间:2023-12-02 00:54:10 25 4
gpt4 key购买 nike

我正在制作一个应用程序,其中有多个 Horizo​​ntalScrollview ,总体来说应用程序运行良好,但是当用户退出 Boom 应用程序崩溃时,logcast 告诉我它在 onPostExecute 中有多个标记:

rootView = inflater.inflate(R.layout.three_fragment, container, false);
L1= (LinearLayout) rootView.findViewById(R.id.LinearLayout1);
L3= (TextView) rootView.findViewById(R.id.CheckTest);
prog =(ProgressBar)rootView.findViewById(R.id.progressBar);



booksList = new ArrayList<Books>();



new JSONAsyncTask().execute("http://192.168.30.3/actors/book_tab2.php");

centerLockHorizontalScrollview = (CenterLockHorizontalScrollview) rootView.findViewById(R.id.HMscrollView);

adapter = new CardHomeAdapter(getActivity(), R.layout.row_home_card, booksList);


centerLockHorizontalScrollview.setAdapter(TabThreeFragment.this, adapter);

rootView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT ));


}
}

执行后:

protected void onPostExecute(Boolean result) {

adapter.notifyDataSetChanged();

if(result == false)

Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
if(booksList != null) {


adapter = new CardHomeAdapter(getActivity().getApplicationContext(), R.layout.row_home_card, booksList);
centerLockHorizontalScrollview.setAdapter(TabThreeFragment.this, adapter);
}

错误:

03-07 11:49:21.478: E/AndroidRuntime(9703): FATAL EXCEPTION: main
03-07 11:49:21.478: E/AndroidRuntime(9703): java.lang.NullPointerException
03-07 11:49:21.478: E/AndroidRuntime(9703): at br.liveo.fragments.TabThreeFragment$JSONAsyncTask.onPostExecute(TabThreeFragment.java:344)
03-07 11:49:21.478: E/AndroidRuntime(9703): at br.liveo.fragments.TabThreeFragment$JSONAsyncTask.onPostExecute(TabThreeFragment.java:1)
03-07 11:49:21.478: E/AndroidRuntime(9703): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-07 11:49:21.478: E/AndroidRuntime(9703): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-07 11:49:21.478: E/AndroidRuntime(9703): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-07 11:49:21.478: E/AndroidRuntime(9703): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 11:49:21.478: E/AndroidRuntime(9703): at android.os.Looper.loop(Looper.java:137)
03-07 11:49:21.478: E/AndroidRuntime(9703): at android.app.ActivityThread.main(ActivityThread.java:4961)
03-07 11:49:21.478: E/AndroidRuntime(9703): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 11:49:21.478: E/AndroidRuntime(9703): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 11:49:21.478: E/AndroidRuntime(9703): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
03-07 11:49:21.478: E/AndroidRuntime(9703): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
03-07 11:49:21.478: E/AndroidRuntime(9703): at dalvik.system.NativeStart.main(Native Method)

最佳答案

当您退出应用程序时,仍然需要执行后执行,它会尝试调用 Activity 来显示 toast 或创建新的适配器。但是由于您的应用程序已被终止,因此该 Activity 不再存在,因此您会收到空指针异常。在显示 toast 或创建新适配器之前检查 if(getActivity() != null),或者在 Activity 的 ondestroy 函数中终止任务。

protected void onPostExecute(Boolean result) {
if(getActivity() != null)
{
adapter.notifyDataSetChanged();

if(result == false)

Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
if(booksList != null) {


adapter = new CardHomeAdapter(getActivity().getApplicationContext(), R.layout.row_home_card, booksList);
centerLockHorizontalScrollview.setAdapter(TabThreeFragment.this, adapter);
}
}

关于android - 应用程序退出后崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29077036/

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