Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试识别正在按下的按钮时出错_Java_Android_Android Studio - Fatal编程技术网

Java 尝试识别正在按下的按钮时出错

Java 尝试识别正在按下的按钮时出错,java,android,android-studio,Java,Android,Android Studio,作为Android新手,我试图通过在该按钮上输出“Hello”来确定按下了哪个按钮,但我在执行代码时遇到了一个错误 MainActivity.java 上述代码返回以下错误: java.lang.RuntimeException:无法实例化活动 将findViewById放入onCreate: Button button1, button2; @Override protected void onCreate(Bundle savedInstanceState) {

作为Android新手,我试图通过在该按钮上输出
“Hello”
来确定按下了哪个按钮,但我在执行代码时遇到了一个错误

MainActivity.java

上述代码返回以下错误:

java.lang.RuntimeException:无法实例化活动


findViewById
放入
onCreate

Button button1, button2;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

    }

findViewById
放入
onCreate

Button button1, button2;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

    }

问题在于您的类属性:

Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
onCreate
方法中调用
setContentView
之前,不能调用
findViewById
。更改您的代码:

Button button1;
Button button2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);

}

问题在于您的类属性:

Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
onCreate
方法中调用
setContentView
之前,不能调用
findViewById
。更改您的代码:

Button button1;
Button button2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);

}

您必须在onCreate methode中实例化按钮,如下所示:

Button button1 ;
Button button2 ;

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

    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);

}

问题在于绑定按钮时使用了正确的viewId,因此这些viewId位于activity\u main布局内,因此findViewById将在该布局内搜索这些viewId,所有这些都将在onCreate方法内完成您必须在onCreate方法内实例化按钮,如下所示:

Button button1 ;
Button button2 ;

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

    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);

}

问题在于绑定按钮时使用了正确的视图ID,因此这些视图ID位于活动\u main布局中,因此findViewById将在该布局中搜索这些ViewID,所有这些都将在onCreate方法中完成

请发布完整的stacktrace,以便我们知道错误发生在哪一行。请发布完整的stacktrace,以便我们知道错误发生在哪一行。完美-这很有意义,谢谢!完美-这很有道理,谢谢!