I have Activity that got TabHost. XML file is:

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

In my MainActivity at the begining onCreate method, I wrote:

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

I'm easily uploaded data to dictionaryList using async task. After that I have added listener to 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();
            }
        }
    });

When It tries to get favouritesList there appears an NullPointerException. What did I do wrong ?

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)

I have found how to get this listFavourites and setted adapter. When I start application, it starts well, there is no mistakes. However, when I click on the second tab, where this ListView is located, it gives me again NullPointerException

有帮助吗?

解决方案 2

I have found my mistake. It was in my adapter that will be setted in my ListView. There in getView() method I forgot to write return view, there still was return null. Anyway, Thank you for attention.

其他提示

Use indicator2 instead of view to initialize favouritesList ListView as:

favouritesList = (ListView) indicator2.findViewById(R.id.listFavourites);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top