2012-07-27 109 views
0

我想從我的首選項屏幕啓動一項活動,任何幫助將不勝感激。這是我一直在玩的一些代碼。通過使用onPreferenceClick啓動活動

Preference customerPref2 = (Preference) findPreference("community"); 
    customerPref2.setOnPreferenceClickListener(new OnPreferenceClickListener() { 

    public boolean onPreferenceClick(Preference preference) { 
       Toast.makeText(getBaseContext(), 
       "Launching Activity", 
       Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent();                 
      intent.setClassName("com.xxx.xxxx", "Community"); 
      intent.setClass(Prefs.this, Community.class); 
      startActivity(intent); 

我不知道是幹什麼用這個代碼的一部分,所以我既包括setClassName和公正的setClass

Preference.xml

 <Preference 
      android:title="Checkout this Activity" 
      android:summary="Launch the Activity" 
      android:key="community"> 

    <intent android:action="android.intent.action.VIEW" 
     android:targetPackage="com.xxx.xxxx" 
     android:targetClass="com.xxx.xxxx.Community 

也是我所發現的是人們說因爲當我試圖發起活動時有時會說它無法找到它。 這裏是我的清單

<activity android:name=".Community" android:label="@string/gotodashboard"> 
     <intent-filter> 
      <action android:name="android.intent.category.VIEW" /> 
      <category android:name="android.intent.category.PREFERENCE" /> 
     </intent-filter> 
    </activity> 

任何幫助或源代碼示例會幫我想出解決辦法。感謝gusy。

回答

1

我已經通過將我的首選項java文件中的代碼變爲此來解決了此問題。

public boolean onPreferenceClick(Preference preference) { 
    Intent myIntent = new Intent(Prefs.this, Community.class); 
    Prefs.this.startActivity(myIntent); 
    return true; 
    }