2012-07-27 101 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; 
    }