2011-12-30 87 views
4

這是Android動態壁紙。Android:如何從首選項菜單啓動活動?

.LiveWallpaperSettings是設置首選項的主要活動。 < - 正常工作

.AboutActivity是簡單的對話框活動。 < - 應用craches

我有以下代碼:

一個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.我通過其他反編譯的應用程序製作了此示例

我該怎麼做?

+0

顯示logcat輸出。 – Flo 2011-12-30 11:06:42

回答

1

YourActivity備份它的類層次結構擴展了ListActivity,因此無法找到列表的錯誤。只需擴展Activity,而不是擴展LiveWallpaperSettings類。

2

問題是AboutActivity延伸LiveWallpaperSettings,它本身是PreferenceActivity的子類,因此是ListActivity的子類。而ListActivity類需要在佈局文件中使用id爲android.R.id.list的ListView。

我想你不需要你關於活動可以通過LiveWallpaperSettings擴展。改用普通的Activity。

相關問題