2016-05-16 87 views
0

謝謝大家,我管理!我不得不放入一個開關((按鈕)v)Android onClick問題()

我是相當新的編碼,並不明白爲什麼不工作我的代碼。 我想在一個方法onClick()下合併所有三個按鈕,android studio向我展示了所有內容都沒有錯誤,但是當我運行應用程序時,它立即關閉。

package com.example.trafficlight; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 
final Button LightSide = (Button) findViewById(R.id.LightSide); 
final Button DarkSide = (Button) findViewById(R.id.DarkSide); 
final Button Chubaka = (Button) findViewById(R.id.Chubaka); 
private RelativeLayout mRelativeLayout; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); 
    LightSide.setOnClickListener(this); 
    DarkSide.setOnClickListener(this); 
    Chubaka.setOnClickListener(this); 


} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.LightSide: 
      LightSide.setText("LightSide"); 
      mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); 
     case R.id.DarkSide: 
      DarkSide.setText("DarkSide"); 
      mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.colorRed)); 
     case R.id.Chubaka: 
      Chubaka.setText("Chubakka"); 
      mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.colorBrown)); 

    } 

} 
} 

這是錯誤代碼 12月5日至16日:43:06.874 8223-8223/com.example.trafficlight E/AndroidRuntime:致命異常:主 工藝:com.example.trafficlight,PID:8223 java.lang.NullPointerException:試圖在null對象引用 上的com.example.trafficlight.MainActivity.onClick(MainActivity.java:36)上調用虛擬方法'void android.widget.Button.setText(java.lang.CharSequence)' ) at android.view.View.performClick(View.java:4780) at android.view.View $ PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

+0

OK,初始化所有按鈕的onCreate方法中調用'的setContentView' –

回答

2

你需要移動您的通話findViewById到onCreate方法:

Button LightSide; 
Button DarkSide; 
Button Chubaka; 
private RelativeLayout mRelativeLayout; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); 
    LightSide = (Button) findViewById(R.id.LightSide); 
    DarkSide = (Button) findViewById(R.id.DarkSide); 
    Chubaka = (Button) findViewById(R.id.Chubaka); 
    LightSide.setOnClickListener(this); 
    DarkSide.setOnClickListener(this); 
    Chubaka.setOnClickListener(this); 


} 
+0

檢查這個答案 –

+0

謝謝,現在的應用程序打開,但是當我嘗試再次單擊按鈕中的一個崩潰)的文字是「不幸的是,交通燈有後停止' –

+0

這是行李代碼 –

0

試試這個

Intialise在onCreate方法的所有按鈕。每種情況下

case R.id.LightSide: 
      LightSide.setText("LightSide"); 
      mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); 
break; 
     case R.id.DarkSide: 
      DarkSide.setText("DarkSide"); 
      mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.colorRed)); 
break; 
     case R.id.Chubaka: 
      Chubaka.setText("Chubakka"); 
      mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.colorBrown)); 
break; 
0

這個問題後
插入break;是上述裝置中的位絡合物。我猜你已經在xml文件中鏈接了你的按鈕的點擊事件(onClick =「onClick」)的點擊事件,因爲onclick的參數是View,在這種情況下,該方法只能用於按鈕已被鏈接,爲另一個按鈕。

簡短的答案是:給每個按鈕小部件一個唯一的ID,並實現他們的OnClickListner;