nullpointerexception в wyswig xml layout в Eclipse для Android и визуальных артефактов - PullRequest
1 голос
/ 20 ноября 2010

В настоящее время я разрабатываю приложение для Android, в котором необходимо использовать пользовательские вкладки. Я столкнулся с двумя проблемами:

Я начну с моей первой проблемы:

java.lang.NullPointerException
at android.widget.TabWidget.initTabWidget(TabWidget.java:115)
at android.widget.TabWidget.<init>(TabWidget.java:86)
at android.widget.TabWidget.<init>(TabWidget.java:69)
...

Я получаю это исключение, когда хочу переключиться из текстового режима в режим wyswig в Eclipse. Это фактический код XML, который дает мне эту ошибку:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

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

       <FrameLayout android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="0dip"
           android:layout_weight="1"
           android:padding="20dip"
           android:background="#fff" />

       <RadioGroup android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"
           android:checkedButton="@+id/first"
           android:id="@+id/states">
           <RadioButton android:id="@+id/first"
               android:background="#FF00FF"
               android:width="80dip"
               android:height="70dip" />
           <RadioButton android:id="@+id/second"
               android:background="#FFFFFF"
               android:width="80dip"
               android:height="70dip" />
           <RadioButton android:id="@+id/third"
               android:background="#00FFFF"
               android:width="80dip"
               android:height="70dip" />
           <RadioButton android:id="@+id/fourth"
               android:background="#0000FF"
               android:width="80dip"
               android:height="70dip" />
       </RadioGroup>

      <TabWidget android:id="@android:id/tabs"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="0"
          android:visibility="gone" />  
    </LinearLayout>
</TabHost>

Теперь вторая проблема - это Графический Артефакт: http://img529.imageshack.us/img529/8216/99725485.png

Как я могу решить свои проблемы?

Ответы [ 2 ]

0 голосов
/ 20 ноября 2010

Мне удалось исправить то, что я считал визуальным артефактом:

<FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_weight="1" android:background="#FFFFFF" />

также кажется, что всякий раз, когда я использую TabHost и TabWidget в своих проектах, я получаю это исключение, также я проверял его на уроках Android: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html и да, у меня тоже есть

0 голосов
/ 20 ноября 2010

Совершенно очевидно, что не так, и сообщение в Eclipse точно говорит вам, в чем заключается ошибка. Ваш FrameLayout не имеет содержимого.

Чего вы пытаетесь достичь с этим FrameLayout?

ОБНОВЛЕНИЕ: Просто понял, что ты пытался сделать. Попробуйте этот макет.

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:padding="20dip"
            android:background="#fff">
            <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:checkedButton="@+id/first"
                android:id="@+id/states">
                <RadioButton
                    android:id="@id/first"
                    android:background="#FF00FF"
                    android:width="80dip"
                    android:height="70dip"/>
                <RadioButton
                    android:id="@+id/second"
                    android:background="#FFFFFF"
                    android:width="80dip"
                    android:height="70dip"/>
                <RadioButton
                    android:id="@+id/third"
                    android:background="#00FFFF"
                    android:width="80dip"
                    android:height="70dip"/>
                <RadioButton
                    android:id="@+id/fourth"
                    android:background="#0000FF"
                    android:width="80dip"
                    android:height="70dip"/>
            </RadioGroup>
        </FrameLayout>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:visibility="gone"/>
    </LinearLayout>
</TabHost>

Я также исправил еще одну ошибку для вас. На самом деле не обязательно ошибка, но все же. В RadioGroup вы присваиваете первому RadioButton идентификатор first с android:checkedButton="@+id/first", когда ссылаетесь на него. Когда вы действительно доберетесь до RadioButton, ваше определение идентификатора не должно больше содержать +. Проверьте макет.

...