找不到一个TextView通过调用findViewById方法 [英] Could not find a TextView by calling findViewById method

查看:208
本文介绍了找不到一个TextView通过调用findViewById方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到一个TextView通过调用甚至可以通过ID确实存在findViewById方法。 请参阅我的code:

I cannot find a TextView by calling findViewById method even through the ID does exists. please see my code:

活动:

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是这样的:

I add a breakpoint at line 5 and start project in debug mode, when the executing stopped at the break point, I moved the mouse to variable textView, it is null. the other_activity.xml is like this:

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

和Androidmenifest.xml是这样的:

and the Androidmenifest.xml is like this:

<?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是如下:

the R.java is as below:

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为空。我很困惑。 请你帮我找到问题的根源?

So, I don't think the TextView could not be found, but why the variable textView is null. I was confused. could you please help me to find the root cause?

推荐答案

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

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.

更改code这...

Change your code to this...

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

这篇关于找不到一个TextView通过调用findViewById方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆