2015-10-22 45 views
0

我正在使用拉來重新加載RSS android應用程序。我想使用AsyncTask。這是我的代碼:在asynctask中出現錯誤對話框的句柄異常

class Refresh extends AsyncTask<String,Void,Void>{ 

    @Override 
    protected Void doInBackground(String... params) { 
     try { 
      Download(getResources().getString(R.string.link)); 
     }catch (Exception e){ 
      createNetErrorDialog(); 

     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     try{ 
      views(); 
     }catch (Exception e){ 
      Toast.makeText(MainActivity.this, 
        "Error", 
        Toast.LENGTH_LONG).show(); 
      cancel(true); 
     } 

     lstPost.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, 
            long id) { 
       Intent intent = new Intent(MainActivity.this, WebviewActivity.class); 
       Bundle bundle = new Bundle(); 

       Document xmlFeed = rssfeed 
         .getRSSFromServer(getResources().getString(R.string.link)); 
       NodeList nodes = xmlFeed.getElementsByTagName(getResources().getString(R.string.item)); 
       Element item = (Element) nodes.item(position); 
       String summary = rssfeed.getValue(item, key_summary); 
       String title = rssfeed.getValue(item, key_title); 
       bundle.putString(getResources().getString(R.string.tag_summary), summary); 
       bundle.putString(getResources().getString(R.string.tag_title), title); 
       intent.putExtras(bundle); 
       startActivity(intent); 
      } 
     }); 
    } 
} 




void Download(String url) throws Exception{ 
    lists.clear(); 

     Document xmlFeed = rssfeed.getRSSFromServer(url); 
     NodeList nodes = xmlFeed.getElementsByTagName("entry"); 
     for (int i = 0; i < nodes.getLength(); i++) { 
      Element item = (Element) nodes.item(i); 
      HashMap<String, Object> feed = new HashMap<String, Object>(); 
      feed.put(key_title, rssfeed.getValue(item, key_title)); 
      feed.put(key_summary, rssfeed.getValue(item, key_summary)); 
      feed.put(key_link, rssfeed.getValue(item, key_link)); 
      //feed.put(key_date, rssfeed.getValue(item, key_date)); 
      post_lists.add(feed); 
      lists.add(feed.get(key_title).toString()); 
     } 

} 


void views(){ 
    lstPost = (ListView) findViewById(R.id.lstPosts); 

    adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_2, android.R.id.text1, lists) { 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 
      TextView txt1 = (TextView) view 
        .findViewById(android.R.id.text1); 
      TextView txt2 = (TextView) view 
        .findViewById(android.R.id.text2); 
      HashMap<String, Object> data = post_lists.get(position); 
      txt1.setText(Html.fromHtml(data.get(key_title).toString())); 

      if (data.get(key_summary).toString().length() > 125) { 

       txt2.setText(Html.fromHtml((data.get(key_summary).toString()).substring(0, 125) + "...")); 
      } else { 

       txt2.setText(Html.fromHtml(data.get(key_summary).toString())); 
      } 
      return view; 
     } 

    }; 

    TextView dateview=(TextView) findViewById(R.id.date); 
    Calendar rightNow = Calendar.getInstance(); 
    DateFormat formatter = new SimpleDateFormat(getResources().getString(R.string.date_format)); 

    dateview.setText(getResources().getString(R.string.lastupdate) + formatter.format(rightNow.getTime())); 
    lstPost.setAdapter(adapter); 
} 


protected void createNetErrorDialog() { 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage(getResources().getString(R.string.errorbox)) 
      .setTitle("Unable to connect") 
      .setCancelable(false) 
      .setPositiveButton("Settings", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 
          startActivity(i); 
         } 
        } 
      ) 
      .setNeutralButton("Refresh", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          new Refresh().execute(getResources().getString(R.string.link)); 
         } 
        }) 
      .setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          finish(); 
         } 
        } 
      ); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
} 

問題:如果我切換網絡連接關閉時,應用程序退出,而不是顯示createNetErrorDialog()。有人能幫我出來嗎?

+0

你知道是否實際調用了'createNetErrorDialog()'嗎?無論如何,我不認爲你可以在'doInBackground()'中顯示對話框。你也可以檢查你的logcat。很有可能你會在那裏打印一個異常。 –

回答

0

使用下面的方法來檢查網絡是否開啓或關閉:

public static boolean isDeviceOnline(Context context) { 

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
    boolean isOnline = (networkInfo != null && networkInfo.isConnected()); 
    if(!isOnline) 
     Toast.makeText(context, " No internet Connection ", Toast.LENGTH_SHORT).show(); 

    return isOnline; 
} 

而當你需要TTO檢查設備在線稱之爲:

if(isDeviceOnline(context)) 
{ 
//call Refresh 
} 
else 
{ 
//whatever you want 
} 

我的事情異常處理這不是一個很好的做法。

+0

謝謝。我現在就試試這個。 – Stacks13

相關問題