尝试从imagebutton启动新活动,但我的应用程序停止在模拟器中工作

时间:2016-01-05 10:53:18

标签: java android xml android-layout

我是Android开发的新手。我想要实现的是当用户点击图像按钮时,它会启动一个新活动,这是一个带有新xml布局的新类。一切都编译好没有错误但是当我在我的模拟器中启动它(在实现这个之前工作)应用程序停止。我试图再次编码,但仍然遇到同样的问题。我错过了什么这是我所涉及的所有五个文件的代码。

MainMenu Class

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 Class

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          

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainMenu" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MenuSearch" >
            <intent-filter>
                <action android:name="com.example.james.card_app.MenuSearch" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainMenu xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainMenu"
    android:background="#1A6600">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/AppTitle"
        android:textColor="#FFFFFF"
        android:textSize="60dp"
        android:id="@+id/textView"
        android:layout_alignTop="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="22dp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/playbutton"
        android:src="@drawable/playbutton"
        android:layout_marginRight="120dp"
        android:layout_marginEnd="120dp"
        android:background="@null"
        android:layout_above="@+id/imageButton2"
        android:layout_toLeftOf="@+id/imageView"
        android:layout_toStartOf="@+id/imageView" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/applogo"
        android:paddingBottom="200dp"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:contentDescription="logoimage" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton2"
        android:src="@drawable/settings"
        android:contentDescription="@string/settingsbutton"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/imageView"
        android:layout_alignEnd="@+id/imageView"
        android:background="@null"/>
</RelativeLayout>

搜索菜单xml          

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/settingtitle"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="121dp"
        android:layout_marginStart="121dp"
        android:layout_marginTop="24dp" />
</RelativeLayout>

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

3 个答案:

答案 0 :(得分:5)

这一行

 Button btn = (Button)findViewById(R.id.playbutton);

应该是:

 ImageButton btn = (ImageButton)findViewById(R.id.playbutton);

另外,当您使用明确的Intent来启动Activity时,您可以删除

<intent-filter>
     <action android:name="com.example.james.card_app.MenuSearch" />    
     <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
来自清单的

,因为您没有向开始的Intent添加所需的操作。

答案 1 :(得分:2)

只需将Button替换为ImageButton

即可
Button btn = (Button)findViewById(R.id.playbutton);

这一行

ImageButton btn = (ImageButton)findViewById(R.id.playbutton);

答案 2 :(得分:-1)

这是问题所在:

android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button

将按钮更改为AppCompatImageButton:)