eclipse java中的AndroidRuntime错误

时间:2013-01-03 16:29:01

标签: android eclipse runtime-error classcastexception

我正在创建一个应用程序,在运行它时会显示大量错误。

以下是主要活动的代码:

package alex.greene.snookerscorer;

import android.os.Bundle;


import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity{
Button Cont;
EditText player1name;
EditText player2name;

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

protected void initialize() {
    player1name = (EditText) findViewById(R.id.etName1);
    player2name = (EditText) findViewById(R.id.etName2);
    Cont = (Button) findViewById(R.id.bContinue);
    Cont.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v){
                String player1namesend = player1name.getText().toString();
                String player2namesend = player2name.getText().toString();
                Bundle sendnames = new Bundle();
                sendnames.putString("key", player1namesend);
                sendnames.putString("key2", player2namesend);
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, Scorer.class);
                intent.putExtras(sendnames);
                startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

以下是第二节课(得分手)的代码:

package alex.greene.snookerscorer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Scorer extends Activity {

TextView player1;
TextView player2;
TextView Score1;
TextView Score2;
String gotName1;
String gotName2;
Button red;
Button white;
Button yellow;
Button green;
Button brown;
Button blue;
Button pink;
Button black;
int counter;

public void initialize() {
    player1 = (TextView) findViewById(R.id.Player1);
    player2 = (TextView) findViewById(R.id.Player2);
    Score1 = (TextView) findViewById(R.id.Player1Score);
    Score2 = (TextView) findViewById(R.id.Player2Score);
    red = (Button) findViewById(R.id.btnRed);
    yellow = (Button) findViewById(R.id.btnYellow);
    white = (Button) findViewById(R.id.btnWhite);
    green = (Button) findViewById(R.id.btnGreen);
    brown = (Button) findViewById(R.id.btnBrown);
    blue = (Button) findViewById(R.id.btnBlue);
    pink = (Button) findViewById(R.id.btnPink);
    black = (Button) findViewById(R.id.btnBlack);
    counter = 0;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scorer);
    initialize();       
    Bundle receivenames = getIntent().getExtras();  
    gotName1 = receivenames.getString("key");
    gotName2 = receivenames.getString("key2");
    player1.setText(gotName1);
    player2.setText(gotName2);
    finish();

    red.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            Score1.setText("" + counter);               
        }
    });

    yellow.setOnClickListener(new View.OnClickListener() {          
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter += 2;
            Score1.setText(Score1 + "" + counter);
        }
    });

    green.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter += 3;
            Score1.setText("" + counter);
        }
    });

    brown.setOnClickListener(new View.OnClickListener() {           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter += 4;
            Score1.setText("" + counter);
        }
    });

    blue.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter += 5;
            Score1.setText("" + counter);
        }
    });

    pink.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter += 6;
            Score1.setText("" + counter);
        }
    });

    black.setOnClickListener(new View.OnClickListener() {           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter += 7;
            Score1.setText("" + counter);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_scorer, menu);
    return true;
}

这是logcat:

01-03 16:21:39.973: E/AndroidRuntime(1122): FATAL EXCEPTION: main
01-03 16:21:39.973: E/AndroidRuntime(1122): java.lang.RuntimeException: Unable to start         activity ComponentInfo{alex.greene.snookerscorer/alex.greene.snookerscorer.Scorer}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button    
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.os.Looper.loop(Looper.java:137)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at dalvik.system.NativeStart.main(Native Method)
01-03 16:21:39.973: E/AndroidRuntime(1122): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button
01-03 16:21:39.973: E/AndroidRuntime(1122):     at alex.greene.snookerscorer.Scorer.initialize(Scorer.java:38)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at alex.greene.snookerscorer.Scorer.onCreate(Scorer.java:48)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.app.Activity.performCreate(Activity.java:4465)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-03 16:21:39.973: E/AndroidRuntime(1122):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-03 16:21:39.973: E/AndroidRuntime(1122):     ... 11 more    

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

检查您的blue = (Button) findViewById(R.id.btnBlue);行。这是给你一个错误的那个。

Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button

隐含使用ID为R.id.btnBlue定义的视图为RelativeLayout,而您尝试将其视为Button(使用显式转换)。