无法执行Android活动的方法

前端之家收集整理的这篇文章主要介绍了无法执行Android活动的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下按钮:
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_create_user"
    android:id="@+id/btn_create_user"
    android:onClick="onClickCreateUser"

    android:layout_alignParentBottom="true"
    android:background="@color/btn_create_user"
    android:layout_alignLeft="@+id/btn_login"
    android:layout_alignStart="@+id/btn_login"
    android:layout_marginBottom="23dp"
    android:layout_alignRight="@+id/btn_login"
    android:layout_alignEnd="@+id/btn_login" />

具有以下onclick功能

public void onClickCreateUser(View v){
    Intent i = new Intent("dk.anapp.noteBox.activity_createUser");
    startActivity(i);
}

当我按下按钮时,我收到以下错误

02-12 18:20:53.526    1598-1598/dk.anapp.noteBox E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3044)
        at android.view.View.performClick(View.java:3511)
        at android.view.View$PerformClick.run(View.java:14105)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3039)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=dk.anapp.noteBox.activity_createUser }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1512)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
        at android.app.Activity.startActivityForResult(Activity.java:3190)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
        at android.app.Activity.startActivity(Activity.java:3297)
        at dk.anapp.noteBox.Login.onClickCreateUser(Login.java:74)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3039)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

搜索了SO,但我能找到的唯一答案是那些忘记在清单中宣布课程的人

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name="dk.anapp.noteBox.Mediator"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="dk.anapp.noteBox.Login"
        android:label="@string/title_activity_login" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="dk.anapp.noteBox.SoloActivity"
        android:label="@string/title_activity_solo" >
        <intent-filter>
            <action android:name="dk.anapp.noteBox.SoloActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="dk.anapp.noteBox.SpashScreen"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_spash_screen"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="dk.anapp.noteBox.SpashScreen"
        android:label="@string/title_activity_spash_screen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="dk.anapp.noteBox.MyGroups"
        android:label="@string/title_activity_my_groups" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="dk.anapp.noteBox.activity_createUser"
        android:label="@string/title_activity_activity_create_user" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

那我做错了什么?

解决方法

在你的清单中就行了
<activity android:name="dk.anapp.noteBox.activity_createUser" />

你应该使用这个< intent-filter>属性只有一次,以指定在启动时运行的Activity

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

此外,将您的代码更改为

public void onClickCreateUser(View v){
    Intent i = new Intent(this,Activity_createUser.class);
    startActivity(i);
}

我假设您的活动类名为Activity_createUser.java,如果不是这种情况,您应该更改上面的新Intent(this,Activity_createUser.class);与您的活动类名称匹配

猜你在找的Android相关文章