2013-07-18 92 views
0

我製作了一個計算器應用程序,我試圖創建一個關於顯示一些信息的頁面。 enter image description here 確定按鈕將被編碼setContentView(originallayout.xml)返回到計算器佈局。我應該在哪裏聲明不同佈局的按鈕?

我應該在哪裏放這些代碼來聲明OK按鈕?

private Button btnOK;

btnOK = (Button)findViewById(R.id.btnOK);

btnok.setOnClickListener(OKListener);

我試圖把略低於推出後在那裏我沒有在主要佈局的按鈕,但該應用程序剛剛停止這些代碼。

07-18 09:39:43.290: E/AndroidRuntime(6984): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hychentsa.calculator/com.hychentsa.calculator.CalculatorActivity}: java.lang.NullPointerException

+0

您從不同的視圖層次初始化按鈕?您可以將當前視圖層次結構的'findViewById'設置爲活動。 – Raghunandan

回答

1

,如果你的佈局不含量按鈕的ID(你的情況btnOK),Eclipse將拋出NullPointerException異常 - 它不能在佈局中的內容找到它。 所以當你設置你的佈局(或菜單)時,它必須包含id btnOK。覈實!

把你的按鈕初始化setContentView(R.layout.your_about_layout_name); 把所有的代碼

Button btnOK; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.about); 

btnOK = (Button)findViewById(R.id.btnOK); 
btnok.setOnClickListener(OKListener); 
} 

更新: 看invertigo的答案: 是錯誤的改變佈局當你點擊按鈕。 你要做這樣說:

CalculatorActivity

public class CalculatorActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.calculator_black); 

// initialization of your views stays here 
} 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.calculator_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
      case R.id.your_id_to_go_in_about_activity: 
       Intent intent = new Intent(CalculatorActivity.this, AboutActivity.class); 

       // put some extras if you need to send information from this page to the 
       // AboutActivity page with this code: intent.putExtra(); 
       startActivity(intent); // with this code you go to AboutActivity 
       return true; 
      case R.id.theme: 
       // Do Something with the theme 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

現在,您OKButton初始化的地方是在新的班級,讓我們把它叫做 AboutActivity 在這裏,你可以把我的較早的代碼:

public class AboutActivity extends Activity{ 

Button btnOK; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.about); 

btnOK = (Button)findViewById(R.id.btnOK); 
btnok.setOnClickListener(OKListener); 
} 

// and the listener for your OK button have to look like this: 
OnClickListener OKListener = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // Do something here if you need 
      finish(); // with finish() you are returning to the previous page 
      // which is CalculatorActivity 
     } 
    }; 
} 
+0

對不起,它不工作... 其實當我打開應用程序它將設置佈局爲'R.layout.calculator_black',我點擊菜單中的「約」項後,它會將佈局設置爲'R.layout .about',這個佈局包含一個Button btnOK。我想設置這個按鈕,以便在單擊時將佈局設置回R.layout.calculator_black。 – user2236096

+0

在完成更新後,您是否嘗試過我的代碼?它應該工作。 – user2086554

相關問題