Android:如何从首选项菜单启动活动?

时间:2011-12-30 10:48:52

标签: java android android-activity settings live-wallpaper

这是Android动态壁纸。

.LiveWallpaperSettings是设置首选项的主要活动。 < - 正常工作

.AboutActivity是简单的对话活动。 < - 申请破产

我有以下代码:

A ndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="7" />
    <uses-feature android:name="ru.fph.iiidlayer" />

    <application android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:debuggable="true" >

        <supports-screen android:anyDensity="true" />

        <service android:name=".LiveWallpaper"
            android:label="@string/app_name"
            android:icon="@drawable/ic_launcher"
            android:permission="android.permission.BIND_WALLPAPER" >

            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data android:name="android.service.wallpaper"
                android:resource="@xml/livewallpaper" />

        </service>

        <activity android:label="@string/app_name"
            android:name=".LiveWallpaperSettings"
            android:theme="@android:style/Theme.Light.WallpaperSettings"
            android:exported="true"
            android:icon="@drawable/ic_launcher">
        </activity>

        <activity android:theme="@android:style/Theme.Dialog" 
            android:label="@string/livewallpaper_about_title" 
            android:name=".AboutActivity">
            <intent-filter>
                <action android:name="ABOUT_ACTION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>
    <supports-screens android:anyDensity="true" 
        android:smallScreens="true" 
        android:normalScreens="true" 
        android:largeScreens="true" 
        android:resizeable="true" />
</manifest> 

livewallpaper_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/livewallpaper_settings"
    android:key="livewallpaper_settings">

    <Preference android:key="livewallpaper_image" 
        android:title="@string/livewallpaper_image_title" 
        android:summary="@string/livewallpaper_image_summary" />
    <ListPreference
        android:key="livewallpaper_sens"
        android:title="@string/livewallpaper_sens_title"
        android:summary="@string/livewallpaper_sens_summary"
        android:entries="@array/livewallpaper_sens_names"
        android:entryValues="@array/livewallpaper_sens_prefix"/>
    <Preference android:key="livewallpaper_about"
        android:title="@string/livewallpaper_about_title"
        android:summary="@string/livewallpaper_about_summary">
        <intent android:action="ABOUT_ACTION" />
    </Preference>
</PreferenceScreen>

LivewallpaperSettings.java

package ru.fph.iiidlayer;

import ru.fph.iiidlayer.R;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.provider.MediaStore;

public class LiveWallpaperSettings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener, OnPreferenceClickListener
{
    Preference gallery_pref, about_pref;
    Intent gallery, about;
    String gallery_key = "livewallpaper_image";
    String about_key = "livewallpaper_about";

    @Override
    protected void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        getPreferenceManager().setSharedPreferencesName(LiveWallpaper.SHARED_PREFS_NAME);
        addPreferencesFromResource(R.xml.livewallpaper_settings);
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

        gallery_pref = findPreference(gallery_key);
        gallery = new Intent();
        gallery.setType("image/*");
        gallery.setAction(gallery.ACTION_GET_CONTENT);
        gallery_pref.setOnPreferenceClickListener(this);

        about_pref = findPreference(about_key);
        about = new Intent(this, AboutActivity.class);
        about_pref.setOnPreferenceClickListener(this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK)
            if(requestCode == 1) {
                String src = getRealPathFromURI(data.getData());
                LiveWallpaper.backgroundSrc = src;
            }
    }

    public boolean onPreferenceClick(Preference pr) {
        if(pr.getKey().equals(gallery_key)) {
            startActivityForResult(Intent.createChooser(gallery, getString(R.string.livewallpaper_gallery_title)),1);
        }
        /* if(pr.getKey().equals(about_key)) {
            startActivity(about);
        } */
        return false;
    }

    public String getRealPathFromURI(Uri contentUri) {
        String [] proj={MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(contentUri, proj, null, null,  null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onDestroy()
    {
        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        super.onDestroy();
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
    }
}

AboutActivity.java

package ru.fph.iiidlayer;

import android.app.Activity;
import android.os.Bundle;

public class AboutActivity extends LiveWallpaperSettings {
    @Override
    protected void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.about);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }
}

logcat的:

12-30 11:11:26.967: D/AndroidRuntime(228): Shutting down VM
12-30 11:11:26.967: W/dalvikvm(228): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-30 11:11:26.967: E/AndroidRuntime(228): Uncaught handler: thread main exiting due to uncaught exception
12-30 11:11:26.987: E/AndroidRuntime(228): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.fph.iiidlayer/ru.fph.iiidlayer.AboutActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.os.Looper.loop(Looper.java:123)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-30 11:11:26.987: E/AndroidRuntime(228):  at java.lang.reflect.Method.invokeNative(Native Method)
12-30 11:11:26.987: E/AndroidRuntime(228):  at java.lang.reflect.Method.invoke(Method.java:521)
12-30 11:11:26.987: E/AndroidRuntime(228):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-30 11:11:26.987: E/AndroidRuntime(228):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-30 11:11:26.987: E/AndroidRuntime(228):  at dalvik.system.NativeStart.main(Native Method)
12-30 11:11:26.987: E/AndroidRuntime(228): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.ListActivity.onContentChanged(ListActivity.java:236)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.preference.PreferenceActivity.onContentChanged(PreferenceActivity.java:160)
12-30 11:11:26.987: E/AndroidRuntime(228):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.Activity.setContentView(Activity.java:1622)
12-30 11:11:26.987: E/AndroidRuntime(228):  at ru.fph.iiidlayer.AboutActivity.onCreate(AboutActivity.java:11)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-30 11:11:26.987: E/AndroidRuntime(228):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-30 11:11:26.987: E/AndroidRuntime(228):  ... 11 more
12-30 11:11:27.017: I/Process(52): Sending signal. PID: 228 SIG: 3
12-30 11:11:27.017: I/dalvikvm(228): threadid=7: reacting to signal 3
12-30 11:11:27.017: I/dalvikvm(228): Wrote stack trace to '/data/anr/traces.txt'

图库设置首选项工作正常。但是当我尝试以相同的方式调用AboutActivity时(LivewallpaperSettings.java中的注释部分),应用程序在点击首选项时崩溃。

P.S。我用其他反编译的应用程序做了这个例子

我做错了什么?

2 个答案:

答案 0 :(得分:2)

问题是AboutActivity扩展LiveWallpaperSettings,它本身是PreferenceActivity的子类,因此是ListActivity的子类。 ListActivity类需要布局文件中具有标识android.R.id.list的ListView。

我想你不需要你LiveWallpaperSettings扩展了AboutActivity。改为使用普通的活动。

答案 1 :(得分:1)

您的AboutActivity备份它的类层次结构正在扩展ListActivity,因此导致无法找到列表的错误。只需扩展Activity而不是扩展LiveWallpaperSettings类。

相关问题