2

私は次のコードを取得java.lang.NullPointerExceptionしています。createTabContent2つのタブスペックがあります。呼び出してタブを設定したときに、初めてタブを変更しても問題ありません。しかし、2番目のタブを表示しているときにもう一度呼び出すと、次の行のnullポインター例外が発生します。NoStudentText.setVisibility(View.VISIBLE);

学生リストのデータがない場合は、学生テキストなしを表示します。初めての通話のテキストが表示されます。しかし、そのタブを2回呼び出すと、エラーが発生します。

tspecStudent.setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                if(listStudent != null && listStudent .size() > 0) {
                    //show the student list
                } else {
                    TextView noStudentText = (TextView)findViewById(R.id.NoStudentText);
                    noStudentText.setVisibility(View.VISIBLE);
                    return noStudentText;
                }

            }
        });

メインレイアウトに別のxmlを含めています。

main.xml

<TabHost
        android:id="@+id/tabhostMain"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" > 
....
<include layout="@layout/tab_student" />

</TabHost>

tab_student.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

 <ListView
        android:id="@+id/StudentList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false" />

<TextView
        android:id="@+id/NoStudentText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="No Student"
        android:visibility="gone" />

</FrameLayout>
4

2 に答える 2

2

適用された条件が原因で初期化できない場合があります。この変数を次のようにグローバルにします。

    TextView _noStudentText;

onCreateメソッドで初期化します::

   _noStudentText = (TextView)findViewById(R.id.NoStudentText);

次に、このように必要に応じて可視性を変更します-

    _noStudentText.setVisibility(View.VISIBLE);
于 2012-11-26T05:54:44.100 に答える
0

たぶんあなたは追加するのを忘れますsetContentView(R.layout.your_layout);

super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
于 2012-11-26T04:59:44.703 に答える