当我尝试使用 Intent 更改 textview(detail_text1) 时,我得到了一个 NullPointerException。
出现此异常的原因是 findViewById 调用返回 null。
我之前已经尝试过设置上下文,但它仍然给出相同的异常。
请看下面:
详细 Activity
public class DetailActivity extends Activity {
...
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle
savedInstanceState){
//Activity called via intent. Inspect for forecast data.
Intent rcvIntent = getActivity().getIntent();
//TextView text = (TextView) view.findViewById(R.id.detail_text1);
View rootView = inflater.inflate(R.layout.fragment_detail,container
,false);
if(rcvIntent != null && rcvIntent.hasExtra(Intent.EXTRA_TEXT)){
String forecastStr = rcvIntent
.getStringExtra(Intent.EXTRA_TEXT);
//View myView = (View) view.getParent();
//setContentView(R.layout.fragment_detail);
//ERROR HERE!
TextView tv = (TextView)rootView
.findViewById(R.id.detail_text1);
tv.setText(forecastStr);
}
return rootView;
}
}
fragment 细节
<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"
tools:context="com.example.rv.myapplication
.DetailActivity.DetailFragment">
<TextView
android:text="@+id/detail_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Activity 详情
<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.rv.myapplication.DetailActivity"
tools:ignore="MergeRootFrame" />
list
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rv.myapplication" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
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=".DetailActivity"
android:label="@string/title_activity_detail"
android:parentActivityName=".MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.rv.myapplication.MyActivity" />
</activity>
</application>
</manifest>
如果需要更多代码,请告诉我。
谢谢大家!
按要求进口
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
最佳答案
如果您的错误在行中:
TextView tv = (TextView)rootView.findViewById(R.id.detail_text1);
这意味着 rootView
是 null
,这也意味着真正的问题是 inflate()
方法正在返回 null
出于某种原因。请提供来自 Logcat 的错误日志
关于android - 使用 Android 的 TextView 的 FindViewById 上的 NulPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25935634/