2011-04-28 28 views
0

我在onCreateDialog()的錯誤NullPointerException異常此代碼:錯誤onCreateDialog在Android的

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class More extends Activity{ 
    static int DIALOG_ID=0; 
    Dialog dialog = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.myfavourate); 
     //showDialog(DIALOG_ID); 

    } 
    protected Dialog onCreateDialog(int id) { 

     switch(id) { 
     case 0: 
      // do the work to define the pause Dialog 

      dialog = new Dialog(this); 
      dialog.setContentView(R.layout.more); 
      dialog.setTitle("Please Select One:"); 
      Button btn_setting = (Button)findViewById(R.id.btn_more_setting); 
      Button btn_about = (Button)findViewById(R.id.btn_more_about); 
      Button btn_privacy = (Button)findViewById(R.id.btn_more_privacy); 
      Button btn_cancel = (Button)findViewById(R.id.btn_more_cancel); 
      btn_setting.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        Intent intent = new Intent(More.this,SettingActivity.class); 
        startActivity(intent); 
       } 
      }); 

      btn_about.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        Intent intent = new Intent(More.this,About.class); 
        startActivity(intent); 

       } 
      }); 

      btn_privacy.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        Intent intent = new Intent(More.this,Privacy_Policy.class); 
        startActivity(intent); 

       } 
      }); 

      btn_cancel.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        More obj = new More(); 
        obj.finish(); 
       } 
      }); 
      break; 

     default: 
      dialog = null; 
     } 
     return dialog; 
    } 

} 
+1

您應該指出nullpointerexception引發的位置。 – 2011-04-28 06:31:30

回答

0

修復

Button btn_setting = (Button)findViewById(R.id.btn_more_setting); 

Button btn_setting = (Button) dialog.findViewById(R.id.btn_more_setting); 

而對於其他按鈕做到這一點。

訣竅是,當你簡單地調用findViewById,它被調用的活動,並沒有你對話按鈕膨脹。相反,他們被誇大了對話框,你應該在對話框中搜索它們。