0

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

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

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

其余的代码

 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

 }

编辑;

添加了堆栈跟踪信息

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)
4

1 回答 1

3

getActivity()null如果之前运行将返回onAttach(Activity)。相反,您应该getActivity()直接在您的onPreExecuteandonPostExecute方法中调用或在以下位置获取对它的引用onAttach

public void onAttach (Activity attachedActivity) {
    activity = attachedActivity;
}
于 2013-07-13T00:43:04.100 回答