Android应用程序完成概率

时间:2011-02-23 14:04:18

标签: android nullpointerexception dialog

我正在使用自定义警报对话框。如果用户使用代码的否定按钮,我需要完全关闭应用程序。我正在使用以下代码。

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。我的代码有问题吗?

2 个答案:

答案 0 :(得分:0)

这是与完成时cursor.close()相关的问题。在完成期间没有这样的接近()

答案 1 :(得分:-1)

请显示错误的堆栈跟踪。

在没有看到堆栈跟踪的情况下疯狂猜测我会说调用this.finish()(而不是finish())是你的问题,因为“这个”可能并不是指你正在尝试的活动端。

另请参阅此链接:
http://www.connorgarvey.com/blog/?p=93
完成类似的事情。