onClick()

时间:2016-05-16 10:20:03

标签: java android

谢谢大家,我做到了!我不得不放入一个开关((按钮)v)

我在编码方面很陌生,无法理解为什么我的代码不起作用。 我想在一个方法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));

    }

}
}

这是错误代码     05-16 12:43:06.874 8223-8223 / com.example.trafficlight E / AndroidRuntime:FATAL EXCEPTION:main                                                                         处理:com.example.trafficlight,PID:8223                                                                         java.lang.NullPointerException:尝试调用虚方法' void android.widget.Button.setText(java.lang.CharSequence)'在null对象引用上                                                                             在com.example.trafficlight.MainActivity.onClick(MainActivity.java:36)                                                                             在android.view.View.performClick(View.java:4780)                                                                             在android.view.View $ PerformClick.run(View.java:19866)                                                                             在android.os.Handler.handleCallback(Handler.java:739)                                                                             在android.os.Handler.dispatchMessage(Handler.java:95)                                                                             在android.os.Looper.loop(Looper.java:135)                                                                             在android.app.ActivityThread.main(ActivityThread.java:5254)                                                                             at java.lang.reflect.Method.invoke(Native Method)                                                                             在java.lang.reflect.Method.invoke(Method.java:372)                                                                             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

4 个答案:

答案 0 :(得分:3)

您需要将findViewById来电转移到onCreate方法:

Button mLightSide;
Button mDarkSide;
Button mChubaka;
private RelativeLayout mRelativeLayout;


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


}

答案 1 :(得分:0)

试试这个

在OnCreate方法中初始化所有按钮 在每个案例之后插入break;

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;

答案 2 :(得分:0)

这个问题在机制上有点复杂。我猜你已经在xml文件中链接了click事件,其中你设置了一个按钮的点击事件(onClick =" onClick"),因为onclick的参数是View,在这里例如,该方法仅适用于已链接的按钮,用于另一个按钮。

简短的回答是:给每个按钮小部件一个唯一的ID,并实现他们的OnClickListner;

答案 3 :(得分:0)

尝试为所有按钮的onClick事件创建一个新函数,并且不要覆盖Activity 1。另外,插入休息;在每个案例之后:

SortableElement.js

EDITED: 另外,像NSimon这样的findViewById方法在他/她的回答中说。