我是android开发的新手.我想要实现的是当用户点击图像按钮时,它会启动一个新活动,这是一个带有新xml布局的新类.一切都编译好没有错误但是当我在我的模拟器中启动它(在实现这个之前工作)应用程序停止.我试图再次编码,但仍然遇到同样的问题.我错过了什么 这是我所涉及的所有五个文件的代码.
MainMenu类
package com.example.james.card_app; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; public class MainMenu extends AppCompatActivity { private static Button button_sbm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_menu); onClickButtonListener(); } public void onClickButtonListener(){ Button btn = (Button)findViewById(R.id.playbutton); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainMenu.this, MenuSearch.class)); } }); } }
SearchMenu类
package com.example.james.card_app; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MenuSearch extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); } }
Android Manifest xml
MainMenu xml
搜索菜单xml
Logcat错误
01-05 10:57:57.928 4316-4316/? D/dalvikvm: Not late-enabling CheckJNI (already on) 01-05 10:57:57.998 4316-4316/com.example.james.card_app E/Trace: error opening trace file: No such file or directory (2) 01-05 10:57:58.038 4316-4316/com.example.james.card_app W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 01-05 10:57:58.038 4316-4316/com.example.james.card_app I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested 01-05 10:57:58.038 4316-4316/com.example.james.card_app W/dalvikvm: VFY: unable to resolve interface method 17897: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 01-05 10:57:58.038 4316-4316/com.example.james.card_app D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 01-05 10:57:58.038 4316-4316/com.example.james.card_app I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode 01-05 10:57:58.038 4316-4316/com.example.james.card_app W/dalvikvm: VFY: unable to resolve interface method 17901: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 01-05 10:57:58.038 4316-4316/com.example.james.card_app D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 01-05 10:57:58.178 4316-4316/com.example.james.card_app I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged 01-05 10:57:58.178 4316-4316/com.example.james.card_app W/dalvikvm: VFY: unable to resolve virtual method 17793: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V 01-05 10:57:58.188 4316-4316/com.example.james.card_app D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007 01-05 10:57:58.188 4316-4316/com.example.james.card_app I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations 01-05 10:57:58.188 4316-4316/com.example.james.card_app W/dalvikvm: VFY: unable to resolve virtual method 422: Landroid/content/res/TypedArray;.getChangingConfigurations ()I 01-05 10:57:58.188 4316-4316/com.example.james.card_app D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 01-05 10:57:58.188 4316-4316/com.example.james.card_app I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType 01-05 10:57:58.188 4316-4316/com.example.james.card_app W/dalvikvm: VFY: unable to resolve virtual method 444: Landroid/content/res/TypedArray;.getType (I)I 01-05 10:57:58.188 4316-4316/com.example.james.card_app D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 01-05 10:57:58.248 4316-4321/com.example.james.card_app D/dalvikvm: GC_CONCURRENT freed 171K, 2% free 18879K/19207K, paused 19ms+0ms, total 24ms 01-05 10:57:58.258 4316-4316/com.example.james.card_app D/dalvikvm: GC_FOR_ALLOC freed 16K, 2% free 18928K/19271K, paused 2ms, total 3ms 01-05 10:57:58.258 4316-4316/com.example.james.card_app I/dalvikvm-heap: Grow heap (frag case) to 19.043MB for 429168-byte allocation 01-05 10:57:58.268 4316-4316/com.example.james.card_app D/dalvikvm: GC_FOR_ALLOC freed <1K, 2% free 19347K/19719K, paused 9ms, total 9ms 01-05 10:57:58.278 4316-4316/com.example.james.card_app D/dalvikvm: GC_FOR_ALLOC freed 48K, 3% free 19302K/19719K, paused 2ms, total 2ms 01-05 10:57:58.278 4316-4316/com.example.james.card_app I/dalvikvm-heap: Grow heap (frag case) to 19.675MB for 708972-byte allocation 01-05 10:57:58.298 4316-4321/com.example.james.card_app D/dalvikvm: GC_CONCURRENT freed <1K, 3% free 19994K/20423K, paused 12ms+0ms, total 14ms 01-05 10:57:58.298 4316-4316/com.example.james.card_app D/dalvikvm: GC_FOR_ALLOC freed 0K, 3% free 19995K/20423K, paused 3ms, total 3ms 01-05 10:57:58.308 4316-4316/com.example.james.card_app I/dalvikvm-heap: Grow heap (frag case) to 25.760MB for 6380652-byte allocation 01-05 10:57:58.328 4316-4321/com.example.james.card_app D/dalvikvm: GC_CONCURRENT freed 0K, 2% free 26226K/26695K, paused 14ms+1ms, total 18ms 01-05 10:57:58.388 4316-4316/com.example.james.card_app D/AndroidRuntime: Shutting down VM 01-05 10:57:58.388 4316-4316/com.example.james.card_app W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb2fa8288) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: FATAL EXCEPTION: main 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.james.card_app/com.example.james.card_app.MainMenu}: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:130) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4745) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at com.example.james.card_app.MainMenu.onClickButtonListener(MainMenu.java:20) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at com.example.james.card_app.MainMenu.onCreate(MainMenu.java:16) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5008) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:130) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4745) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-05 10:57:58.398 4316-4316/com.example.james.card_app E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 01-05 10:58:01.188 4316-4316/? I/Process: Sending signal. PID: 4316 SIG: 9
Evgeniy Mish.. 5
这条线
Button btn = (Button)findViewById(R.id.playbutton);
应该:
ImageButton btn = (ImageButton)findViewById(R.id.playbutton);
此外,当您使用显式Intent
启动时Activity
,您可以删除
从清单中,因为你没有为Intent
你开始添加所需的动作.
这条线
Button btn = (Button)findViewById(R.id.playbutton);
应该:
ImageButton btn = (ImageButton)findViewById(R.id.playbutton);
此外,当您使用显式Intent
启动时Activity
,您可以删除
从清单中,因为你没有为Intent
你开始添加所需的动作.