Java OnCreate forces活动崩溃:ImageButton ClassCastException

Java OnCreate forces活动崩溃:ImageButton ClassCastException,java,android,eclipse,Java,Android,Eclipse,我试图定义按钮,比如setOnClickListener。我在OnCreate方法中这样做,当我在android emulator上运行代码时,它会抛出错误 package com.example.yyy; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.

我试图定义按钮,比如setOnClickListener。我在OnCreate方法中这样做,当我在android emulator上运行代码时,它会抛出错误

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>

日志猫中的这一行告诉您问题所在:

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 start = (Button)findViewById(R.id.start);
    start.setOnClickListener(this); 
将代码更改为:

    ImageButton start = (ImageButton)findViewById(R.id.start);
    start.setOnClickListener(this); 
错误显而易见:

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

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

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

我建议两种选择:

1) 将OnCreate方法中的按钮替换为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"/>

您正在将imagebutton强制转换为button,这就是它引发此异常的原因。更改

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


显示布局XML和堆栈跟踪。请发布logcat错误。虽然我假设其中一个按钮没有定义并抛出一个空指针,但我已经发布了logcat和布局XML。谢谢你的帮助。
Button start = (Button)findViewById(R.id.start);
ImageButton start = (ImageButton)findViewById(R.id.start);