1
片段對我的問題不是必需的(所以不要留下大聲笑),但我提到它來解釋我爲什麼要嘗試這樣做。顯示來自Android應用程序對象的對話框(或需要可用於多個活動的例程)
我正在使用片段,所以根據佈局不同的活動將是容器。因此,我需要這個例程可用於多個活動。我有一個公用的例程,無論使用哪個活動,都需要運行,所以爲了不重複代碼,我設置了例程以便從應用程序對象運行。
如果代碼包含在活動中,但該代碼在放入應用程序對象(並根據需要進行修改)時會失敗。當我試圖.show()對話框時,我得到錯誤「無法添加窗口 - 標記null不適用於應用程序」。
這是需要調用的活動之一調用程序的故障例程:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpTo(this, new Intent(this, ChecklistListActivity.class));
return true;
case R.id.mnuDelete:
((KnowUrStuffApp)getApplication()).deleteChecklist(this);//<--This is the call!!!
return true;
default:
return super.onOptionsItemSelected(item);
}
}
這是包含在我的應用程序子類常規:
public void deleteChecklist(final FragmentActivity sender){
Checklist cl = getDbHelper().getCurrentChecklist();
if (cl == null)
Toast.makeText(this, getString(R.string.strSelectAChecklistToDelete), Toast.LENGTH_SHORT).show();
else {
try {
new AlertDialog.Builder(this)
.setMessage(cl.getChecklistTitle() + " " + getString(R.string.strConfirmDelete))
.setCancelable(true)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
performDeleteChecklist();
if (sender instanceof ChecklistDetailActivity)
NavUtils.navigateUpTo(sender, new Intent(sender, ChecklistListActivity.class));
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();//<--This causes exception!
} catch (Exception e) {
Log.e(TAG,e.getLocalizedMessage());
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG * 4).show();
}
}
}
我怎樣才能得到這個工作,或者如果這是一個錯誤的方法來讓這個例程可用於多個活動,我怎麼才能使它可用?
我可以在每個活動中複製代碼以使其正常工作,但是當我進行更改時,我必須記得更新這兩個代碼。此外,我還需要更多的例程,我需要做同樣的事情,所以我真的需要弄清楚如何讓我的例程可用於多個活動。
謝謝你們這麼多! :-D
是的,就是這樣。謝謝Kaedil。還要感謝kcoppock在聊天中告訴我同樣的情況。 :-)我還有幾分鐘才能接受它,但我會返回;-) – 2012-07-27 13:58:50