2013-07-13 99 views
0

當這兩個Toast裏面的AsyncTask裏面導致崩潰的時候,當我刪除兩個Toast時,一切正常,沒有崩潰。爲什麼是這樣?以及如何在AsyncTask中使用Toast消息而不導致崩潰?Toast裏面的AsyncTask裏面的片段導致崩潰

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的其餘部分;

添加堆棧跟蹤信息,如果onAttach(Activity)之前運行

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

郵政logcat的痕跡。 –

回答

3

getActivity()將返回null。相反,你應該在你的onPreExecuteonPostExecute方法直接調用getActivity()或得到onAttach對它的引用:

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

它現在可以工作,您的答案是正確的,但您在onAttach下給出的答案無效。並繼續崩潰。但是我在答案中做了你所說的,我在onPreExecute方法中調用了getActivity,並將它放入AsyncTask類成員的活動變量中。現在一切都很完美 – Kevik

+0

沒有工作,因爲當你的toast被創建的時候atttach甚至還沒有被調用,你應該閱讀關於Fragment的生命週期,這將幫助你更多的問題... –

+0

爲了進一步@ ianhanniballake的響應。創建一個全局變量'private Activity mActivity;'然後在AsyncTask內的'onPreExecute'中定義變量'mActivity = getActivity();'(對於片段類型)。 'mActivity = this;'爲一個活動。然後,當你打電話給吐司時,用'mActivity'替換你的getActivity()。你去了。 – Brandon