首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >findViewById返回null

findViewById返回null
EN

Stack Overflow用户
提问于 2011-05-09 22:02:23
回答 4查看 11.5K关注 0票数 6

我有代码:

代码语言:javascript
复制
public class HelloWorld extends Activity {

private Button buttonOk;
private Button buttonCancel;

private OnClickListener buttonOkListener = new OnClickListener() {
    public void onClick(View v){
        EditText editText = (EditText)findViewById(R.id.input);

        CharSequence textFromInput = (CharSequence) editText.getText();
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context,textFromInput,duration);
        toast.show();
    }
};

private OnClickListener buttonCancelListener = new OnClickListener() {
    public void onClick(View v){
        EditText editText = (EditText)findViewById(R.id.input);

        CharSequence textFromInput = (CharSequence) editText.getText();
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context,textFromInput,duration);
        toast.show();
    }
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // ToDo add your GUI initialization code here

    setContentView(R.layout.main);

    buttonOk = (Button)findViewById(R.id.buttonOk);
    buttonCancel = (Button)findViewById(R.id.buttonCancel);

    buttonOk.setOnClickListener(buttonOkListener);
    buttonCancel.setOnClickListener(buttonCancelListener);
}
}

和布局文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Wpisz swoje imię:"/>
<EditText
    android:id="@+id/input"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/editbox_background"
    android:layout_below="@id/label"/>
<Button
    android:id="@+id/buttonOk"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/input"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dip"
    android:text="OK" />
<Button
    android:id="@+id/buttonCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/buttonOk"
    android:layout_alignTop="@id/buttonOk"
    android:text="Anuluj" />
</RelativeLayout>

由于buttonCancel为null,因此包含buttonCancel.setOnClickListener(buttonCancelListener);的行将引发异常。我做错了什么?

EN

回答 4

Stack Overflow用户

发布于 2011-05-09 22:22:22

你的代码没有问题,一切都应该正常工作。

通过findViewById()方法遇到null的最常见错误是忘记调用setContentView()或调用它的布局错误。

我建议清理您的项目,然后重试!

票数 14
EN

Stack Overflow用户

发布于 2012-07-18 10:36:02

我也遇到了同样的问题,但在清理我的项目并再次运行它之后,它工作得很好。

票数 2
EN

Stack Overflow用户

发布于 2011-05-09 23:41:38

我不是100%确定,但是你在类初始化中调用了findviewbyid。我认为这段代码是在调用onCreate方法之前调用的,所以找不到视图。在oncreate方法中初始化侦听器应该是可行的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5937962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档