Android FindViewById的onCreate出现Java.Lang.Null.Pointer异常,这是什么原因造成的?
以前我有一个活动布局,现在我正试图将其复制到一个片段中,作为viewPager的一部分。我复制了旧的XML文件,并更改了所有ID以防冲突。我正在为这个片段引用新的XML布局,并创建了新的ID。但当我运行它时,它会得到这个nullpointerexception 这是logcat文件 下面是java文件中发生错误的部分Android FindViewById的onCreate出现Java.Lang.Null.Pointer异常,这是什么原因造成的?,android,android-layout,android-fragments,android-xml,android-fragmentactivity,Android,Android Layout,Android Fragments,Android Xml,Android Fragmentactivity,以前我有一个活动布局,现在我正试图将其复制到一个片段中,作为viewPager的一部分。我复制了旧的XML文件,并更改了所有ID以防冲突。我正在为这个片段引用新的XML布局,并创建了新的ID。但当我运行它时,它会得到这个nullpointerexception 这是logcat文件 下面是java文件中发生错误的部分 @Override public RelativeLayout onCreateView(LayoutInflater inflater, ViewGroup conta
@Override
public RelativeLayout onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
inflater.inflate(R.layout.one, null);
ViewPager viewFinder = (ViewPager) getView();
//setContentView(R.layout.one);
title = (TextView) viewFinder.findViewById(R.id.frag_AA); //ERROR xml id file probz
这是布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/frag_AA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="I Am Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
原因是
取景器
为空
我看到你把R.layout充气。一个:
inflater.inflate(R.layout.one, null);
但您不使用充气机返回的视图。
inflater.inflate()
返回一个视图,您应该在这里查找标题
文本视图
大概是这样的:
View view = inflater.inflate(R.layout.one, null);
title = (TextView)view.findViewById(R.id.frag_AA);
(我从未使用过ViewPagers,但看一看,我感觉您使用它是错误的,尽管我可能没有正确的方法)尝试下面的代码
View myview = inflater.inflate(R.layout.one, null);
title = (TextView)myview.findViewById(R.id.frag_AA);
因为您的充气机没有为任何布局充气,所以它是null
,因为如果
充气机充气(右布局1,空)代码>是返回类型。
这是一个返回视图。
这就是问题产生的原因。如果您先将布局名称调整为“一”,我可以接受这个答案。很抱歉,当我回答你的问题时,它是breed\u frag1\u layou
。但这并不重要,你知道是什么导致了这个问题以及如何解决它。你的情况不公平,不公平,伙计。:)如果将来我看到你的问题,我会记住这种情况我等了两天,只想保持邮件中的所有内容不变。很抱歉你现在能换吗?