我将 PreferenceFragment 用于我的设置页面,我有一个偏好,即 onclick 将打开联系人以选择并返回结果。但是,它有一个失败的交付结果错误。
这是我的偏好片段类
public class SettingsXML extends PreferenceFragment {
private SharedPreferences sharedPrefs;
final static int PICK_CONTACT = 99;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set background to Drawable
addPreferencesFromResource(R.xml.settings);
// Get Custom contact Pref
Preference customContact = (Preference)findPreference("prefPick");
customContact.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
return true;
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri contactData = data.getData();
// contactData returns some value
Log.i(TAG, "" + contactData);
String[] projection = {Phone.NUMBER};
// Something wrong at cursor?
Cursor cursor = ctx.getContentResolver()
.query(contactData, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
Log.i(TAG, "" + number);
}
} else {
//
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
还有我的日志猫。
05-02 17:36:27.496: W/dalvikvm(6142): threadid=1: thread exiting with uncaught exception (group=0xb4e3b908)
05-02 17:36:27.906: D/dalvikvm(6142): GC_CONCURRENT freed 356K, 10% free 4398K/4864K, paused 12ms+64ms, total 146ms
05-02 17:36:30.607: E/AndroidRuntime(6142): FATAL EXCEPTION: main
05-02 17:36:30.607: E/AndroidRuntime(6142): java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=99, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/1906i7ef0c7d80ffe6bca/2 flg=0x1 }} to activity {SettingsFragment}: java.lang.NullPointerException
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.os.Looper.loop(Looper.java:137)
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-02 17:36:30.607: E/AndroidRuntime(6142): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 17:36:30.607: E/AndroidRuntime(6142): at java.lang.reflect.Method.invoke(Method.java:511)
05-02 17:36:30.607: E/AndroidRuntime(6142): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-02 17:36:30.607: E/AndroidRuntime(6142): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-02 17:36:30.607: E/AndroidRuntime(6142): at dalvik.system.NativeStart.main(Native Method)
05-02 17:36:30.607: E/AndroidRuntime(6142): Caused by: java.lang.NullPointerException
05-02 17:36:30.607: E/AndroidRuntime(6142): at SettingsXML.onActivityResult(SettingsXML.java:80)
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.app.Activity.dispatchActivityResult(Activity.java:5297)
05-02 17:36:30.607: E/AndroidRuntime(6142): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
05-02 17:36:30.607: E/AndroidRuntime(6142): ... 11 more
我确实在清单上设置了权限。