无法使用 findViewById() 找到视图

发布于 2024-11-25 14:15:47 字数 3597 浏览 1 评论 0原文

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

OtherActivity

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 行添加了一个断点,并在调试模式下启动了项目。当它停在断点处时,我将鼠标移动到变量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?

I cannot find a TextView by calling findViewById(), even though the ID does exist.

OtherActivity:

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

I added a breakpoint at line 5, and started the project in debug mode. When it stopped at the break point, I moved the mouse to variable textView, and it was null.

The other_activity layout:

<?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>

The generated R.java class:

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

I don't think the TextView should not be found, but why is the variable textView null?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

離人涙 2024-12-02 14:15:47

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

将您的代码更改为此...

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

You have to call setContentView(...) BEFORE you attempt to find any of the UI components. You're trying to find the TextView before you've called it.

Change your code to this...

super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
TextView textView = (TextView)findViewById(R.id.txt02);
风渺 2024-12-02 14:15:47

更改您的代码,例如 ::

  TextView textView = (TextView)findViewById(R.id.txt02);

没有像“ID”

Main 这样的大写字母:

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

change your code like ::

  TextView textView = (TextView)findViewById(R.id.txt02);

There is no capital letter like "ID"

Main :

 <TextView android:id="@+id/txt02" android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
夏至、离别 2024-12-02 14:15:47

实际上我刚刚遇到了这个问题,它是由于现在自动检查构建而引起的。只需手动构建项目或单击自动构建即可解决您的问题!

I actually just had this problem it was caused by now having build automatically checked. Just either build your project manually or click build automatically and that should solve your problem!

苦行僧 2024-12-02 14:15:47

您必须确保您的 TextView 元素已在创建活动时生成。

最有可能的是,如果它是 Menu 元素,您将必须在 onCreateOptionsMenu() 回调上调用 findViewById 方法,而不是在 Oncreate() 回调上调用。

You have to make sure that your TextView element is already generated on the creation of your activity.

Most likely, if it's a Menu element, you will have to call the findViewById method on the onCreateOptionsMenu() callback and not on the Oncreate() callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文