gpt4 book ai didi

android - 当我更改 xml 元素的位置时强制关闭

转载 作者:行者123 更新时间:2023-11-29 17:58:59 24 4
gpt4 key购买 nike

我正在尝试重新组织我的一项 Activity 。基本上我想做的就是将 linearlayout 元素和文本元素从页面底部移动到中间。但是当我这样做并运行我的应用程序时,我得到了这个错误:

06-26 13:37:38.385: E/AndroidRuntime(11578): FATAL EXCEPTION: main
06-26 13:37:38.385: E/AndroidRuntime(11578): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beerportfoliopro/com.example.beerportfoliopro.BeerPage}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.app.ActivityThread.access$600(ActivityThread.java:151)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.os.Looper.loop(Looper.java:155)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.app.ActivityThread.main(ActivityThread.java:5536)
06-26 13:37:38.385: E/AndroidRuntime(11578): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 13:37:38.385: E/AndroidRuntime(11578): at java.lang.reflect.Method.invoke(Method.java:511)
06-26 13:37:38.385: E/AndroidRuntime(11578): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
06-26 13:37:38.385: E/AndroidRuntime(11578): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
06-26 13:37:38.385: E/AndroidRuntime(11578): at dalvik.system.NativeStart.main(Native Method)
06-26 13:37:38.385: E/AndroidRuntime(11578): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
06-26 13:37:38.385: E/AndroidRuntime(11578): at com.example.beerportfoliopro.BeerPage.onCreate(BeerPage.java:49)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.app.Activity.performCreate(Activity.java:5066)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
06-26 13:37:38.385: E/AndroidRuntime(11578): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)

我原来的 xml 是:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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


<TextView
android:id="@+id/beerTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>

<ImageView android:id="@+id/image"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_margin="10dip"/>



<TableLayout

android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:shrinkColumns="*"
android:stretchColumns="*">

<TableRow
android:id="@+id/tableStatTitles"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:padding="5dip" >

<TextView
android:id="@+id/abvTitle"
android:text="ABV"
android:gravity="center"
android:textStyle = "bold"
android:textSize="20sp"
android:layout_weight="1"

></TextView>

<TextView
android:id="@+id/IBUTitle"
android:text="IBU"
android:gravity="center"
android:textStyle = "bold"
android:textSize="20sp"
android:layout_weight="1"

></TextView>

<TextView
android:id="@+id/glassTitle"
android:text="Glass"
android:gravity="center"
android:textStyle = "bold"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="wrap_content"
></TextView>


</TableRow>

<TableRow
android:id="@+id/tableStat"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:padding="5dip" >

<TextView
android:id="@+id/abv"
android:text=""
android:gravity="center"
android:textSize="15sp"
android:layout_width="wrap_content"
></TextView>

<TextView
android:id="@+id/IBU"
android:text=""
android:gravity="center"
android:textSize="15sp"
android:layout_width="wrap_content"
></TextView>

<TextView
android:id="@+id/glass"
android:text=""
android:gravity="center"
android:textSize="15sp"
android:layout_width="wrap_content"
></TextView>


</TableRow>

</TableLayout>







<Button
android:id="@+id/buttonBrewery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:text=""
android:onClick="viewBrewery"
/>



<Button
android:id="@+id/buttonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:text=""
android:onClick="viewStyle"

/>

<TextView
android:id="@+id/beerDescriptionTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="5dip"
android:text="Description:"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/beerDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:padding="5dip"

></TextView>

<TextView
android:id="@+id/yourPortfolio"
android:textStyle = "bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:text="Your Portfolio:"
android:padding="5dip"
></TextView>

<LinearLayout
android:id="@+id/addBeerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

</LinearLayout>

</LinearLayout>
</ScrollView>

我想要的和强制关闭应用程序的是:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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


<TextView
android:id="@+id/beerTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>

<ImageView android:id="@+id/image"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_margin="10dip"/>



<TableLayout

android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:shrinkColumns="*"
android:stretchColumns="*">

<TableRow
android:id="@+id/tableStatTitles"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:padding="5dip" >

<TextView
android:id="@+id/abvTitle"
android:text="ABV"
android:gravity="center"
android:textStyle = "bold"
android:textSize="20sp"
android:layout_weight="1"

></TextView>

<TextView
android:id="@+id/IBUTitle"
android:text="IBU"
android:gravity="center"
android:textStyle = "bold"
android:textSize="20sp"
android:layout_weight="1"

></TextView>

<TextView
android:id="@+id/glassTitle"
android:text="Glass"
android:gravity="center"
android:textStyle = "bold"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="wrap_content"
></TextView>


</TableRow>

<TableRow
android:id="@+id/tableStat"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:padding="5dip" >

<TextView
android:id="@+id/abv"
android:text=""
android:gravity="center"
android:textSize="15sp"
android:layout_width="wrap_content"
></TextView>

<TextView
android:id="@+id/IBU"
android:text=""
android:gravity="center"
android:textSize="15sp"
android:layout_width="wrap_content"
></TextView>

<TextView
android:id="@+id/glass"
android:text=""
android:gravity="center"
android:textSize="15sp"
android:layout_width="wrap_content"
></TextView>


</TableRow>

</TableLayout>







<Button
android:id="@+id/buttonBrewery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:text=""
android:onClick="viewBrewery"
/>



<Button
android:id="@+id/buttonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:text=""
android:onClick="viewStyle"

/>

<TextView
android:id="@+id/yourPortfolio"
android:textStyle = "bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:text="Your Portfolio:"
android:padding="5dip"
></TextView>

<LinearLayout
android:id="@+id/addBeerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

</LinearLayout>

<TextView
android:id="@+id/beerDescriptionTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="5dip"
android:text="Description:"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/beerDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:padding="5dip"

></TextView>



</LinearLayout>
</ScrollView>

最佳答案

假设您没有在 xml 或 Java 中更改您的 id ,您可能只需要清理您的项目

Project --> Clean... then pick your project

有时 Eclipse 不会立即获取 xml 更改,因此它会抛出这些类转换异常。简单地清理和重建项目通常可以解决此类错误。在拔掉你所有的头发试图弄清楚为什么你会得到这样的异常之前,这是一件快速而简单的事情。

关于android - 当我更改 xml 元素的位置时强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17326961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com