尝试从imagebutton启动新活动,但我的应用程序停止在模拟器中工作 [英] Trying to launch a new activity from imagebutton but my app stops working in emulator

查看:56
本文介绍了尝试从imagebutton启动新活动,但我的应用程序停止在模拟器中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手.我要实现的目标是,当用户单击图像按钮时,它将启动一个新活动,该活动是具有新xml布局的新类.一切都可以正常编译,没有错误,但是当我在模拟器中启动该程序(在实现此功能之前曾工作过)时,该应用程序停止运行.我尝试再次编写所有代码,但仍然遇到相同的问题.我想念的是什么.这是我涉及的所有五个文件的代码.

I am new to android development. What I'm trying to achieve is when a user clicks an image button it launches a new activity which is a new class with a new xml layout. Everything compiles fine with no errors however when I launch it in my emulator (Which worked before implementing this) the app stops. I have tried to code it all again but am still getting the same problem. What am I missing. Here is my code for all five files involved.

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类

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清单xml

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

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

推荐答案

此行

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

应为:

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

此外,由于您使用显式的 Intent 来启动 Activity ,因此您可以删除

Also as you are using explicit Intent to start your Activity, you can delete

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

从清单中开始,因为您没有将所需的操作添加到 Intent 中,所以就开始了.

from the manifest, since you don't add needed action to the Intent you start.

这篇关于尝试从imagebutton启动新活动,但我的应用程序停止在模拟器中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆