android - 错误膨胀类按钮

标签 android class button inflate

我刚开始学习计算机科学和 Android 开发。我一直在通过一些 helloworld 演示来尝试和学习。

所以疯狂地尝试使用 onClickListener 重写一个具有一个按钮用于两个按钮的程序。虽然我没有任何编译错误,但我的程序强制关闭:

01-05 11:20:33.968: E/AndroidRuntime(3257): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multbuttontest/com.example.multbuttontest.MultiButtonActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class button

我的 XML 文件看起来像这样(抱歉,我的格式很烂):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MultiButtonActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<button 
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

和代码:

package com.example.multbuttontest;

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

public class MultiButtonActivity extends Activity implements View.OnClickListener{

Button button1, button2;
int touchCount1, touchCount2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multi_button);

    button1 = (Button) findViewById(R.id.button1);
    button1.setText( "Touch Button 1!");
    button1.setOnClickListener(this);

    button2 = (Button) findViewById(R.id.button2);
    button2.setText( "Touch Button 2!");
    button2.setOnClickListener(this);
}

public void onClick(View v){
    switch(v.getId()){
        case R.id.button1:
            touchCount1++;
            button1.setText("Touched b1 " + touchCount1 + " times(s)");
            break;
        case R.id.button2:
            touchCount2++;
            button2.setText("Touched b2 " + touchCount2 + " times(s)");
            break;
    }
}
}

这完全只是为了我的学习目的,代码确实很糟糕。任何帮助将不胜感激。

最佳答案

这不是button,而是Button。 XML 标记指向框架中的 java 类。 Java 区分大小写。

<Button 
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

关于android - 错误膨胀类按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16554380/

相关文章:

android - 两侧的 NavigationView

python - jitclass 属性中的空集

ios - 从 SpriteKit 中的类播放音频

javascript 两个按钮使用单一功能互相改变

javascript - 使用 jquery 将按钮文本初始化为日期值

android - 在 libgdx 游戏 (Android) 中通过加速度计应用水平运动时对象摇晃

java - android.database.sqlite.SQLiteException : no such column: bucket_display_name

java - 如何修复 'NotSerializableException: java.time.format.DateTimeFormatter' 错误

Java - 添加标签/删除按钮

android - Fragment 中的 View 绑定(bind)导致 KotlinNullPointerException 在生命周期范围的协程内运行