2013-03-12 72 views
0

每當用戶按下返回按鈕使用AsyncTask從Web獲取xml數據時,我的應用程序會強制關閉狀態。我該如何解決這個問題。提前致謝。當從互聯網獲取數據時按壓返回時強制關閉

public class InboxActivity extends Activity { 
public static ExpandableListView mailList; 
List<HashMap<String,String>> list; 
MailList asyncTaskMailList; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.inbox_layout); 
    ConstantValues.footerCurrentActivity=InboxActivity.this; 
    mailList=(ExpandableListView)findViewById(R.id.expandableListView1); 
    HomePageActivity.homePageTabHost.getTabWidget().getChildTabViewAt(ConstantValues.CURRENT_POSITION) 
     .setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_widget_normal)); 
    asyncTaskMailList=new MailList(); 
    asyncTaskMailList.execute(); 
} 
private OnChildClickListener childClickListener=new ExpandableListView.OnChildClickListener() { 
    public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) { 
     ConstantValues.STATION_NAME=ConstantValues.inboxStations.get(groupPosition); 
     ConstantValues.CURRENT_POSITION=1; 
     startActivity(new Intent(InboxActivity.this, HomePageActivity.class)); 
     return false; 
    } 
}; 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if(keyCode == KeyEvent.KEYCODE_BACK){ 
     this.getParent().onBackPressed(); 
     asyncTaskMailList.cancel(true); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
@Override 
protected void onStop() { 
    super.onStop(); 
    asyncTaskMailList.cancel(true); 
} 
@Override 
protected void onDestroy() { 
    asyncTaskMailList.cancel(true); 
    super.onDestroy(); 
} 
class MailList extends AsyncTask<String, String, String> 
{ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     NetworkExceptionPopUp.showProgressBar(); 
    } 
    @Override 
    protected String doInBackground(String... params) { 
     list=new ArrayList<HashMap<String,String>>(); 
     WebServerCall.getInboxMail(InboxActivity.this); 
     return null; 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     mailList.setAdapter(new InboxAdapter(InboxActivity.this)); 
     NetworkExceptionPopUp.dismissProgressBar(); 
     mailList.setOnGroupClickListener(null); 
     mailList.setOnChildClickListener(childClickListener); 
     mailList.setClickable(true); 

    } 
} 

} 上面的代碼是我更新的代碼。這也有時會顯示異常。在這裏,我已經使用onStop()和OnDestroy()來取消AsyncTask,而其他活動最重要。

+2

你可以檢查logcat,看看是否有堆棧跟蹤? – 2013-03-12 06:44:28

+1

它可能是onPostExecute中的代碼。您正在訪問很多用戶按下後不存在的UI元素。 – Premsuraj 2013-03-12 06:52:33

+0

是的,我的logcat出現了一些錯誤。 – SathishKumar 2013-03-12 06:52:38

回答

2
@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     NetworkExceptionPopUp.showProgressBar(); 
     /** add this line to your progressDialog so that diaolg as well your background operation wont stop in intermediate state. */ 
     // pd.setCancelable(false); 
    } 

所以現在當你點擊後退按鈕時,對話框不會消失。

希望爲您提供幫助。

+0

是的,你是正確的,但我不想讓用戶等待那麼多時間。請提供一些其他的想法。謝謝 – SathishKumar 2013-03-12 06:55:23

1
private MailList task; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    ... 
    task = new MailList().execute(); 
    ... 
} 

@Override 
public void onBackPressed() { 
    if (task != null && task.getStatus() == AsyncTask.Status.RUNNING) { 
     task.cancel(true); 
    } 
    ... 
} 

... 
+0

@覆蓋 \t公共布爾的onkeydown(INT的keyCode,KeyEvent的事件){ \t如果(的keyCode == KeyEvent.KEYCODE_BACK){ \t \t this.getParent()onBackPressed()。 \t \t new MailList()。cancel(true); \t返回true; \t} \t return super.onKeyDown(keyCode,event); \t} – SathishKumar 2013-03-12 06:55:48

+0

我用過這個,但是我不知道它是否有效 – SathishKumar 2013-03-12 06:56:24

+2

@SathishKumar你不能使用新的MailList.cancel(true),你需要保留你的任務的成員變量,就像belyjz指出的那樣,task = new MailList()。execute ,.然後在'task'變量上調用cacel(true) – Premsuraj 2013-03-12 07:07:27

相關問題