【问题标题】:findViewById returns nullfindViewById 返回 null
【发布时间】:2020-05-03 16:23:58
【问题描述】:

我有代码:

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);
}
}

和布局文件:

<?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.setOnClickListener(buttonCancelListener);抛出异常,因为 buttonCancel 为空。我做错了什么?

【问题讨论】:

  • 我也面临同样的问题。尽管代码中的所有内容看起来都正确,但 FindViewById 使用其中一个按钮返回 null。

标签: android


【解决方案1】:

您的代码没有问题,一切正常。

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

我建议清理你的项目,然后再试一次!!!!

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但是在清理我的项目并再次运行它之后,它就可以完美运行了。

    【讨论】:

      【解决方案3】:

      我不是 100% 确定,但您在类初始化中调用了 findviewbyid。我认为此代码是在 onCreate 方法之前调用的,因此无法找到视图。在 oncreate 方法中初始化监听器应该可以工作。

      【讨论】:

        【解决方案4】:

        有时需要考虑的要点很少!

        即使对于经验丰富的开发人员,这种情况也总是会发生。我建议以下几点,

        • 检查findViewById 是否已添加到所考虑的活动或片段的适当生命周期中。
          • 还要验证布局是否正确膨胀,以及是否在 setContentView 中设置了有效布局 - 在我的情况下这是不正确的。
          • 如上所述,在某些情况下清理项目可以帮助解决此问题。

        【讨论】:

          猜你喜欢
          • 2011-03-16
          • 1970-01-01
          • 1970-01-01
          • 2016-09-18
          • 1970-01-01
          相关资源
          最近更新 更多