gpt4 book ai didi

java - findViewById(R.id.bAdd) 错误

转载 作者:行者123 更新时间:2023-12-01 17:17:42 24 4
gpt4 key购买 nike

我正在尝试学习 Android 应用程序开发,而且我还处于起步阶段。
有人可以告诉我我在这里做错了什么吗?
在我的 MainActivity.java 中,我在添加、子和显示方面遇到错误(bAdd/bSubb/display 无法解析或不是字段)

public class MainActivity extends Activity {

int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45sp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"/>

<Button
android:layout-width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20dp"
android:id="@+id/bAdd" />

<Button
android:layout-width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Subtract one"
android:layout_gravity="center"
android:textSize="20dp"
android:id="@+id/bSub" />
</LinearLayout>

最佳答案

改变这个

 <Button
android:layout-width="250dp"

<Button
android:layout_width="250dp" // _

TextView 和按钮也是如此。

如果你的资源文件中有错误,R.java将不会生成,从而导致错误

关于java - findViewById(R.id.bAdd) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20826993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com