当我试图使用意图更改textview(detail_text1)时,我会得到一个NullPointerException。
此异常的原因是,findViewById调用返回null。
我之前已经尝试过设置上下文,但它仍然提供了相同的例外。
请见下文:
细节活动
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;
}
}碎片细节
<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>活动细节
<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" />清单
<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;发布于 2014-09-19 14:47:04
你的问题是:
在活动生命周期中,onCreateView是:
public View onCreateView (View parent, String name, Context context, AttributeSet attrs)和
onCreateView(String name, Context context, AttributeSet attrs)但是在片段生命周期中,onCreatView是
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)所以你把这些搞砸了。
那你该怎么办?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_detail);
Intent rcvIntent = getActivity().getIntent();
if(rcvIntent != null && rcvIntent.hasExtra(Intent.EXTRA_TEXT)){
String forecastStr = rcvIntent
.getStringExtra(Intent.EXTRA_TEXT);
TextView tv = (TextView)findViewById(R.id.detail_text1);
tv.setText(forecastStr);
}
}发布于 2014-09-19 14:41:07
如果您的错误在行中:
TextView tv = (TextView)rootView.findViewById(R.id.detail_text1);这意味着rootView是null,这也意味着真正的问题是inflate()方法由于某种原因返回null。请从Logcat中提供错误日志。
https://stackoverflow.com/questions/25935634
复制相似问题