0

以前我有一个活动布局,我现在试图在片段中作为viewPager的一部分进行复制。我复制了旧的XML文件并在发生冲突时更改了所有ID。我为这个片段引用了新的XML布局,并创建了新的ID。但是当我运行它时,它会得到这个nullpointerexception。对于FindViewById,onCreate上的Java.Lang.Null.Pointer异常,可能是什么原因造成的?

这里是logcat的文件 http://i48.tinypic.com/14ekq6s.png

这里是在有错误发生java文件的一部分。

@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> 

回答

1

原因是viewFinder为空。

我看你膨胀R.layout.one

inflater.inflate(R.layout.one, null); 

,但你不要吹气使用返回的视图。 inflater.inflate()返回一个视图,这是你应该寻找title TextView的地方。

事情是这样的:

View view = inflater.inflate(R.layout.one, null); 
title = (TextView)view.findViewById(R.id.frag_AA); 

(我从来没有与ViewPagers工作,但考虑看看over this page,我中有你使用的是错误的感觉,虽然我可能没有右)

+0

如果您现在先将布局名称调整为“one”,我可以接受此答案。对不起 –

+0

当我回答你的问题时,它是'breed_frag1_layout'。但是这并不重要,你知道是什么原因导致了这个问题,以及如何解决这个问题。你的情况不公平,不公平的哥们。 :)如果将来我会看到你的问题,我会记住这种情况。 ;) –

+0

我等了2天,只是想让帖子里的所有内容都一样。抱歉。你现在可以换掉吗? –

0

尝试这下面的代码..

View myview = inflater.inflate(R.layout.one, null); 
title = (TextView)myview.findViewById(R.id.frag_AA); 

,因为你不打气筒充气任何布局是null因为如果

inflater.inflate(R.layout.one, null);是返回类型。 这是返回一个视图。 这就是这个问题出现的方式。

相关问题