goderbauer / contact_picker

A Flutter plugin for picking a contact from the address book.
BSD 3-Clause "New" or "Revised" License
77 stars 109 forks source link

I'm getting an exception "Failure delivering result ResultInfo". #1

Closed fiatjaf closed 6 years ago

fiatjaf commented 6 years ago

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:

        onPressed: () async {
          try {
            var contact = await _contactPicker.selectContact();
            if (contact != null) {
              R.navigateTo(
                context,
                '/peer/${contact.phoneNumber.number}',
                transition: TransitionType.fadeIn,
              );
            } else {
              print('contact is null');
            }
          } catch (exc, st) {
            print('exception');
            print(exc);
            print(st);
          }
        },

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 that try-catch block didn't change a thing.

The error logs from the Flutter CLI:

I/Timeline( 8684): Timeline: Activity_idle id: android.os.BinderProxy@39635fb9 time:9891368
I/OpenGLRenderer( 8684): Initialized EGL, version 1.4
I/Timeline( 8684): Timeline: Activity_idle id: android.os.BinderProxy@39635fb9 time:9908917
I/OpenGLRenderer( 8684): Initialized EGL, version 1.4
I/Timeline( 8684): Timeline: Activity_idle id: android.os.BinderProxy@39635fb9 time:9918850
I/flutter ( 8684): contact is null
D/AndroidRuntime( 8684): Shutting down VM
E/AndroidRuntime( 8684): FATAL EXCEPTION: main
E/AndroidRuntime( 8684): Process: com.yourcompany.debtmanager, PID: 8684
E/AndroidRuntime( 8684): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2015, result=0, data=null} to activity {com.yourcompany.debtmanager/com.yourcompany.debtmanager.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
E/AndroidRuntime( 8684):        at android.app.ActivityThread.deliverResults(ActivityThread.java:3607)
E/AndroidRuntime( 8684):        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3650)
E/AndroidRuntime( 8684):        at android.app.ActivityThread.access$1400(ActivityThread.java:154)
E/AndroidRuntime( 8684):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
E/AndroidRuntime( 8684):        at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 8684):        at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 8684):        at android.app.ActivityThread.main(ActivityThread.java:5294)
E/AndroidRuntime( 8684):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 8684):        at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 8684):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
E/AndroidRuntime( 8684):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
E/AndroidRuntime( 8684): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
E/AndroidRuntime( 8684):        at net.goderbauer.flutter.contactpicker.ContactPickerPlugin.onActivityResult(ContactPickerPlugin.java:66)
E/AndroidRuntime( 8684):        at io.flutter.app.FlutterActivityDelegate.onActivityResult(FlutterActivityDelegate.java:137)
E/AndroidRuntime( 8684):        at io.flutter.app.FlutterActivity.onActivityResult(FlutterActivity.java:103)
E/AndroidRuntime( 8684):        at android.app.Activity.dispatchActivityResult(Activity.java:6192)
E/AndroidRuntime( 8684):        at android.app.ActivityThread.deliverResults(ActivityThread.java:3603)
E/AndroidRuntime( 8684):        ... 10 more
I/Process ( 8684): Sending signal. PID: 8684 SIG: 9
Lost connection to device.
Mahender-Prayuta commented 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

goderbauer commented 6 years ago

@Mahender-Prayuta Do you want to send a pull request with that change?

tjerkw commented 6 years ago

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
tjerkw commented 6 years ago

@goderbauer after merging the pull request this issue should be resolved.

Nice library! Really appreciated!

AmandaDEn32 commented 6 years ago

is there any equivalent for contactscontract in flutter?

Mahi-K commented 6 years ago

hi @AmandaDEn32 sorry, i didn't get your question. Could you please explain what you are trying to achieve? Thanks.

AmandaDEn32 commented 6 years ago

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?

harshOnAndroid commented 5 years ago

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