我的代码有问题。我是开发新手。我试图使用一个按钮从我的一项活动转到另一项活动。请在下面找到代码。
我不断收到以下错误:
06-03 15:25:45.117: W/dalvikvm(2410): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-03 15:25:45.127: E/AndroidRuntime(2410): FATAL EXCEPTION: main
06-03 15:25:45.127: E/AndroidRuntime(2410): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ktacknowledge.mysweetmessages/com.ktacknowledge.mysweetmessages.Home}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.os.Looper.loop(Looper.java:137)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-03 15:25:45.127: E/AndroidRuntime(2410): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 15:25:45.127: E/AndroidRuntime(2410): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 15:25:45.127: E/AndroidRuntime(2410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-03 15:25:45.127: E/AndroidRuntime(2410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-03 15:25:45.127: E/AndroidRuntime(2410): at dalvik.system.NativeStart.main(Native Method)
06-03 15:25:45.127: E/AndroidRuntime(2410): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
06-03 15:25:45.127: E/AndroidRuntime(2410): at com.ktacknowledge.mysweetmessages.Home.onCreate(Home.java:19)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.app.Activity.performCreate(Activity.java:5008)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-03 15:25:45.127: E/AndroidRuntime(2410): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-03 15:25:45.127: E/AndroidRuntime(2410): ... 11 more
本地Java
package com.ktacknowledge.mysweetmessages;
import android.os.Bundle;
import android.widget.ImageButton;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
public class Home extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ImageButton yourButton = (ImageButton) findViewById(R.id.imageButton1);
switchImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Home.this, SweetMessage.class);
startActivity(i);
}
});
}
活动2
package com.ktacknowledge.mysweetmessages;
import android.os.Bundle;
import android.app.Activity;
public class SweetMessage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sweet_message);
}
}
首页xml
<RelativeLayout 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"
tools:context="com.ktacknowledge.mysweetmessages.Home"
tools:ignore="MergeRootFrame" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="122dp"
android:layout_toLeftOf="@+id/imageButton4"
android:src="@drawable/ic_message" />
问题似乎是您在视图(在各自视图的xml中)中使用ImageButton,但在相应的Java类中将其定义为Button,请按照以下步骤进行操作,问题将得到解决。:)
如果您想使用ImageButton定义为
ImageButton yourButton = (ImageButton) findViewById(R.id.imageButtonId);
如果您想使用Button将其定义为
Button yourButton = (Button) findViewById(R.id.buttonId);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句