我使用此代码在按下图像按钮时启动 Activity
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickListener listnr=new OnClickListener() {
@Override
public void onClick(View v) {
Intent i= new Intent(MainActivity.this, FlashActivity.class);
startActivity(i);
}
};
Button imageButton1 =(Button) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(listnr);
}
}
它崩溃了谁能告诉我这段代码中的错误
错误 12-04 14:30:02.506: E/AndroidRuntime (31514): 致命异常: 主要 12-04 14:30:02.506: E/AndroidRuntime(31514): java.lang.RuntimeException: 无法启动 Activity ComponentInfo{com.app.flash/com.app.flash.MainActivity}: java.lang.ClassCastException: android .widget.ImageButton 无法转换为 android.widget.Button 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2185) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2210) 12-04 14:30:02.506: E/AndroidRuntime(31514): 在 android.app.ActivityThread.access$600(ActivityThread.java:142) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 android.app.ActivityThread$H.handleMessage (ActivityThread.java:1208) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 android.os.Handler.dispatchMessage (Handler.java:99) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 android.os.Looper.loop (Looper.java:137) 12-04 14:30:02.506: E/AndroidRuntime(31514): 在 android.app.ActivityThread.main(ActivityThread.java:4931) 12-04 14:30:02.506:E/AndroidRuntime(31514):在 java.lang.reflect.Method.invokeNative( native 方法) 12-04 14:30:02.506: E/AndroidRuntime(31514): 在 java.lang.reflect.Method.invoke(Method.java:511) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:791) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:558) 12-04 14:30:02.506:E/AndroidRuntime(31514):在 dalvik.system.NativeStart.main( native 方法) 12-04 14:30:02.506: E/AndroidRuntime(31514): Caused by: java.lang.ClassCastException: android.widget.ImageButton 无法转换为 android.widget.Button 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 com.app.flash.MainActivity.onCreate (MainActivity.java:17) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 android.app.Activity.performCreate (Activity.java:5008) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1079) 12-04 14:30:02.506: E/AndroidRuntime (31514): 在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2139) 12-04 14:30:02.506: E/AndroidRuntime(31514): ... 11 更多
最佳答案
使用它会解决你的问题
Intent i= new Intent(CurrentActivity.this, UpcomingActivity.class);
startActivity(i);
不要忘记在 list 文件中定义您的类。
例如:
Button imageButton1 =(Button) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i= new Intent(CurrentActivity.this, UpcomingActivity.class);
startActivity(i);
}
});
确保在 xml 文件中包含按钮而不是 imageButton。如果 xml 文件中的图像按钮,那么您需要如下所述:
ImageButton imageButton1 =(ImageButton) findViewById(R.id.imageButton1);
关于android按钮启动 Activity 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374359/