【问题标题】:Android SDK: findViewById(int) undefined for typeAndroid SDK:findViewById(int) 未定义类型
【发布时间】:2013-02-26 02:06:18
【问题描述】:

这是我第一次尝试创建安卓应用,所以我犯了很多恼人的初学者错误!现在我只是想创建一个非常简单的登录/注册系统。但是,我似乎误解了如何处理用户输入的输入。 从我在这里看到的以前的帖子中,我正在尝试做这样的事情:

//in Register class
public void submission(View view){
    //Call User Constructor based on info received from Registration Activity
    EditText enteredUser = (EditText)findViewbyId(R.id.enteredUser);
    //then use the input from this field to do other stuff      

}

但是由于某种原因,我不断收到错误,即 findViewById(int) 未定义类型 Register(我当前所在的类的名称)。有人可以告诉我我做错了什么吗?我很确定我在顶部有正确的导入语句(android.widget.EditText)。

下面是对应的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=".Register" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter New UserName:" />

<EditText
    android:id="@+id/enteredUser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:ems="10" >

    <requestFocus />
</EditText>

...more input buttons etc...

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/confirmPass"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="64dp"
    android:onClick = "submission" <----- Used to call the submission function after 
    android:text="Submit" />              all inputs have been filled

 </RelativeLayout>

抱歉,我提出了一个冗长的问题,但我将不胜感激。

谢谢!

【问题讨论】:

  • 是的,即公共类Register extends Activity{}(包含我定义的java函数)
  • 尝试清理您的项目。我也很好奇,这是IDE唯一说你有错误的东西吗?或者任何与Android相关的东西? @HoanNguyen 这是一个编译时错误,findViewById() 是 Activity 的一部分。
  • 不,我一直遵循的教程没有指定我是否应该这样做。我该怎么称呼它?
  • @A--C 是的,这是 Eclipse 给出的唯一错误

标签: java android xml sdk


【解决方案1】:

您的编译时错误是由于拼写错误:

findViewbyId

需要

findViewById

然后确保在调用super.onCreate(savedInstanceState) 之后再调用setContentView()。我强烈建议遵循Building Your First App 教程,并清楚地关注代码。

和往常一样,familiarize yourself with the documentation。如果你有编译时错误,先检查一下,你可能只是错字了!

【讨论】:

  • @WakkaWakkaWakka 似乎是写它的人半心半意的努力。构建您的第一个应用程序以及 Vogella 教程是一个很好的来源。
  • 哈,是的,遵循实际的 android 教程可能更有意义。我也会检查vogella。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多