2014-02-19 15 views
0

我有Activity,它得到了TabHost。 XML文件是:Android無法在第二個標籤上獲取ListView

<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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="70sp" 
      android:layout_alignParentBottom="true" > 

     </TabWidget> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@android:id/tabs" > 

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <ListView 
        android:id="@+id/listDictionary" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

       </ListView> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <ListView 
        android:id="@+id/listFavourites" 
        android:layout_width="match_parent" 
        android:layout_height="356dp" 
        android:layout_weight="1" > 
       </ListView> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab3" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <ScrollView 
        android:id="@+id/scrollView1" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" > 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:orientation="vertical" > 

         <ImageView 
          android:id="@+id/imageView1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:src="@drawable/wikibilim" /> 

         <TextView 
          android:id="@+id/textFavourite" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/wiki" /> 

        </LinearLayout> 
       </ScrollView> 

      </LinearLayout> 
     </FrameLayout> 



    </RelativeLayout> 
</TabHost> 

在我MainActivity在開始時onCreate方法,我寫道:

​​

我使用異步任務輕鬆地上傳數據dictionaryList。當它試圖獲得favouritesList似乎有一個NullPointerException

tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

     @Override 
     public void onTabChanged(String tabId) { 
      // TODO Auto-generated method stub 
      if (tabId.equals("tag2")) { 
       View view = tabHost.getChildAt(tabHost.getCurrentTab()); 
       favouritesList = (ListView) view.findViewById(R.id.listFavourites); 
       Toast.makeText(getApplicationContext(), favouritesList.toString(), Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 

:從那以後,我已經添加監聽器TabHost。我做錯了什麼 ?

的logcat:

 02-19 21:40:52.360: E/AndroidRuntime(25828): FATAL EXCEPTION: main 
     02-19 21:40:52.360: E/AndroidRuntime(25828): java.lang.NullPointerException 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at           kz.wikibilim.sozdik.MainActivity$1.onTabChanged(MainActivity.java:117) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at   android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:402) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.widget.TabHost.setCurrentTab(TabHost.java:387) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:560) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.view.View.performClick(View.java:4223) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.view.View$PerformClick.run(View.java:17275) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.os.Handler.handleCallback(Handler.java:615) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.os.Handler.dispatchMessage(Handler.java:92) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.os.Looper.loop(Looper.java:137) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at android.app.ActivityThread.main(ActivityThread.java:4898) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at java.lang.reflect.Method.invokeNative(Native Method) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at java.lang.reflect.Method.invoke(Method.java:511) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 
     02-19 21:40:52.360: E/AndroidRuntime(25828): at dalvik.system.NativeStart.main(Native Method) 

我發現如何得到這個listFavourites和設置好的適配器。當我開始申請時,它開始順利,沒有錯誤。然而,當我點擊第二個選項卡,在此ListView位於,再次給了我NullPointerException

+0

空指針異常什麼行,請發佈logcat – user2450263

+0

favouritesList.toString是爲? – user2450263

+0

檢查是否找到此ListView –

回答

0

我發現我錯了。這是在我的適配器,將設置在我的ListView。有getView()方法我忘了寫return view,還有return null。無論如何,感謝您的關注。

0

使用indicator2,而不是view初始化favouritesList的ListView爲:

favouritesList = (ListView) indicator2.findViewById(R.id.listFavourites); 
+0

仍然獲得NullPointerException。 –