我正在创建一个多个HorizontalScrollview的应用程序,整体上应用程序运行良好但是当用户退出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 ));
}
}
onPostExecute:
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)
答案 0 :(得分:0)
当您退出应用程序并且仍然必须执行后执行时,它会尝试调用活动以显示Toast或创建新适配器。但是因为你的应用程序已经被杀死,所以不再存在激活因此你得到一个nullpointer异常。在显示吐司或创建新适配器之前检查if(getActivity() != null)
,或者在活动的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);
}
}