我的代码存在问题。我是发展新手。我试图用一个按钮从我的一个活动转移到另一个活动。请找到以下代码。
我一直收到以下错误:
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
Home 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" />
答案 0 :(得分:4)
你的logcat说你做错了类型转换。你试图将一个按钮转换成图像按钮:
Button _button = (ImageButton) findViewById(R.id.yourbuttonid);
Button _button = (Button) findViewById(R.id.yourbuttonid);
答案 1 :(得分:0)
通过查看您的logcat,我确信您正在尝试从您在xml文件中定义的Button
转换ImageButton
。
您可以在xml文件中将ImageButton
更改为Button
,反之亦然。
答案 2 :(得分:0)
问题似乎是您在视图中使用ImageButton(在各自的视图中使用xml),但在相应的java类中将其定义为Button,请执行以下操作,并解决您的问题。 :)
如果你想使用ImageButton将其定义为
ImageButton yourButton = (ImageButton) findViewById(R.id.imageButtonId);
如果你想使用Button将其定义为
Button yourButton = (Button) findViewById(R.id.buttonId);