我在运行时得到一个 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/