在片段内部的AsyncTask中吐司会导致崩溃

时间:2013-07-13 00:33:30

标签: android android-asynctask fragment

当这两个Toasts位于片段内部的AsyncTask内部时会导致崩溃,当我删除两个Toast时,一切正常,没有崩溃。为什么是这样?如何在AsyncTask中使用Toast消息而不会导致崩溃?

 Toast.makeText(activity, "onPostExecute", Toast.LENGTH_LONG).show();
 Toast.makeText(activity,"onPreExecute", Toast.LENGTH_LONG).show();

Logcat显示toast消息的nullPointerException。当我删除两个吐司中的第一个它将运行然后崩溃,当第一个吐司在那里它将在开始时崩溃,当两个吐司被移除时它永远不会崩溃。

代码的其余部分

 public class FragmentStaticFragment extends Fragment {

private Button buttonStartProgress;
private ProgressBar progressBar;
Activity activity = getActivity();

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

    buttonStartProgress = (Button) view.findViewById(R.id.button1);
     progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);


    buttonStartProgress.setOnClickListener(new Button.OnClickListener(){

           @Override
           public void onClick(View v) {
            // TODO Auto-generated method stub
            new BackgroundAsyncTask().execute();
            buttonStartProgress.setClickable(false);
           }

        });


    return view;
}

 public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void>{

    int myProgress;

     @Override
      protected void onPostExecute(Void result) {
       // TODO Auto-generated method stub
               // commented out this toast to avoid crashing
      // Toast.makeText(activity, "onPostExecute", Toast.LENGTH_LONG).show();
       buttonStartProgress.setClickable(true);
      }

      @Override
      protected void onPreExecute() {
       // TODO Auto-generated method stub
              // commented out this toast to avoid crashing
     //  Toast.makeText(activity,"onPreExecute", Toast.LENGTH_LONG).show();
       myProgress = 0;
      }

      @Override
      protected Void doInBackground(Void... params) {
       // TODO Auto-generated method stub
       while(myProgress<100){
        myProgress++;
        publishProgress(myProgress);
           SystemClock.sleep(100);
       }
       return null;
      }

      @Override
      protected void onProgressUpdate(Integer... values) {
       // TODO Auto-generated method stub
       progressBar.setProgress(values[0]);
      }


} // end BackgroundAsyncTask

 }

EDIT;

添加了堆栈跟踪信息

07-13 09:23:01.383: E/AndroidRuntime(19489): FATAL EXCEPTION: main
07-13 09:23:01.383: E/AndroidRuntime(19489): java.lang.NullPointerException
07-13 09:23:01.383: E/AndroidRuntime(19489):    at android.widget.Toast.<init>(Toast.java:92)
07-13 09:23:01.383: E/AndroidRuntime(19489):    at Android.widget.Toast.makeText(Toast.java:233)
07-13 09:23:01.383: E/AndroidRuntime(19489):    at com.example.asynctaskprogressbarexample.FragmentStaticFragment$BackgroundAsyncTask.onPreExecute(FragmentStaticFragment.java:62)
07-13 09:23:01.383: E/AndroidRuntime(19489):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
07-13 09:23:01.383: E/AndroidRuntime(19489):    at android.os.AsyncTask.execute(AsyncTask.java:511)
07-13 09:23:01.383: E/AndroidRuntime(19489):    at com.example.asynctaskprogressbarexample.FragmentStaticFragment$1.onClick(FragmentStaticFragment.java:36)

1 个答案:

答案 0 :(得分:3)

如果在getActivity()之前运行,

null将返回onAttach(Activity)。相反,您应该直接在getActivity()onPreExecute方法中调用onPostExecute,或在onAttach中获取对它的引用:

public void onAttach (Activity attachedActivity) {
    activity = attachedActivity;
}