2014-02-19 2 views
0

ActivityTabHost이 있습니다. XML 파일은 다음과 같습니다에서안드로이드는 두 번째 탭에서 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> 

내 시작 부분 onCreate 방법에 MainActivity는, 내가 쓴 :

tabHost = (TabHost) findViewById(android.R.id.tabhost); 

    tabHost.setup(); 

    // TabHost.TabSpec tabSpec; 

    View indicator1 = getLayoutInflater().inflate(R.layout.tab1, null); 
    im1 = (ImageView) indicator1.findViewById(R.id.imageView1); 

    View indicator2 = getLayoutInflater().inflate(R.layout.tab2, null); 
    im2 = (ImageView) indicator2.findViewById(R.id.imageView1); 

    View indicator3 = getLayoutInflater().inflate(R.layout.tab3, null); 
    im3 = (ImageView) indicator3.findViewById(R.id.imageView1); 

    TabHost.TabSpec tabSpec; 
    tabSpec = tabHost.newTabSpec("tag1"); 
    tabSpec.setIndicator(indicator1); 
    tabSpec.setContent(R.id.tab1); 
    tabHost.addTab(tabSpec); 

    tabSpec = tabHost.newTabSpec("tag2"); 
    tabSpec.setIndicator(indicator2); 
    tabSpec.setContent(R.id.tab2); 
    tabHost.addTab(tabSpec); 

    tabSpec = tabHost.newTabSpec("tag3"); 
    tabSpec.setIndicator(indicator3); 
    tabSpec.setContent(R.id.tab3); 
    tabHost.addTab(tabSpec); 

    dialog = new ProgressDialog(this); 
    dialog.setMessage(getString(R.string.loading)); 
    dialog.setCanceledOnTouchOutside(false); 

    dictionaryList = (ListView) this.findViewById(R.id.listDictionary); 

것은 내가 쉽게 비동기 작업을 사용 dictionaryList에 데이터를 업로드하고 있습니다. 그 후 나는 TabHost에 리스너를 추가 한 :

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(); 
      } 
     } 
    }); 

을 그것은이 NullPointerException이 나타납니다 favouritesList 얻기 위해 시도합니다. 나는 무엇을 잘못 했는가?

로그 캣은 :

 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과 잘 살고 어댑터를 얻는 방법을 발견했다. 제가 응용 프로그램을 시작하면, 그것은 잘 시작, 실수가 없습니다. 내 실수를 발견

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

null 줄 포인터 예외, pls logcat 게시 – user2450263

+0

favouritesList.toString은 무엇입니까? – user2450263

+0

이 ListView가 있는지 확인하려면 –

답변

0

:이 ListView가있는 두 번째 탭을 클릭하면 그러나, 그것은으로 favouritesList 목록보기 초기화 다시 NullPointerException

0

사용 indicator2 대신 view 날 수 있습니다. 내 어댑터에 내 ListView에 설정됩니다. 거기에 getView() 방법으로 나는 return view을 쓰는 것을 잊었고, 여전히 return null이었다. 어쨌든, 관심을 가져 주셔서 감사합니다.

+0

여전히 NullPointerException이 발생합니다. –