android - 无法使用 findViewById() 找到 View

标签 android android-layout android-view findviewbyid

我无法通过调用 findViewById() 找到 TextView,即使 ID 确实存在。

其他 Activity :

public class OtherActivity extends Activity { 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TextView textView = (TextView)findViewById(R.ID.txt02);
        super.onCreate(savedInstanceState);//line 5
        setContentView(R.layout.other_activity);
        //textView.setText(ACCESSIBILITY_SERVICE);
        Button button = (Button)findViewById(R.ID.otherActivity_Btn);
        button.setText(R.string.otherActivityBtn);
        button.setOnClickListener(new GoBackBtnListener(this));
    }

    class GoBackBtnListener implements OnClickListener {
        OtherActivity other = null;
        public GoBackBtnListener(OtherActivity p_other) {
            other = p_other;
        }
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(other, Activity01Activity.class);
            other.startActivity(intent);
        }
    }
}

我在第 5 行添加了一个断点,并以 Debug模式启动了项目。当它停在断点处时,我将鼠标移动到变量textView,它是空的。

other_activity 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<TextView 
    android:id="@+ID/txt02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

<Button 
    android:id="@+ID/otherActivity_Btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>
</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hp" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity01Activity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".OtherActivity" android:label="@string/other"></activity>
    </application>
</manifest>

生成的R.java类:

public final class R {
    public static final class ID {
        public static final int Activity01_btn=0x7f050001;
        public static final int otherActivity_Btn=0x7f050003;
        public static final int txt01=0x7f050000;
        public static final int txt02=0x7f050002;
    }
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
        public static final int other_activity=0x7f030001;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
        public static final int other=0x7f040002;
        public static final int otherActivityBtn=0x7f040003;
    }
}

我不认为TextView不应该被找到,但为什么变量textView为null?

最佳答案

在尝试查找任何 UI 组件之前,您必须调用 setContentView(...)。您试图在调用 TextView 之前找到它。

将您的代码更改为...

super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
TextView textView = (TextView)findViewById(R.id.txt02);

关于android - 无法使用 findViewById() 找到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6761366/

相关文章:

android - AppWidgetHost 如何将焦点设置到 AppWidget 按钮?

android - 固定屏幕布局比例

android - 如何在 RelativeLayout 中将一个 View 放置在另一个 View 之上?

android - 如何在合并的自定义 android View 上维护布局属性?

android - 签署我的 flutter 应用程序时,我无法构建该应用程序 *已编辑*

android - 带有 ProgressDialog 的 AsyncTask 与方向更改

Android Studio 3.5 我的按钮怎么了?

java - 检查 fragment 当前是否可见

java - 在 kotlin 类中创建 "public"常量

android - 将 TabHost 控件添加到 Linearlayout/在 tabhost 选项卡下添加布局