2012-12-10 189 views
-1
customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { 

       public boolean onPreferenceClick(Preference preference) { 



        Toast.makeText(getBaseContext(), 
        "The custom preference has been clicked", 
        Toast.LENGTH_LONG).show(); 
        SharedPreferences customSharedPreference = 
        getSharedPreferences(
        "myCustomSharedPrefs", Activity.MODE_PRIVATE); 
        SharedPreferences.Editor editor = 
        customSharedPreference 
        .edit(); 
        editor.putString("myCustomPref", 
        "The preference has been clicked"); 
        editor.commit(); 
        return true; 
       } 

      }); 

上面的代碼工作正常如何瀏覽從一個活動到另一個活動onPreferenceClick

但是當我嘗試使用下面

customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { 

       public boolean onPreferenceClick(Preference preference) { 

        Intent int1 = new Intent(getBaseContext(), 
          termandcondition.class); 
        getBaseContext().startActivity(int1); 


        return true; 
       } 

      }); 

它給人錯誤,請告訴我如何implment它

登錄

12-10 15:34:20.405: E/AndroidRuntime(377): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adodis.taxmann/com.adodis.taxmann.termandcondition}: java.lang.NullPointerException Error is coming 
+0

可能重複[如何去針對Android偏好屏幕的另一項活動(http://stackoverflow.com/questions/13833951/how-to-go-another在Android中的活動從首選屏幕) – Damon

回答

0

使用

Intent int1 = new Intent(Current_Activity.this, 
          termandcondition.class); 
        Current_Activity.this.startActivity(int1); 

,而不是

Intent int1 = new Intent(Current_Activity.this, 
          termandcondition.class); 
        Current_Activity.this.startActivity(int1); 

開始新的活動使用Current ActivitygetApplicationContext()語境,而不是getBaseContext()方面

+0

我沒有工作親愛的我在共享普羅芬斯類起訴這個代碼 – user1872231

+1

張貼您的termandcondition.class代碼也 –

+1

,因爲你有問題termandcondition.class活動 –

0

嘗試做來回XML file..as例如:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.mycompany.myapp" android:versionName="1.3" android:versionCode="4"> 

... 

    <activity android:name=".termandcondition" 
       android:label="@string/my_activity_title" 
       android:theme="@android:style/Theme.Black.NoTitleBar"> 
     <intent-filter> 
      <action android:name=".termandcondition" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

然後在首xml文件:的

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

<ListPreference 
    android:entries="@array/listOptions" 
    android:entryValues="@array/listValues" 
    android:key="listpref" 
    android:summary="Sharing Taxmann Application" 
    android:title="Share Application" /> 

<PreferenceCategory> 
    <ListPreference 
     android:entries="@array/listOptions2" 
     android:entryValues="@array/listValues2" 
     android:key="listprefrefresh" 
     android:summary="set Refresh The Applciation" 
     android:title="Set TIme Intervale" /> 
</PreferenceCategory> 
<PreferenceCategory> 
    <ListPreference 
     android:entries="@array/listOptions3" 
     android:entryValues="@array/listValues3" 
     android:key="listtaxmann" 
     android:summary="About Taxmann" 
     android:title="Taxmann" /> 
</PreferenceCategory> 

<PreferenceCategory 
     android:title="@string/my_activity_title"> 
    <PreferenceScreen 
       android:key="customPref" 
     android:title="Term and Condition" 
     android:summary="END-USER LICENCE AGREEMENT FOR USING WWW.TAXMANN.COM" 
     <intent android:action=".termandcondition"/> 
    </PreferenceScreen> 
</PreferenceCategory> 

+0

MyActivity哪類說得好? – user1872231

+0

termandcondition – Nermeen

+0

http://pastie.org/5506240可以請編輯並把該代碼plz這是我的Prefnces.xml – user1872231

相關問題