从我的菜单打开首选项时应用程序崩溃

时间:2013-04-25 06:10:45

标签: android android-preferences

当我从菜单中选择pref时出现以下错误,我的应用程序崩溃了。

04-25 11:02:01.177: D/AndroidRuntime(888): Shutting down VM
04-25 11:02:01.177: W/dalvikvm(888): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
04-25 11:02:01.227: E/AndroidRuntime(888): FATAL EXCEPTION: main
04-25 11:02:01.227: E/AndroidRuntime(888): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.thenewboston.travis.prefs }
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.app.Activity.startActivityForResult(Activity.java:3370)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.app.Activity.startActivityForResult(Activity.java:3331)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.app.Activity.startActivity(Activity.java:3566)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.app.Activity.startActivity(Activity.java:3534)
04-25 11:02:01.227: E/AndroidRuntime(888):  at com.thenewboston.travis.Menu.onOptionsItemSelected(Menu.java:65)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.app.Activity.onMenuItemSelected(Activity.java:2548)
04-25 11:02:01.227: E/AndroidRuntime(888):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
04-25 11:02:01.227: E/AndroidRuntime(888):  at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-25 11:02:01.227: E/AndroidRuntime(888):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
04-25 11:02:01.227: E/AndroidRuntime(888):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-25 11:02:01.227: E/AndroidRuntime(888):  at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:166)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.os.Handler.handleCallback(Handler.java:725)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.os.Looper.loop(Looper.java:137)
04-25 11:02:01.227: E/AndroidRuntime(888):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-25 11:02:01.227: E/AndroidRuntime(888):  at java.lang.reflect.Method.invokeNative(Native Method)
04-25 11:02:01.227: E/AndroidRuntime(888):  at java.lang.reflect.Method.invoke(Method.java:511)
04-25 11:02:01.227: E/AndroidRuntime(888):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-25 11:02:01.227: E/AndroidRuntime(888):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-25 11:02:01.227: E/AndroidRuntime(888):  at dalvik.system.NativeStart.main(Native Method)

首选项enter code here。XML

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <EditTextPreference
    android:title="EditText"
    android:key="name"
    android:summary="Enter Your Name"
    ></EditTextPreference>
    <CheckBoxPreference
        android:title="CheckkBox"
        android:defaultValue="true"
        android:key="checkbox"
        android:summary="Check This box">

    </CheckBoxPreference>
    <ListPreference `enter code here`
        android:title="list"
        android:key="list"
        android:summary="This is a list to choose from"
        android:entries="@array/list"
        android:entryValues="@array/lValues">
        </ListPreference>

</PreferenceScreen>

prefs.java

package com.thenewboston.travis;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class prefs extends PreferenceActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
    }


}

menu.java

package com.thenewboston.travis;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity{

    String classes[] = {"StartingPoint","TextPlay","Email","Camera",
                        "Data","example5","example6","example7"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub

        super.onListItemClick(l, v, position, id);
        String cheese  = classes[position];
        try {
            Class ourClass = Class.forName("com.thenewboston.travis."+cheese);
            Intent ourIntent = new Intent(Menu.this,ourClass);
            startActivity(ourIntent);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);
        MenuInflater blowUp = getMenuInflater();
        blowUp.inflate(R.menu.cool_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
            switch(item.getItemId()){

            case R.id.aboutMe:
                Intent i = new Intent("com.thenewboston.travis.About");
                startActivity(i);

                break;
            case R.id.preferences :
                Intent i1 = new Intent("com.thenewboston.travis.prefs");
                startActivity(i1);
                break;
            case R.id.exit:
                finish();
                break;
            }
        return false;
    }




}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thenewboston.travis"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="KCRaju"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.thenewboston.travis.Splash"
        android:label="KCRaju"
        android:screenOrientation="portrait" >
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".StartingPoint"
        android:label="KCRaju" >
        <intent-filter>
        <action android:name="com.thenewboston.travis.STARTINGPOINT" />
        <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Menu"
        android:label="KCRaju" >
        <intent-filter>
        <action android:name="com.thenewboston.travis.MENU" />
        <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
     <activity
        android:name=".prefs"
        android:label="KCRaju" >
        <intent-filter>
        <action android:name="com.thenewboston.travis.PREFS" />
        <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".About"
        android:label="KCRaju"
        android:theme="@android:style/Theme.Dialog" >
        <intent-filter>
        <action android:name="com.thenewboston.travis.About" />
        <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>
    </activity>
        <activity android:name=".TextPlay" android:label="@string/app_name" >
    </activity>
        <activity android:name=".Email" android:label="@string/app_name" >
    </activity>
         <activity android:name=".Camera" android:label="@string/app_name" android:screenOrientation="portrait" >
    </activity>
     <activity android:name=".Data" android:label="@string/app_name" android:screenOrientation="portrait" >
    </activity>
    <activity android:name=".OpenedClass" android:label="@string/app_name" android:screenOrientation="portrait" >
    </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

您不匹配用于启动活动的Action字符串的大小写:

Intent i1 = new Intent("com.thenewboston.travis.prefs");
startActivity(i1);

与你在Manifest中声明的内容相比:

 <activity
    android:name=".prefs"
    android:label="KCRaju" >
    <intent-filter>
    <action android:name="com.thenewboston.travis.PREFS" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

因此,您正在使用操作"com.thenewboston.travis.prefs"创建一个意图,但您的IntentFilter将匹配"com.thenewboston.travis.PREFS"

或者,尝试从Manifest中的<intent-filter>声明中删除prefs,然后更改启动它的方式:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
        switch(item.getItemId()){

        case R.id.aboutMe:
            Intent i = new Intent(this, About.class);
            startActivity(i);

            break;
        case R.id.preferences :
            Intent i1 = new Intent(this, prefs.class);
            startActivity(i1);
            break;
        case R.id.exit:
            finish();
            break;
        }
    return false;

答案 1 :(得分:0)

在您的清单中,您将活动操作声明为:

<activity
        android:name=".prefs"
        android:label="KCRaju" >
        <intent-filter>
        <action android:name="com.thenewboston.travis.PREFS" />
        <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

请注意大写字母中的PREFS ..在您的代码中,您使用的是小案例prefs

case R.id.preferences :
                Intent i1 = new Intent("com.thenewboston.travis.prefs");
                startActivity(i1);
                break;

尝试使用大写字母PREFS,如下面的代码所示:

case R.id.preferences :
                Intent i1 = new Intent("com.thenewboston.travis.PREFS");
                startActivity(i1);
                break;