0

我只打了一個activity,打開另一個activity。一切工作正常,直到我設置ClickListener。現在我的應用程序部隊在啓動時關閉。如果沒有這兩條線的應用程序正常啓動:由onClickListener造成的關閉力

BUeasycounter.setOnClickListener(this); 
BUrps.setOnClickListener(this); 

這裏是我的全部源:

package com.dano.learning; 

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

public class Menu extends Activity implements View.OnClickListener{ 

    Button BUeasycounter; 
    Button BUrps; 
    @Override 
    protected void onCreate(Bundle MyBundle) { 
     super.onCreate(MyBundle); 
     setContentView(R.layout.activity_main); 
     initializeVars(); 
// next 2 lines cause the problem 
     BUeasycounter.setOnClickListener(this); 
     BUrps.setOnClickListener(this); 
    } 
    public void initializeVars(){ 
     Button BUeasycounter= (Button) findViewById(R.id.BUeasycounter); 
     Button BUrps = (Button) findViewById(R.id.BUrps); 
    } 

    public void onClick(View v) { 
     switch (v.getId()){ 
      case R.id.BUeasycounter: 
       Intent openEasyCounter = new Intent("com.dano.learning.EASYCOUNTER"); 
       startActivity(openEasyCounter); 
       break; 
      case R.id.BUrps: 
       Intent openRPS = new Intent("com.dano.learning.EASYCOUNTER"); 
       startActivity(openRPS); 
       break; 
     }; 

    } 
} 

也許我只是輸入一些錯誤,但我不能找到我的源錯誤超過兩天。感謝您的幫助。

+1

LogCat/StackTrace? –

+0

你必須提供更多信息。堆棧跟蹤會很有幫助。可能在onClick方法中有一個NullPointer。 – RoflcoptrException

+0

發佈您的崩潰logcat。 – Ralgha

回答

2

有問題也不例外堆棧,但基於代碼中,我看到的一個問題是:

Button BUeasycounter; 
    Button BUrps; 

兩者都指向null可能被拋NullPointerException當你做

BUeasycounter.setOnClickListener(this); 
    BUrps.setOnClickListener(this); 

刪除新變量initializeVars方法。

例子:

public void initializeVars(){ 
      BUeasycounter= (Button) findViewById(R.id.BUeasycounter); 
      BUrps = (Button) findViewById(R.id.BUrps); 
    } 

注:Java的命名約定建議使用小寫字母作爲變量名的第一個字母。

1

刪除SMI-冒號後SWICH情況:

public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch (v.getId()){ 
      case R.id.BUeasycounter: 
       Intent openEasyCounter = new Intent("com.dano.learning.EASYCOUNTER"); 
       startActivity(openEasyCounter); 
       break; 
      case R.id.BUrps: 
       Intent openRPS = new Intent("com.dano.learning.EASYCOUNTER"); 
       startActivity(openRPS); 
       break; 
     } //>>>> from here remove semi-colon 

,改變initializeVars方法:

public void initializeVars(){ 
      BUeasycounter= (Button) findViewById(R.id.BUeasycounter); 
      BUrps = (Button) findViewById(R.id.BUrps); 
    } 
0
public void initializeVars(){ 
    Button BUeasycounter= (Button) findViewById(R.id.BUeasycounter); 
    Button BUrps = (Button) findViewById(R.id.BUrps); 
} 

你在這個方法中定義了兩個按鈕,但這些都是局部變量的作用域謊言在這個塊之間。 你不能從此外訪問。你必須初始化局部變量,但例如按鈕變量 還有null.To得到NPE的解決方案,你應該寫爲

public void initializeVars(){ 
    BUeasycounter= (Button) findViewById(R.id.BUeasycounter); 
    BUrps = (Button) findViewById(R.id.BUrps); 
} 

從上面的方法它的實例變量,直到這個類存在分配參考的範圍。 並最後從開關塊的末尾刪除分號。