为什么我的findViewById()没有发现我的身份证,我在我的XML code纷纷推出? [英] Why does my findViewById() not find my Id that I have introduced in my xml code?
本文介绍了为什么我的findViewById()没有发现我的身份证,我在我的XML code纷纷推出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很新的Android应用程序的开发。但在我的第一批项目之一,我所遇到的一个错误。我的 findViewById()
未找到编号
已在XML code被引入。
下面是我的Java code:
包com.example.fuzzylogic;进口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);
分=(按钮)findViewById(R.id.bSubtract);
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
}
下面是我的XML code:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。><的TextView
机器人:ID =@ + ID / tvDisplay
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:比重=中心
机器人:文字=你总为0
机器人:TEXTSIZE =45dp/><按钮
机器人:ID =@ + ID / BADD
机器人:layout_width =250dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / tvDisplay
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =20dp
机器人:文字=加一
机器人:TEXTSIZE =25dp/><按钮
机器人:ID =@ + ID / bSubtract
机器人:layout_width =250dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / BADD
机器人:layout_below =@ + ID / BADD
机器人:layout_marginTop =23dp
机器人:文字=。减去一
机器人:TEXTSIZE =25dp/>< / RelativeLayout的>
解决方案
我findViewById()未找到已在引入一个Id
XML code。
块引用>因此,这可能会导致:
- 在XML文件(S),然后错误
R.java
不正确生成- 您没有保存的所有编辑XML文件(Ctrl + S)
因此,使它确保你没有在你的项目的错误,干净,重新构建项目。
I am very new to Android app development. But in one of my first projects I have come across an error. My
findViewById()
does not find anId
that has been introduced in the xml code.Here is my Java code:
package com.example.fuzzylogic; 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 { 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.bSubtract); } @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; } }
Here is my xml code:
<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=".MainActivity" > <TextView android:id="@+id/tvDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Your total is 0" android:textSize="45dp" /> <Button android:id="@+id/bAdd" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_below="@+id/tvDisplay" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="add one" android:textSize="25dp" /> <Button android:id="@+id/bSubtract" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignLeft="@+id/bAdd" android:layout_below="@+id/bAdd" android:layout_marginTop="23dp" android:text="substract one" android:textSize="25dp" /> </RelativeLayout>
解决方案My findViewById() does not find an Id that has been introduced in the xml code.
So this may cause:
- Errors in XML file(s) and then
R.java
is not correctly generated- You did not save all edited xml files(ctrl+s)
So make it sure that you don't have errors in your project and clean and rebuild your project.
这篇关于为什么我的findViewById()没有发现我的身份证,我在我的XML code纷纷推出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文