我正在使用自定义警报对话框。如果用户使用代码的负面按钮,我需要完全关闭应用程序。我正在使用以下代码。Android应用程序完成概率
public class TestApp extends TabActivity {
private int tabid = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
Intent myint = this.getIntent();
tabid = myint.getIntExtra("tab_id", 0);
.......................
.........................
.......................
tabHost.setCurrentTab(tabid);
showReward(this);
}
private void showReward(Context c) {
// TODO Auto-generated method stub
final AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = c;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.title);
text.setText("Sample text");
text.setGravity(Gravity.CENTER);
TextView msg = (TextView) layout.findViewById(R.id.msg);
msg.setText("Sample text.");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d(null,"step1");
dialog.cancel();
Log.d(null,"step2");
closeApp();
}
});
alertDialog = builder.create();
alertDialog.show();
}
private void closeApp(){
Log.d(null,"step3");
this.finish();
}
}
但是在添加showReward()函数之前它完美地工作。当你添加该功能时,对话框显示完美。如果我们点击否定按钮,则由于“无法销毁活动”而导致NullpointerException。我的代码有什么问题?
@ user525004:“我需要完全关闭应用程序” - 在Android中没有这样的概念,比在Web应用程序中更多。 “由于'无法销毁活动',它给出NullpointerException” - 我怀疑你误读了你的堆栈跟踪。请考虑通过编辑您的问题来发布堆栈跟踪。 – CommonsWare 2011-02-23 14:09:36
亚....我需要关闭非常活动。就这样。 – 2011-02-23 14:19:42