gpt4 book ai didi

Android.widget,textView 无法转换为 android.widget,button

转载 作者:IT老高 更新时间:2023-10-28 22:03:51 28 4
gpt4 key购买 nike

我在启动我的 Activity 时不断收到运行时错误,它说 android.widget.textview 无法转换为 android.widget.button?

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText" >

<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:text="Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textViewName"
android:text="Level:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_below="@+id/textViewLevel"
android:text="Class:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textViewClass"
android:layout_alignBottom="@+id/textViewClass"
android:layout_centerHorizontal="true"
android:text="Title:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewXP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewTitle"
android:layout_alignLeft="@+id/textViewTitle"
android:text="XP:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewGold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewXP"
android:layout_alignParentTop="true"
android:text="Gold:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewLineBreak"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewClass"
android:text="________________________________________"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />

<TextView
android:id="@+id/textViewGoldValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textViewGold"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/textViewTitle"
android:text="Stats" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="@+id/textViewTitle"
android:text="Equipment" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button2"
android:layout_marginBottom="22dp"
android:text="Inventory" />

<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/button4"
android:layout_alignParentRight="true"
android:text="Shop" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"
android:layout_toRightOf="@+id/textViewXP"
android:text="Class Training" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:text="Crafting" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewLineBreak"
android:text="HP:"
android:textAppearance="?android:attr/textAppearanceMedium" />


<TextView
android:id="@+id/textViewStrTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewLineBreak"
android:layout_alignLeft="@+id/textViewGoldValue"
android:text="N/A"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/textViewXPValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewGoldValue"
android:layout_below="@+id/textViewGoldValue"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewLevelValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewClass"
android:layout_toRightOf="@+id/textViewName"
android:text="1"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/TextViewStrClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewLineBreak"
android:layout_alignLeft="@+id/textViewLevelValue"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/textViewRace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewClass"
android:text="Race:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_toLeftOf="@+id/TextViewStrClass"
android:text="10"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Spinner
android:id="@+id/spinnerMonsters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView4"
android:layout_marginTop="50dp"
android:layout_toLeftOf="@+id/btnBattle"
android:entries="@array/Monsters"
android:prompt="@string/MonsterPrompt" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/spinnerMonsters"
android:layout_alignRight="@+id/textViewXPValue"
android:text="10"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
android:id="@+id/btnBattle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/spinnerMonsters"
android:text="Battle!" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textViewStrTitle"
android:layout_alignRight="@+id/spinnerMonsters"
android:layout_marginRight="14dp"
android:text="/"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewXpNextLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textViewLevel"
android:layout_alignBottom="@+id/textViewLevel"
android:layout_alignLeft="@+id/btnBattle"
android:text="100"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewBattleResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinnerMonsters"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/textViewStrRace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_toRightOf="@+id/textViewClass"
android:text="l"
android:textAppearance="?android:attr/textAppearanceSmall" />


<TextView
android:id="@+id/textViewStrAlliance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textViewStrTitle"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/textViewAlliance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewTitle"
android:layout_toRightOf="@+id/button1"
android:text="Alliance:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewAlliance"
android:layout_below="@+id/textViewLineBreak"
android:text="MP:"
android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

这里发生了什么?翻了好几遍都没找到

编辑:

09-17 18:28:51.485: E/AndroidRuntime(724): FATAL EXCEPTION: main
09-17 18:28:51.485: E/AndroidRuntime(724): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intent_buttontests/com.example.intent_buttontests.PlayScreen}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.os.Looper.loop(Looper.java:137)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-17 18:28:51.485: E/AndroidRuntime(724): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 18:28:51.485: E/AndroidRuntime(724): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 18:28:51.485: E/AndroidRuntime(724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-17 18:28:51.485: E/AndroidRuntime(724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-17 18:28:51.485: E/AndroidRuntime(724): at dalvik.system.NativeStart.main(Native Method)
09-17 18:28:51.485: E/AndroidRuntime(724): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-17 18:28:51.485: E/AndroidRuntime(724): at com.example.intent_buttontests.PlayScreen.onCreate(PlayScreen.java:110)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.Activity.performCreate(Activity.java:4465)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-17 18:28:51.485: E/AndroidRuntime(724): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-17 18:28:51.485: E/AndroidRuntime(724): ... 11 more

最佳答案

I added the logcat. But i have no idea what is happening

这是您的 LogCat 文件中的重要信息:

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
at com.example.intent_buttontests.PlayScreen.onCreate(PlayScreen.java:110)

您正确阅读了错误,它是 ClassCastExceptionCaused by... 下面的行告诉你错误是在哪里引发的,它在第 110 行的 PlayScreen.onCreate() 中。尽我所能告诉第 110 行是:

Button btnBattle = (Button) findViewById(R.id.btnBattle);

但这行很好,btnBattle 的 XML 看起来也很好...

我自己使用您的布局运行了您的 Activity,没有出现任何错误。你清理过你的项目吗?这通常会消除这些“幻影”错误。(在 Eclipse 中,Project -> Clean...)


我确实有一个建议,你有很多按钮可以完成类似的任务。如果您使用 XML onClick 属性,您可以用更少的代码执行相同的操作。首先在您的 Activity 中创建一个方法(称为 launchClick()),如下所示:

public void launchClick(View v) {
Intent intent;

switch(v.getId()) {
case R.id.button1:
intent = new Intent(PlayScreen.this, Inventory.class);
break;

case R.id.button2:
intent = new Intent(PlayScreen.this, Equipment.class);
break;

// etc, etc
}
startActivityForResult(intent, 0);
};

并将 android:onClick 属性添加到您应该在 play_screen.xml 中具有此行为的每个 Button:

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/textViewTitle"
android:onClick="launchClick"
android:text="Stats" />

希望有帮助!

关于Android.widget,textView 无法转换为 android.widget,button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12465551/

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