“无法启动活动组件信息”droid force close问题

时间:2013-01-16 14:07:56

标签: java android xml

这是类

的java代码
package com.warriorsoft.mtgupanddown;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {
    int guy1;
    int guy2;
    MediaPlayer sound;
    Button plus1g1, plus1g2, min1g1, min1g2, plus5g1, plus5g2, min5g1, min5g2,
            GG, RESET;
    TextView g1, g2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);

        guy1 = 20;
        guy2 = 20;
        min5g1 = (Button) findViewById(R.id.button1); // x
        plus5g1 = (Button) findViewById(R.id.button2); //x
        plus1g1 = (Button) findViewById(R.id.button3); // x
        min1g1 = (Button) findViewById(R.id.button4); // x
        min5g2 = (Button) findViewById(R.id.button5); // x
        plus5g2 = (Button) findViewById(R.id.button6); //x
        min1g2 = (Button) findViewById(R.id.button7); // x
        plus1g2 = (Button) findViewById(R.id.button8); //x
        RESET = (Button) findViewById(R.id.RESET);
        GG = (Button) findViewById(R.id.GG);
        g1 = (TextView) findViewById(R.id.tvPlayer1HP);
        g2 = (TextView) findViewById(R.id.tvPlayer2HP);

        min5g1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                guy1 -= 5;
                g1.setText(guy1);

            }
        });
        min5g2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                guy2 -= 5;
                g2.setText(guy2);
            }
        });
        min1g1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                guy1 -= 1;
                g1.setText(guy1);
            }
        });
        min1g2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                guy2 -= 1;
                g2.setText(guy2);
            }
        });
        plus5g2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                guy2 += 5;
                g2.setText(guy2);
            }
        });
        plus5g1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                guy1 += 5;
                g1.setText(guy1);
            }
        });
        plus1g1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                guy1 += 1;
                g1.setText(guy1);
            }
        });
        plus1g2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                guy2 += 1;
                g2.setText(guy2);
            }
        });

        RESET.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0){
            guy1 = 20;
            guy2 = 20;

            g2.setText(guy2);
            g1.setText(guy1);
            }
        });
        GG.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0){
            sound = MediaPlayer.create(MainActivity.this, R.raw.gg );
            }
        });

    }

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

    }

}

这是布局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="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tvPlayer2HP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="36dp"
        android:text="20"
        android:textSize="35dp" />

    <TextView
        android:id="@+id/tvPlayer1HP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="36dp"
        android:text="20"
        android:textSize="35dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@+id/button1"
        android:text="+5" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_alignLeft="@+id/button2"
        android:text="-1" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignRight="@+id/button1"
        android:layout_marginBottom="28dp"
        android:text="+1" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button5"
        android:layout_alignBottom="@+id/button5"
        android:layout_alignParentRight="true"
        android:text="+5" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_marginRight="14dp"
        android:layout_toLeftOf="@+id/button6"
        android:text="-5" />

    <Button
        android:id="@+id/button7"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button5"
        android:layout_alignTop="@+id/button4"
        android:text="+1" />

    <Button
        android:id="@+id/button8"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button7"
        android:layout_alignBottom="@+id/button7"
        android:layout_toRightOf="@+id/button5"
        android:text="-1" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignLeft="@+id/button5"
        android:ems="10"
        android:hint="Guy 1" >
    </EditText>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/button2"
        android:layout_below="@+id/button1"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:hint="Guy 2" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tvPlayer1HP"
        android:layout_marginTop="74dp"
        android:text="-5" />

    <ImageView
        android:id="@+id/GG"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvPlayer2HP"
        android:layout_centerHorizontal="true"
        android:minHeight="140dp"
        android:minWidth="120dp"
        android:src="@drawable/gg" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:text="Optimal sideways"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/RESET"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="RESET" />

</RelativeLayout>

这是logcat

01-16 14:01:51.292: W/dalvikvm(699): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-16 14:01:51.302: E/AndroidRuntime(699): FATAL EXCEPTION: main
01-16 14:01:51.302: E/AndroidRuntime(699): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.warriorsoft.mtgupanddown/com.warriorsoft.mtgupanddown.MainActivity}: java.lang.ClassCastException: android.widget.ImageView
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.os.Looper.loop(Looper.java:123)
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-16 14:01:51.302: E/AndroidRuntime(699):  at java.lang.reflect.Method.invokeNative(Native Method)
01-16 14:01:51.302: E/AndroidRuntime(699):  at java.lang.reflect.Method.invoke(Method.java:507)
01-16 14:01:51.302: E/AndroidRuntime(699):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-16 14:01:51.302: E/AndroidRuntime(699):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-16 14:01:51.302: E/AndroidRuntime(699):  at dalvik.system.NativeStart.main(Native Method)
01-16 14:01:51.302: E/AndroidRuntime(699): Caused by: java.lang.ClassCastException: android.widget.ImageView
01-16 14:01:51.302: E/AndroidRuntime(699):  at com.warriorsoft.mtgupanddown.MainActivity.onCreate(MainActivity.java:33)
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 14:01:51.302: E/AndroidRuntime(699):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-16 14:01:51.302: E/AndroidRuntime(699):  ... 11 more
01-16 14:01:53.712: I/Process(699): Sending signal. PID: 699 SIG: 9

问题是在手机上运行代码。 它告诉我该程序以一些意想不到的方式破坏并迫使我强行关闭。 如果有人能帮助我理解我做错了什么我会永远感激 我还是编程新手,几乎无法理解这些日志 请放轻松 谢谢你的尝试

2 个答案:

答案 0 :(得分:0)

您正在找到ImageView并将其投射到按钮。

检查这行代码。

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

将GG的类型更改为ImageView,并将类型转换为ImageView,如下所示:

ImageView GG;

GG = (ImageView) findViewById(R.id.GG);

答案 1 :(得分:0)

将此更改为此

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

GG = (ImageView) findViewById(R.id.GG);

您无法将ImageView强制转换为Button。这是你在做什么