java - Android 检索 TextView 结果为 Null。已设置ContentView;使用正确的 ID;在 Maifest 有 Activity

标签 java android xml android-intent android-fragments

我在运行时得到一个 nullPointerE,并研究了可能导致它的问题,我相信我没有遇到任何问题。

我认为问题在于我的布局位于 fragment 中,尽管应用程序在我激活 Intent 时正确加载它,但我认为当我 setContentView 时它找不到那里的 View 。我尝试将 fragment 设置为内容 View ,但它也崩溃了。另外,我正在使用另一个屏幕的 fragment ,并且可以很好地访问 View 。我认为它可能是从 Intent 访问字符串,但我确认当我在 onCreate 方法中使用 findViewById 和 if 语句时,我得到了一个 null 对象。该应用程序运行得很好(当然不执行内部代码)。

这是我的类(class):

public class Home extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    Intent prev = getIntent();
    String tipOfTheDay = prev.getStringExtra(Login.EXTRA_TIP);
    String welcome = prev.getStringExtra(Login.EXTRA_WELCOME);
    TextView welcomeScreen = (TextView) findViewById(R.id.textViewHome);
    if (welcomeScreen != null) {
        welcomeScreen.setText(welcome + "\n" + tipOfTheDay);
    }
}

这是我的activity_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wingman.Home"
tools:ignore="MergeRootFrame" />

这是我的fragment_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.wingman.Home$PlaceholderFragment" >

<TextView
    android:id="@+id/textViewHome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:text="@string/empty" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton1"
    android:layout_below="@+id/imageButton1"
    android:src="@drawable/ic_launcher"
    android:contentDescription="@string/q_button" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton3"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="40dp"
    android:src="@drawable/ic_launcher"
    android:contentDescription="@string/calendar_button" />

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageButton2"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_launcher"
    android:contentDescription="@string/tips_button" />

<ImageButton
    android:id="@+id/imageButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageButton3"
    android:layout_below="@+id/imageButton3"
    android:src="@drawable/ic_launcher"
    android:contentDescription="@string/texts_button" />

这是我的 list :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wingman"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.wingman.Login"
        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="com.example.wingman.Home"
        android:label="@string/title_activity_home" >
    </activity>
</application>

感谢您的浏览。我正在尝试对 fragment 进行更多研究,但老实说我不知道​​这是否是原因。

最佳答案

您的TextView位于 fragment xml中,但是当您调用时

TextView welcomeScreen = (TextView) findViewById(R.id.textViewHome);  

它正在activity_home.xml中寻找

关于java - Android 检索 TextView 结果为 Null。已设置ContentView;使用正确的 ID;在 Maifest 有 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22862767/

相关文章:

java - View 或 Fragment 库为常见数据类型组成 UI

Java - 初始化游戏后出错 - 扫雷

java - 从线程执行时相对路径被更改

android - Google Play 警告 : Your app contains a File-based XSS issue? JavaScript 启用 = True

三星 Galaxy S 上的 Android 开发

同一项目中的 Java Spring Boot MVC 和 CLI API

android - 迁移到 androidx 后错误膨胀类 androidx.appcompat.widget.FitWindowsFrameLayout

android - 使用 Android 库项目时,如何正确引用 xml 资源?

python - 在 Python 中解析 XML 字符串

java - 适用于 Android 的 BBC iPlayer 广播应用程序 - 滚轮导航