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);是返回類型。 這是返回一個視圖。 這就是這個問題出現的方式。

相關問題