設定ページに PreferenceFragment を使用しています。ここでは、onclick が連絡先を開いて選択し、結果を返すという設定が 1 つあります。ただし、結果エラーの配信に失敗しました。
これは私の好みのフラグメントクラスです
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
マニフェストでアクセス許可を設定しています。