Closed fiatjaf closed 6 years ago
Hi there,
I had the same problem, i think you need to change the android code as follows. I mean you need to edit the C:\your pub-cache directory\contact_picker-0.0.1+2\android\src\main\java\net\goderbauer\flutter\contactpicker\ContactPickerPlugin.java
to the following code
Android Code:
// Copyright 2017 Michael Goderbauer. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package net.goderbauer.flutter.contactpicker;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import java.util.HashMap;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import static android.app.Activity.RESULT_OK;
public class ContactPickerPlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener {
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "contact_picker");
ContactPickerPlugin instance = new ContactPickerPlugin(registrar.activity());
registrar.addActivityResultListener(instance);
channel.setMethodCallHandler(instance);
}
private ContactPickerPlugin(Activity activity) {
this.activity = activity;
}
private static int PICK_CONTACT = 2015;
private Activity activity;
private Result pendingResult = null;
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("selectContact")) {
if (pendingResult != null) {
pendingResult.error("multiple_requests", "Cancelled by a second request.", null);
pendingResult = null;
}
pendingResult = result;
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
activity.startActivityForResult(i, PICK_CONTACT);
} else {
//result.notImplemented();
pendingResult = null;
}
}
@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != PICK_CONTACT) {
return false;
}
if (resultCode != RESULT_OK) {
pendingResult.success(null);
pendingResult = null;
}
if(pendingResult != null){
Uri contactUri = data.getData();
Cursor cursor = activity.getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
int phoneType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
String label = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(activity.getResources(), phoneType, customLabel);
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String fullName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
HashMap<String, Object> phoneNumber = new HashMap<>();
phoneNumber.put("number", number);
phoneNumber.put("label", label);
HashMap<String, Object> contact = new HashMap<>();
contact.put("fullName", fullName);
contact.put("phoneNumber", phoneNumber);
pendingResult.success(contact);
pendingResult = null;
return true;
}
else{
pendingResult = null;
return false;
}
}
}
I hope this will solve your problem. please close the bug if this works.
Many thanks Mahi
@Mahender-Prayuta Do you want to send a pull request with that change?
Same problem here!
E/AndroidRuntime( 4660): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2015, result=0, data=null} to activity {nl.boomrang.boomrangapp/nl.boomrang.boomrangapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
E/AndroidRuntime( 4660): at android.app.ActivityThread.deliverResults(ActivityThread.java:4324)
E/AndroidRuntime( 4660): at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)
E/AndroidRuntime( 4660): at android.app.ActivityThread.-wrap19(Unknown Source:0)
E/AndroidRuntime( 4660): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)
E/AndroidRuntime( 4660): at android.os.Handler.dispatchMessage(Handler.java:105)
E/AndroidRuntime( 4660): at android.os.Looper.loop(Looper.java:164)
E/AndroidRuntime( 4660): at android.app.ActivityThread.main(ActivityThread.java:6541)
E/AndroidRuntime( 4660): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 4660): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
E/AndroidRuntime( 4660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
E/AndroidRuntime( 4660): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
E/AndroidRuntime( 4660): at net.goderbauer.flutter.contactpicker.ContactPickerPlugin.onActivityResult(ContactPickerPlugin.java:66)
E/AndroidRuntime( 4660): at io.flutter.app.FlutterPluginRegistry.onActivityResult(FlutterPluginRegistry.java:184)
E/AndroidRuntime( 4660): at io.flutter.app.FlutterActivityDelegate.onActivityResult(FlutterActivityDelegate.java:137)
E/AndroidRuntime( 4660): at io.flutter.app.FlutterActivity.onActivityResult(FlutterActivity.java:122)
E/AndroidRuntime( 4660): at android.app.Activity.dispatchActivityResult(Activity.java:7235)
E/AndroidRuntime( 4660): at android.app.ActivityThread.deliverResults(ActivityThread.java:4320)
E/AndroidRuntime( 4660): ... 9 more
@goderbauer after merging the pull request this issue should be resolved.
Nice library! Really appreciated!
is there any equivalent for contactscontract in flutter?
hi @AmandaDEn32 sorry, i didn't get your question. Could you please explain what you are trying to achieve? Thanks.
hi @Mahi-K , well i want to list the groups of contacts that i have created on my phone, how to do that with flutter?
I am having the same issue but with sms package. I think the bug is in CallLogPlugin.java file. Can someone fix this? What should I do temporarily?
Below is my logcat-
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=2, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.harsh.navigation/com.harsh.navigation.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.MethodChannel$Result.error(java.lang.String, java.lang.String, java.lang.Object)' on a null object reference E/AndroidRuntime(14043): at android.app.ActivityThread.deliverResults(ActivityThread.java:4324) E/AndroidRuntime(14043): at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367) E/AndroidRuntime(14043): at android.app.ActivityThread.-wrap19(Unknown Source:0) E/AndroidRuntime(14043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649) E/AndroidRuntime(14043): at android.os.Handler.dispatchMessage(Handler.java:105) E/AndroidRuntime(14043): at android.os.Looper.loop(Looper.java:164) E/AndroidRuntime(14043): at android.app.ActivityThread.main(ActivityThread.java:6548) E/AndroidRuntime(14043): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(14043): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) E/AndroidRuntime(14043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) E/AndroidRuntime(14043): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.MethodChannel$Result.error(java.lang.String, java.lang.String, java.lang.Object)' on a null object reference E/AndroidRuntime(14043): at sk.fourq.calllog.CallLogPlugin.onRequestPermissionsResult(CallLogPlugin.java:66) E/AndroidRuntime(14043): at io.flutter.app.FlutterPluginRegistry.onRequestPermissionsResult(FlutterPluginRegistry.java:191) E/AndroidRuntime(14043): at io.flutter.app.FlutterActivityDelegate.onRequestPermissionsResult(FlutterActivityDelegate.java:125) E/AndroidRuntime(14043): at io.flutter.app.FlutterActivity.onRequestPermissionsResult(FlutterActivity.java:133) E/AndroidRuntime(14043): at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7425) E/AndroidRuntime(14043): at android.app.Activity.dispatchActivityResult(Activity.java:7266) E/AndroidRuntime(14043): at android.app.ActivityThread.deliverResults(ActivityThread.java:4320) E/AndroidRuntime(14043): ... 9 more
Sorry if this is a stupid issue, this is my first Android app and first contact with Dart or Flutter.
The relevant part of my code that calls this library:
The contact picker is shown correctly and when I do select a contact from the contact picker it does what I expect (the route navigation), but when I click on "back" in the contact picker the app crashes.
Before crashing, it prints
'contact is null'
, which is odd. Adding thattry-catch
block didn't change a thing.The error logs from the Flutter CLI: