尝试更改xml中Views的可见性时编辑器中出现NullPointerException

时间:2015-03-05 16:31:42

标签: android xml layout

我正在使用多个按钮和TextViews&父RelativeLayout中LinearLayouts内的按钮。由于我有多个按钮,因此我将其部分可见性设置为无。现在当我尝试在xml中再次显示它们时,我在编辑器中得到一个NullPointer异常。这是我的xml代码。

<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="wrap_content"
android:background="#fff"
tools:context=".MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:textColor="#fff"
                android:background="@drawable/fab_button_circle"
                android:gravity="center"
                android:text="A"
                android:textSize="20sp"/>

            <Button
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                android:text="@string/conversion1"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion1"
                android:onClick="acceleration"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:textColor="#fff"
                android:background="@drawable/fab_button_circle"
                android:gravity="center"
                android:text="A"
                android:textSize="20sp"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                android:text="@string/conversion2"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion2"
                android:onClick="area"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:textColor="#fff"
                android:text="D"
                android:textSize="20sp"
                android:background="@drawable/fab_button_circle"
                android:gravity="center"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                android:text="@string/conversion3"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion3"
                android:onClick="digitalStorage"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:text="@string/conversion4"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion4"
                android:visibility="gone"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:textColor="#fff"
                android:text="F"
                android:textSize="20sp"
                android:background="@drawable/fab_button_circle"
                android:gravity="center"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                android:text="@string/conversion5"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion5"
                android:onClick="force"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:text="@string/conversion6"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion6"
                android:visibility="gone"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:textColor="#fff"
                android:text="L"
                android:textSize="20sp"
                android:background="@drawable/fab_button_circle"
                android:gravity="center"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                android:text="@string/conversion7"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion7"
                android:onClick="length"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:textColor="#fff"
                android:text="M"
                android:textSize="20sp"
                android:background="@drawable/fab_button_circle"
                android:gravity="center"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                android:text="@string/conversion8"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion8"
                android:onClick="mass"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:textColor="#fff"
                android:text="N"
                android:textSize="20sp"
                android:background="@drawable/fab_button_circle"
                android:gravity="center"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                android:text="@string/conversion9"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion9"
                android:onClick="numberSystem"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:text="@string/conversion10"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion10"
                android:visibility="gone"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:text="@string/conversion11"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion11"
                android:visibility="gone"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:text="@string/conversion12"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion12"
                android:visibility="gone"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:textColor="#fff"
                android:text="T"
                android:textSize="20sp"
                android:background="@drawable/fab_button_circle"
                android:gravity="center"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                android:text="@string/conversion13"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion13"
                android:onClick="temperature"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:text="@string/conversion14"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion14"
                android:visibility="gone"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:text="@string/conversion15"
                android:background="@drawable/simple_button_onclick_effect"
                android:id="@+id/conversion15"
                android:visibility="gone"
                android:textSize="30sp"
                android:paddingLeft="20dp"
                android:gravity="left|center_vertical"/>

        </LinearLayout>

    </LinearLayout>

</ScrollView>

我在这里做错了什么。我甚至无法在LinearLayouts中添加任何视图。

这是错误日志。

java.lang.NullPointerException
at android.view.ViewGroup_Delegate.drawShadow(ViewGroup_Delegate.java:74)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:61)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15017)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15017)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15117)
at android.widget.FrameLayout.draw(FrameLayout.java:592)
at android.widget.ScrollView.draw(ScrollView.java:1689)
at android.view.View.draw(View.java:15022)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15117)
at android.view.View.draw(View.java:15022)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15017)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15117)
at android.view.View.draw(View.java:15022)
at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404)
at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15117)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:587)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:334)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:708)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:697)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:697)
at com.android.tools.idea.rendering.RenderService.render(RenderService.java:816)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:646)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:82)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:589)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:584)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:327)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:1)

没关系。我想到了。看起来Android Studio无法处理太多mane嵌套布局。所以我删除了父RelativeLayout并将所有内容放在ScrollView中。这解决了它。

答案 1 :(得分:1)

我也使用了很多嵌套视图,也遇到了这个问题。

似乎当你有一个内容不在屏幕上的滚动视图时,渲染器无法处理它。

如果您创建一个屏幕分辨率为1080x4000的自定义AVD,您将获得一个长屏,但它至少会在没有空指针的情况下呈现它。