findViewById(R.id.bAdd)错误 [英] findViewById(R.id.bAdd) error

查看:154
本文介绍了findViewById(R.id.bAdd)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习Android应用开发和我非常开头。结果
可有一个人请告诉我,我究竟做错了什么?结果
在我MainActivity.java我得到ADD,SUB和显示(BADD / bSubb /显示器不能得到解决或不域)

错误

 公共类MainActivity延伸活动{INT计数器;
按钮添加,子;
TextView中显示;
   @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    计数器= 0;
    添加=(按钮)findViewById(R.id.bAdd);
    分=(按钮)findViewById(R.id.bSub);
    显示=(的TextView)findViewById(R.id.tvDisplay);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}}

activity_main.xml中

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=你总为0
        机器人:TEXTSIZE =45SP
        机器人:layout_gravity =中心
        机器人:比重=中心
        机器人:ID =@ + ID / tvDisplay/>    <按钮
        机器人:布局宽度=250dp
        机器人:layout_height =WRAP_CONTENT
        安卓的onClick =添加一
        机器人:文字=添加一
        机器人:layout_gravity =中心
        机器人:TEXTSIZE =20dp
        机器人:ID =@ + ID / BADD/>    <按钮
        机器人:布局宽度=250dp
        机器人:layout_height =WRAP_CONTENT
        安卓的onClick =添加一
        机器人:文字=减一
        机器人:layout_gravity =中心
        机器人:TEXTSIZE =20dp
        机器人:ID =@ + ID / bSub/>
< / LinearLayout中>


解决方案

更改此

 <按钮
机器人:布局宽度=250dp

 <按钮
机器人:layout_width =250dp// _

同为TextView的和按钮。

如果您在您的资源有错误的文件R. java会不会产生导致错误

I am trying to learn android app development and I am very much at the beginning.
Can some one please tell me what am I doing wrong here?
In my MainActivity.java I get errors on add, sub and display (bAdd/bSubb/display cannot be resolved or is not field)

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>

解决方案

Change this

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

to

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

Same for the textview and buttons.

If you have errors in your resource files R. java will not be generated leading to errors

这篇关于findViewById(R.id.bAdd)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆