2011-02-23 51 views
0

我正在使用自定义警报对话框。如果用户使用代码的负面按钮,我需要完全关闭应用程序。我正在使用以下代码。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。我的代码有什么问题?

+3

@ user525004:“我需要完全关闭应用程序” - 在Android中没有这样的概念,比在Web应用程序中更多。 “由于'无法销毁活动',它给出NullpointerException” - 我怀疑你误读了你的堆栈跟踪。请考虑通过编辑您的问题来发布堆栈跟踪。 – CommonsWare 2011-02-23 14:09:36

+0

亚....我需要关闭非常活动。就这样。 – 2011-02-23 14:19:42

回答

0

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

-1

请显示错误的堆栈跟踪。

在没有看到堆栈跟踪的情况下进行狂野猜测我会说,调用this.finish()(而不是finish())是您的问题,因为“this”可能不是指您要结束的活动。

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

+0

不,我也没有试过。它给出同样的错误。 – 2011-02-23 14:20:17