以前我有一個活動佈局,我現在試圖在片段中作爲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>
如果您現在先將佈局名稱調整爲「one」,我可以接受此答案。對不起 –
當我回答你的問題時,它是'breed_frag1_layout'。但是這並不重要,你知道是什麼原因導致了這個問題,以及如何解決這個問題。你的情況不公平,不公平的哥們。 :)如果將來我會看到你的問題,我會記住這種情況。 ;) –
我等了2天,只是想讓帖子裏的所有內容都一樣。抱歉。你現在可以換掉嗎? –