如何从一个活动导航到另一个活动onPreferenceClick

时间:2012-12-10 12:13:09

标签: android android-intent sharedpreferences

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;
                }

            });

以上代码工作正常

但是当我尝试使用下面的

customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {

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


                    return true;
                }

            });

它给出错误,请告诉我如何实施它

日志

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 

2 个答案:

答案 0 :(得分: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()上下文

答案 1 :(得分:0)

尝试为xml文件执行此操作......如下:

<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>

然后在Preferences 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>