findViewById在Java(R.id.bAdd) [英] findViewById in java (R.id.bAdd)
本文介绍了findViewById在Java(R.id.bAdd)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我一直努力学习Android和我试过最流行的Android thenewbostons的教程,但我坚持错误,我会告诉你MainActivity.java和activity_main.xml中
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。> <的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=你总为0
机器人:TEXTSIZE =45dp
机器人:layout_gravity =中心
机器人:比重=中心
机器人:ID =@ + ID / tvDisplay
/> <按钮
机器人:layout_width =250dp
机器人:layout_height =WRAP_CONTENT
机器人:文字=添加一个
机器人:layout_gravity =中心
机器人:TEXTSIZE =20dp
机器人:ID =@ ID / BADD/> <按钮
机器人:layout_width =250dp
机器人:layout_height =WRAP_CONTENT
机器人:文字=减一
机器人:layout_gravity =中心
机器人:TEXTSIZE =20dp
机器人:ID =@ ID / bSub/>< / LinearLayout中>
这是扔R.id.bAdd的错误Java文件,请帮助我?
包com.example.addsub;进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.widget.Button;
进口android.widget.TextView;
公共类MainActivity延伸活动{ //瓦尔
INT计数器;
按钮添加,子;
TextView中显示; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 计数器= 0;
添加=(按钮)findViewById(R.id.bAdd);
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}}
解决方案
这是错误的:
机器人:ID =@ ID / BADD/>
应该是:
机器人:ID =@ + ID / BADD/>
同为
机器人:ID =@ + ID / bSub/>
@ + ID /表示加我的ID集。结果
否则,将不会有与该ID的控制
@ ID /用于引用按另一个conttrol(比如在RelativeLayout的)
So i've been trying to learn android and I tried the most popular thenewbostons android tutorial, But I am stuck with the error, I'll show you MainActivity.java and Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
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:text="Subtract One"
android:layout_gravity="center"
android:textSize="20dp"
android:id="@id/bSub"/>
</LinearLayout>
The Java File which is throwing the error of R.id.bAdd, Help me please?
package com.example.addsub;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// vars
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);
}
@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;
}
}
解决方案
This is wrong:
android:id="@id/bAdd"/>
it should be:
android:id="@+id/bAdd"/>
same for
android:id="@+id/bSub"/>
"@+id/" means "add me to the id collection".
Otherwise, there won't be a control with that id.
"@id/" is used to reference a conttrol by another (say in a RelativeLayout)
这篇关于findViewById在Java(R.id.bAdd)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文