2015-01-12 246 views
-4

我试图从下面的代码一切都关闭工作正常应用程序崩溃时的AsyncTask没有互联网连接

有来自地方上的按钮,点击此活动应主要活动开放 它会做解析部分,然后进入下一个清单activity.in情况下,有一个背衬或本次活动被关闭的AsyncTask应停止使用

loader.cancel(true); 

这将禾RK完美的,如果网络不可用,并进行测试时,不要有互联网连接

警告框节目和它关系到的第一个活动,然后崩溃

我想警告框来显示和应用问题的发生应该不会崩溃,回到第一 - > mainactivity

我已审阅本 http://techiedreams.com/android-simple-rss-reader/

How to end AsyncTask onBackPress()

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 

import com.parser.DOMParser; 
import com.parser.RSSFeed; 

public class SplashActivity extends Activity { 



    //private String RSSFEEDURL = "http://www.mobilenations.com/rss/mb.xml"; 
    RSSFeed feed; 
    private AsyncLoadXMLFeed loader; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.splash); 

     Intent i = getIntent(); 
     int position = i.getExtras().getInt("position"); 
     String[] country = i.getStringArrayExtra("country"); 

     //Toast.makeText(getApplicationContext(), country[position], Toast.LENGTH_SHORT).show(); 
     //Toast.makeText(getApplicationContext(), country[position], Toast.LENGTH_SHORT).show(); 
     String name = i.getStringExtra("name"); 
     //Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show(); 

     ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (conMgr.getActiveNetworkInfo() == null) { 


      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage(
        "Unable to reach server, \nPlease check your connectivity.") 
        .setTitle("TD RSS Reader") 
        .setCancelable(false) 
        .setPositiveButton("Exit", 
          new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, 
           int id) { 
          //loader.cancel(true); 
          finish(); 
         } 
        }); 


      AlertDialog alert = builder.create(); 

      alert.show(); 

     } else { 
      // Connected - Start parsing 

      loader = new AsyncLoadXMLFeed(); 
      loader.execute(); 
      //new AsyncLoadXMLFeed().execute(); 

     } 

    } 


    private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void>{ 

     @Override 
     protected Void doInBackground(Void... params) { 

      // Obtain feed 
      DOMParser myParser = new DOMParser(); 
      Intent i = getIntent(); 
      int position = i.getExtras().getInt("position"); 
      String[] country = i.getStringArrayExtra("country"); 
      String name = i.getStringExtra("name"); 
      //feed = myParser.parseXml(RSSFEEDURL); 
      feed = myParser.parseXml("http://"+name+".blogspot.com//feeds/posts/default/-/" + country[position] + "?alt=rss"); 

      return null; 

     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 

      Bundle bundle = new Bundle(); 
      bundle.putSerializable("feed", feed); 

      // launch List activity 
      Intent intent = new Intent(SplashActivity.this, ListActivity.class); 
      intent.putExtras(bundle); 
      startActivity(intent); 

      // kill this activity 
      finish(); 
     } 

    } 

    @Override 
    public void onBackPressed() { 
     finish(); 
    } 

    @Override 
     public void onDestroy(){ 
      super.onDestroy(); 

      // Cancel the task 
      loader.cancel(true); 
     } 
+1

后崩溃日志与问题 –

+0

有你设置权限许可权? – Opiatefuchs

+0

是的,每件事情都可以正常使用网络连接 – 1234567

回答

0

我的猜测是崩溃与NullPointerException。

这是因为如果你没有网络连接,你不会创建AsyncLoadXMLFeed实例。 所以当onDestroy()发生时,你打电话loader.cancel(true);它会抛出这个异常。

0

终于做了这个
感谢阿纳托尔需要检查装载机!= NULL

@Override 
     public void onDestroy(){ 
      super.onDestroy(); 

      // Cancel the task 
      if(loader != null && loader.getStatus() != AsyncTask.Status.FINISHED) { 
       loader.cancel(true); 
      } 

      //loader.cancel(true); 
     } 
相关问题