Android.widget,TextView的不能转换为android.widget,按钮 [英] Android.widget,textView cannot be cast to android.widget,button

查看:363
本文介绍了Android.widget,TextView的不能转换为android.widget,按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断推出我的活动时,得到一个运行时错误,它说android.widget.textview不能转换为android.widget.button?

XML:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / RelativeLayout1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =match_parent
    机器人:方向=垂直
    工具:忽略=硬codedText>

    <的TextView
        机器人:ID =@ + ID / textViewName
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =假
        机器人:layout_alignParentTop =假
        机器人:文本=名称:
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewLevel
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentRight =真
        机器人:layout_below =@ + ID / textViewName
        机器人:文本=等级
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewClass
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =假
        机器人:layout_below =@ + ID / textViewLevel
        机器人:文本=类:
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewTitle
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / textViewClass
        机器人:layout_alignBottom =@ + ID / textViewClass
        机器人:layout_centerHorizo​​ntal =真
        机器人:文本=标题:
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewXP
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textViewTitle
        机器人:layout_alignLeft =@ + ID / textViewTitle
        机器人:文本=XP
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewGold
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / textViewXP
        机器人:layout_alignParentTop =真
        机器人:文本=金牌
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewLineBreak
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textViewClass
        机器人:文本=________________________________________
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:能见度=隐形/>

    <的TextView
        机器人:ID =@ + ID / textViewGoldValue
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_toRightOf =@ + ID / textViewGold
        机器人:文本=0
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <按钮
        机器人:ID =@ + ID /按钮3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentLeft =真
        机器人:layout_toLeftOf =@ + ID / textViewTitle
        机器人:文本=统计/>

    <按钮
        机器人:ID =@ + ID /按钮2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID /按钮3
        机器人:layout_alignParentLeft =真
        机器人:layout_marginBottom =20dp
        机器人:layout_toLeftOf =@ + ID / textViewTitle
        机器人:文本=设备/>

    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID /按钮2
        机器人:layout_alignParentLeft =真
        机器人:layout_alignRight =@ + ID /按钮2
        机器人:layout_marginBottom =22dp
        机器人:文本=盘点/>

    <按钮
        机器人:ID =@ + ID / Button01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID /按钮1
        机器人:layout_alignBottom =@ + ID /按钮1
        机器人:layout_alignLeft =@ + ID /将Button4
        机器人:layout_alignParentRight =真
        机器人:文本=商店/>

    <按钮
        机器人:ID =@ + ID / button5
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_below =@ + ID /按钮2
        机器人:layout_toRightOf =@ + ID / textViewXP
        机器人:文本=课堂培训/>

    <按钮
        机器人:ID =@ + ID /将Button4
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / button5
        机器人:layout_alignParentRight =真
        机器人:layout_below =@ + ID /按钮1
        机器人:文本=手工艺/>

    <的TextView
        机器人:ID =@ + ID / textView4
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textViewLineBreak
        机器人:文本=HP
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>


    <的TextView
        机器人:ID =@ + ID / textViewStrTitle
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textViewLineBreak
        机器人:layout_alignLeft =@ + ID / textViewGoldValue
        机器人:文本=N / A
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

    <的TextView
        机器人:ID =@ + ID / textViewXPValue
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / textViewGoldValue
        机器人:layout_below =@ + ID / textViewGoldValue
        机器人:文本=0
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewLevelValue
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textViewClass
        机器人:layout_toRightOf =@ + ID / textViewName
        机器人:文本=1
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / TextViewStrClass
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textViewLineBreak
        机器人:layout_alignLeft =@ + ID / textViewLevelValue
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

    <的TextView
        机器人:ID =@ + ID / textViewRace
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textViewClass
        机器人:文本=种族
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / textView4
        机器人:layout_alignBottom =@ + ID / textView4
        机器人:layout_toLeftOf =@ + ID / TextViewStrClass
        机器人:文本=10
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <微调
        机器人:ID =@ + ID / spinnerMonsters
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textView4
        机器人:layout_marginTop =50dp
        机器人:layout_toLeftOf =@ + ID / btnBattle
        机器人:项=@阵列/怪兽
        机器人:提示=@字符串/ MonsterPrompt/>

    <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / spinnerMonsters
        机器人:layout_alignRight =@ + ID / textViewXPValue
        机器人:文本=10
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <按钮
        机器人:ID =@ + ID / btnBattle
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignTop =@ + ID / spinnerMonsters
        机器人:文本=战斗! />

    <的TextView
        机器人:ID =@ + ID / textView3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textViewStrTitle
        机器人:layout_alignRight =@ + ID / spinnerMonsters
        机器人:layout_marginRight =14dp
        机器人:文本=/
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewXpNextLevel
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / textViewLevel
        机器人:layout_alignBottom =@ + ID / textViewLevel
        机器人:layout_alignLeft =@ + ID / btnBattle
        机器人:文本=100
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textViewBattleResults
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / spinnerMonsters
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

    <的TextView
        机器人:ID =@ + ID / textViewStrRace
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textView1
        机器人:layout_toRightOf =@ + ID / textViewClass
        机器人:文本=L
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?


    <的TextView
        机器人:ID =@ + ID / textViewStrAlliance
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textView2
        机器人:layout_alignParentRight =真
        机器人:layout_toRightOf =@ + ID / textViewStrTitle
        机器人:文本=小文
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

    <的TextView
        机器人:ID =@ + ID / textViewAlliance
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / textViewTitle
        机器人:layout_toRightOf =@ + ID /按钮1
        机器人:文本=联盟
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / textView6
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / textViewAlliance
        机器人:layout_below =@ + ID / textViewLineBreak
        机器人:文本=MP
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

< / RelativeLayout的>
 

这是怎么回事?我已经经历了几次了,也找不到什么

编辑:

 包com.example.intent_buttontests;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.Spinner;
进口android.widget.TextView;

公共类播放屏幕延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.play_screen);

        //设置种族,等级,联盟从CreateNew.java选项
        字符串赛= getIntent()getStringExtra(strRace)。
        TextView的textViewStrRace =(TextView中)findViewById(R.id.textViewStrRace);
        textViewStrRace.setText(种族);

        String类= getIntent()getStringExtra(strClass)。
        TextView的textViewStrClass =(TextView中)findViewById(R.id.TextViewStrClass);
        textViewStrClass.setText(类);

        串联= getIntent()getStringExtra(strAlliance);
        TextView的textViewStrAlliance =(TextView中)findViewById(R.id.textViewStrAlliance);
        textViewStrAlliance.setText(联盟);

        按钮btnInventory =(按钮)findViewById(R.id.button1);
        btnInventory.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根

                意向意图=新的意图(v.getContext(),Inventory.class);
                startActivityForResult(意向,0);

            }
        });

        按钮btnEquipment =(按钮)findViewById(R.id.button2);
        btnEquipment.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根

                意向意图=新的意图(v.getContext(),Equipment.class);
                startActivityForResult(意向,0);

            }
        });

        按钮btnStats =(按钮)findViewById(R.id.button3);
        btnStats.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根

                意向意图=新的意图(v.getContext(),Statistics.class);
                startActivityForResult(意向,0);

            }
        });

        按钮btnShop =(按钮)findViewById(R.id.Button01);
        btnShop.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根

                意向意图=新的意图(v.getContext(),Shop.class);
                startActivityForResult(意向,0);

            }
        });

        按钮btnCrafting =(按钮)findViewById(R.id.button4);
        btnCrafting.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根

                意向意图=新的意图(v.getContext(),Crafting.class);
                startActivityForResult(意向,0);

            }
        });

        按钮btnClassTraining =(按钮)findViewById(R.id.button5);
        btnClassTraining.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根

                意向意图=新的意图(v.getContext(),ClassTraining.class);
                startActivityForResult(意向,0);

            }
        });

        按钮btnBattle =(按钮)findViewById(R.id.btnBattle);
        btnBattle.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法
                TextView的textViewBattleResults =(TextView中)findViewById(R.id.textViewBattleResults);
                微调spinnerMonsters =(微调)findViewById(R.id.spinnerMonsters);
                textViewBattleResults.setText(你杀了+ spinnerMonsters.getSelectedItem()的toString()+10经验值!);
            }
        });
    }
}
 

编辑2:

  09-17 18:28:51.485:E / AndroidRuntime(724):致命异常:主要
09-17 18:28:51.485:E / AndroidRuntime(724):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.intent_buttontests / com.example.intent_buttontests.PlayScreen}:java.lang.ClassCastException:机器人.widget.TextView不能转换为android.widget.Button
09-17 18:28:51.485:E / AndroidRuntime(724):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.os.Handler.dispatchMessage(Handler.java:99)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.os.Looper.loop(Looper.java:137)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.app.ActivityThread.main(ActivityThread.java:4424)
09-17 18:28:51.485:E / AndroidRuntime(724):在java.lang.reflect.Method.invokeNative(本机方法)
09-17 18:28:51.485:E / AndroidRuntime(724):在java.lang.reflect.Method.invoke(Method.java:511)
09-17 18:28:51.485:E / AndroidRuntime(724):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
09-17 18:28:51.485:E / AndroidRuntime(724):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-17 18:28:51.485:E / AndroidRuntime(724):在dalvik.system.NativeStart.main(本机方法)
09-17 18:28:51.485:E / AndroidRuntime(724):由:java.lang.ClassCastException:android.widget.TextView不能转换为android.widget.Button
09-17 18:28:51.485:E / AndroidRuntime(724):在com.example.intent_buttontests.PlayScreen.onCreate(PlayScreen.java:110)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.app.Activity.performCreate(Activity.java:4465)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-17 18:28:51.485:E / AndroidRuntime(724):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-17 18:28:51.485:E / AndroidRuntime(724):11 ...更多
 

解决方案
  

我增加了logcat的。但我不知道发生了什么事

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

 致:java.lang.ClassCastException:android.widget.TextView不能转换为android.widget.Button
  在com.example.intent_buttontests.PlayScreen.onCreate(PlayScreen.java:110)
 

您正确读取的错误,这是一个 ClassCastException异常。下面的线条由...引起告诉你,被抛出的错误,这是在 PlayScreen.onCreate()上线110作为最好的,我可以告诉行110:

 按钮btnBattle =(按钮)findViewById(R.id.btnBattle);
 

不过,这条线是罚款和XML的 btnBattle 看起来也没关系......

我跑你的活动与你的布局自己并没有得到任何错误。已清洁你的项目?通常,这将删除这些幽灵的错误。 (在Eclipse中,项目 - > 清理...


我有一个建议,你有很多按钮,完成类似的任务。如果您使用的XML 的onClick 属性,你可以做同样的动作用少得多的code。首先创建一个方法(称之为 launchClick())在你的活动像这样:

 公共无效launchClick(视图v){
    意向意图;

    开关(v.getId()){
    案例R.id.button1:
        意图=新的意图(PlayScreen.this,Inventory.class);
        打破;

    案例R.id.button2:
        意图=新的意图(PlayScreen.this,Equipment.class);
        打破;

    //等等等等
    }
    startActivityForResult(意向,0);
};
 

和添加属性安卓的onClick 来,你应该有这种行为的每一个按钮 play_screen.xml

 <按钮
    机器人:ID =@ + ID /按钮3
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentLeft =真
    机器人:layout_toLeftOf =@ + ID / textViewTitle
    机器人:的onClick =launchClick
    机器人:文本=统计/>
 

希望帮助!

I keep getting a runtime error when launching my activity and it says android.widget.textview cannot be cast to 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>

What is happening here? I've gone through it a few times and can't find anything

edit:

package com.example.intent_buttontests;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class PlayScreen extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_screen);

        // Sets the Race,Class,Alliance options from CreateNew.java
        String Race = getIntent().getStringExtra("strRace");
        TextView textViewStrRace = (TextView) findViewById(R.id.textViewStrRace);
        textViewStrRace.setText(Race);

        String Class = getIntent().getStringExtra("strClass");
        TextView textViewStrClass = (TextView) findViewById(R.id.TextViewStrClass);
        textViewStrClass.setText(Class);

        String Alliance = getIntent().getStringExtra("strAlliance");
        TextView textViewStrAlliance = (TextView) findViewById(R.id.textViewStrAlliance);
        textViewStrAlliance.setText(Alliance);

        Button btnInventory = (Button) findViewById(R.id.button1);
        btnInventory.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(v.getContext(), Inventory.class);
                startActivityForResult(intent, 0);

            }
        });

        Button btnEquipment = (Button) findViewById(R.id.button2);
        btnEquipment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(v.getContext(), Equipment.class);
                startActivityForResult(intent, 0);

            }
        });

        Button btnStats = (Button) findViewById(R.id.button3);
        btnStats.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(v.getContext(), Statistics.class);
                startActivityForResult(intent, 0);

            }
        });

        Button btnShop = (Button) findViewById(R.id.Button01);
        btnShop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(v.getContext(), Shop.class);
                startActivityForResult(intent, 0);

            }
        });

        Button btnCrafting = (Button) findViewById(R.id.button4);
        btnCrafting.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(v.getContext(), Crafting.class);
                startActivityForResult(intent, 0);

            }
        });

        Button btnClassTraining = (Button) findViewById(R.id.button5);
        btnClassTraining.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(v.getContext(), ClassTraining.class);
                startActivityForResult(intent, 0);

            }
        });

        Button btnBattle = (Button) findViewById(R.id.btnBattle);
        btnBattle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method
                TextView textViewBattleResults = (TextView) findViewById(R.id.textViewBattleResults);
                Spinner spinnerMonsters = (Spinner) findViewById(R.id.spinnerMonsters);
                textViewBattleResults.setText("You have killed "+ spinnerMonsters.getSelectedItem().toString() + " for 10 experience points!");
            }
        });
    }
}

Edit 2:

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

This is the important information in your LogCat file:

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)

You read the error correctly, it is a ClassCastException. The lines below Caused by... tell you where the error was thrown, which is in PlayScreen.onCreate() on line 110. As best I can tell line 110 is:

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

But this line is fine and the XML for btnBattle looks fine too...

I ran your Activity with your layout myself and didn't get any errors. Have you cleaned your project? Often this will remove these "phantom" errors. (In Eclipse, Project -> Clean...)


I do have one suggestion, you have a lot of Buttons that do similar tasks. You can do the same actions with much less code if you use the XML onClick attribute. First create a method (call it launchClick()) in your Activity like so:

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);
};

And add the attribute android:onClick to every Button that you should have this behavior in play_screen.xml:

<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" />

Hope that helps!

这篇关于Android.widget,TextView的不能转换为android.widget,按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆