这是LogCat,希望它有所帮助:
> 01-15 04:10:50.154: E/AndroidRuntime(369): FATAL EXCEPTION: main
> 01-15 04:10:50.154: E/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.guzi.uyproductions/com.guzi.uyproductions.MainActivity}:
> java.lang.ClassCastException: android.widget.ImageButton
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.os.Handler.dispatchMessage(Handler.java:99)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.os.Looper.loop(Looper.java:130)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.app.ActivityThread.main(ActivityThread.java:3683)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at java.lang.reflect.Method.invokeNative(Native Method)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at java.lang.reflect.Method.invoke(Method.java:507)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at dalvik.system.NativeStart.main(Native Method)
> 01-15 04:10:50.154: E/AndroidRuntime(369): Caused by: java.lang.ClassCastException: android.widget.ImageButton
> 01-15 04:10:50.154: E/AndroidRuntime(369): at com.guzi.uyproductions.MainActivity.onCreate(MainActivity.java:21)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 01-15 04:10:50.154: E/AndroidRuntime(369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
> 01-15 04:10:50.154: E/AndroidRuntime(369): ... 11 more
> 01-15 04:10:52.874: I/Process(369): Sending signal. PID: 369 SIG: 9
这是.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.imageButton2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent viewIntent =
new Intent("android.intent.action.VIEW",
Uri.parse("https://www.google.com"));
startActivity(viewIntent);
}
});
如果您需要更多详情,请与我们联系
答案 0 :(得分:3)
你好删除这一行Button button = (Button)findViewById(R.id.imageButton2);
并使用
ImageButton button = (ImageButton )findViewById(R.id.imageButton2);
您需要使用ImageButton
,但使用的是Button
答案 1 :(得分:2)
正如Jitesh已经解释过的那样,我只想补充一点,以消除一些混淆。
从ImageButton的名称我们可能认为它是Button类的子类(就像textview是View类)。这就是误解可能导致这个问题的原因。我们知道我们可以将子类强制转换为它的超类。
但是ImageButton不是Button类的子类(既不是直接也不是间接)所以你不能像你那样进行转换。这里发生了ClassCastException。