【问题标题】:nullpointerexception in wyswig xml layout in eclipse for android and visual-artifactseclipse中wyswig xml布局中的nullpointerexception for android和visual-artifacts
【发布时间】:2011-05-12 21:47:47
【问题描述】:

我目前正在开发一个需要使用定制选项卡的 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)
...

当我想在 Eclipse 中从文本模式切换到 wyswig 模式时,我遇到了这个异常。 这是给我这个错误的实际 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>

现在第二个问题是图形工件:

我该如何解决我的问题?

【问题讨论】:

    标签: android nullpointerexception tabwidget visual-artifacts


    【解决方案1】:

    很明显哪里出了问题,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 时将ID first 分配给android:checkedButton="@+id/first"。当您实际到达 RadioButton 时,您的 ID 定义不应再包含 +。查看布局。

    【讨论】:

      【解决方案2】:

      我已经设法修复了我认为是视觉伪影的东西:

      <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 是的,我也知道了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        • 2020-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多