OnCreate强制活动崩溃:ImageButton ClassCastException

时间:2014-03-20 01:46:52

标签: java android eclipse

我正在尝试定义按钮,类似于setOnClickListener。我在OnCreate方法中这样做,当我在Android模拟器上运行我的代码时,它会抛出错误。

package com.example.yyy;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;

import android.content.pm.ActivityInfo;
import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;

import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.Button;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity implements OnClickListener{

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

        Button start = (Button)findViewById(R.id.start);
        start.setOnClickListener(this);
        Button hs =(Button)findViewById(R.id.hs);
        hs.setOnClickListener(this);        

    }       

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


        @Override
        public void onClick(View v)
        {
            switch(v.getId())
            {
            case R.id.start:
                Intent find = new Intent(this, Find.class);
                startActivity(find);
                break;
            case R.id.hs:
                Toast t = Toast.makeText(this, "w8", Toast.LENGTH_SHORT);
                t.show();
                break;
                default: break;         
        }
    }

}

这是此应用程序的LogCat。

03-19 21:53:13.063: D/dalvikvm(1242): GC_FOR_ALLOC freed 67K, 5% free 3046K/3188K, paused 80ms, total 84ms
03-19 21:53:13.103: I/dalvikvm-heap(1242): Grow heap (frag case) to 4.937MB for 1987216-byte allocation
03-19 21:53:13.233: D/dalvikvm(1242): GC_FOR_ALLOC freed 2K, 3% free 4984K/5132K, paused 127ms, total 127ms
03-19 21:53:14.993: D/AndroidRuntime(1242): Shutting down VM
03-19 21:53:14.993: W/dalvikvm(1242): threadid=1: thread exiting with uncaught exception (group=0xb1a63ba8)
03-19 21:53:15.003: E/AndroidRuntime(1242): FATAL EXCEPTION: main
03-19 21:53:15.003: E/AndroidRuntime(1242): Process: com.example.yyy, PID: 1242
03-19 21:53:15.003: E/AndroidRuntime(1242): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yyy/com.example.yyy.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.os.Looper.loop(Looper.java:136)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at java.lang.reflect.Method.invoke(Method.java:515)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at dalvik.system.NativeStart.main(Native Method)
03-19 21:53:15.003: E/AndroidRuntime(1242): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
03-19 21:53:15.003: E/AndroidRuntime(1242):     at com.example.yyy.MainActivity.onCreate(MainActivity.java:28)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.app.Activity.performCreate(Activity.java:5231)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-19 21:53:15.003: E/AndroidRuntime(1242):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-19 21:53:15.003: E/AndroidRuntime(1242):     ... 11 more
03-19 21:53:25.683: I/Process(1242): Sending signal. PID: 1242 SIG: 9

这是主要布局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context="com.example.yyy.MainActivity"
    tools:ignore="MergeRootFrame" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/cv" 
            />

        <ImageButton
            android:id="@+id/hs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/imageView1"
            android:layout_marginRight="19dp"
            android:src="@drawable/bt_hs_3d"
           />

        <ImageButton
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="48dp"
            android:layout_toLeftOf="@+id/hs"
            android:src="@drawable/bt_st_3d"
            />

    </RelativeLayout>

</FrameLayout>

3 个答案:

答案 0 :(得分:1)

logcat中的这一行告诉您问题所在:

03-19 21:53:15.003: E/AndroidRuntime(1242): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button

您已在某处的XML中定义了ImageButton,然后在onCreate方法中尝试将其转换为标准Button

    Button start = (Button)findViewById(R.id.start);
    start.setOnClickListener(this); 

将您的代码更改为:

    ImageButton start = (ImageButton)findViewById(R.id.start);
    start.setOnClickListener(this); 

答案 1 :(得分:1)

您正在将图像按钮投射到按钮,这就是它抛出此异常的原因。更改

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

ImageButton start = (ImageButton)findViewById(R.id.start);

答案 2 :(得分:0)

错误很明显:

03-19 21:53:15.003:E / AndroidRuntime(1242):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.yyy / com.example.yyy.MainActivity}:java.lang。 ClassCastException:android.widget.ImageButton无法强制转换为android.widget.Button

findViewById(R.id.start),返回ImageButton,你将其转换为Button,从而导致崩溃。

按钮开始=(按钮)findViewById(R.id.start);

我建议2个选项:

1)将OnCreate方法中的Button替换为ImageButton。

阅读以下示例,已经很好地解释了:

[http://www.mkyong.com/android/android-imagebutton-example/] [1]

2)将android:onClick属性添加到如下...

<ImageButton
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="48dp"
android:layout_toLeftOf="@+id/hs"
android:onClick="onStartAction"
android:src="@drawable/bt_st_3d"/>

你需要实施

public void onStartAction(View v) {
  //DO Some thing here....
}