发送失败结果ResultInfo {who = null,request = 1,result = -1,data = Intent {dat = content://com.android.contacts/contacts/lookup ...}}}

问题描述 投票:2回答:1

我正在尝试从联系人列表中选择联系人并将其添加到数据库。但是,每当我在onActivityResult中选择联系人和流程数据时,它都会抛出此异常:

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/80i23a826b40e879ef2/314 flg=0x1 }} to activity {com.example.MyApplication/com.example.MyApplication.MainActivity}: java.lang.NullPointerException
Caused by: java.lang.NullPointerException at com.example.myapplication.ChildTab1.onActivityResult(ChildTab1.java:92)

点击“添加联系人”按钮(在ChildTab2类中):

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
getActivity().startActivityForResult(intent, PICK_CONTACT);

onActivityResult方法(在ChildTab2类中):

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
    case (PICK_CONTACT) :
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c =  getActivity().getContentResolver().query(contactData, null, null, null, null);
            if (c.moveToFirst()) {

                String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String phoneNumber = null;

                String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                if (hasPhone.equalsIgnoreCase("1")) 
                {
                    Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                    while (phones.moveToNext()) 
                    {
                        phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }
                    phones.close();

                    ContentValues values = new ContentValues();
                    values.put(ProfilesProvider.NAME,name);
                    values.put(ProfilesProvider.NUMBER,phoneNumber);
                    getActivity().getContentResolver().insert(ProfilesProvider.PROFILES_CONTENT_URI, values);

                }
                else {
                    Toast.makeText(getActivity().getBaseContext(), "Phone Number Not Found", Toast.LENGTH_SHORT).show();
                }


            }
        }
    break;
    }
}

在我的应用程序中,我已经实现了ActionBarShelock。因此,有三个选项卡(扩展SherlockFragment),第二个选项卡包含两个选项卡(也扩展了SherlockFragment)。所以,我将onActivityResult值从MainActivity传递到FragmentTab2ChildTab1的方法onActivityResult,如下所示:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      new FragmentTab2().onActivityResult(requestCode, resultCode, intent);
 }

用于创建前三个选项卡的代码(在MainActivity类中):

// Activate Navigation Mode Tabs
    mActionBar = getSupportActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Locate ViewPager in activity_main.xml
    mPager = (ViewPager) findViewById(R.id.pager);

    // Activate Fragment Manager
    FragmentManager fm = getSupportFragmentManager();

    // Capture ViewPager page swipes
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Find the ViewPager Position
            mActionBar.setSelectedNavigationItem(position);
        }
    };

    mPager.setOnPageChangeListener(ViewPagerListener);
    // Locate the adapter class called ViewPagerAdapter.java
    ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
    // Set the View Pager Adapter into ViewPager
    mPager.setAdapter(viewpageradapter);

// Create first Tab
    tab = mActionBar.newTab().setText("Tab 1").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create second Tab
    tab = mActionBar.newTab().setText("Tab 2").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create third Tab
    tab = mActionBar.newTab().setText("Tab 3").setTabListener(tabListener);
    mActionBar.addTab(tab);

以及用于创建子选项卡的代码(在FragmentTab2类中):

tabHost = new FragmentTabHost(getSherlockActivity());
    tabHost.setup(getSherlockActivity(), getChildFragmentManager(), R.layout.fragmenttab2);

    // Create Child Tab1
    tabHost.addTab(tabHost.newTabSpec("childTab1").setIndicator("Child Tab 1"), ChildTab1.class, null);

    // Create Child Tab2
    tabHost.addTab(tabHost.newTabSpec("childTab2").setIndicator("Child Tab 2"), ChildTab2.class, null);
android actionbarsherlock android-tabhost onactivityresult android-nested-fragment
1个回答
0
投票

问题最有可能在这里:

new FragmentTab2().onActivityResult(requestCode, resultCode, intent);

您正在创建新实例,而不是在现有实例上调用该方法。将Fragment实例保存在成员变量中,并在null检查后调用此实例上的方法。

你的第二个错误 - NullPointerException - 出现在这里:

Cursor c =  getActivity().getContentResolver().query(contactData, null, null, null, null);

很可能是由同一件事引起的。由于您创建了一个未连接到任何Fragment的新Activity实例,因此getActivity方法将返回null

因此,基本上修复两个错误只需在附加到onActivityResult(...)的现有Fragment实例上调用Activity方法。它应该看起来像这样:

private FragmentTab2 fragmentTab2;  // Save the instance in this variable   

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if(fragmentTab2 != null) {
        fragmentTab2.onActivityResult(requestCode, resultCode, intent);
    }
}

编辑:

要获得Fragment实例,您必须在onAttachFragment()中覆盖Activity,如下所示:

private FragmentTab2 fragmentTab2;
private SomeOtherFragment someOtherFragment;

@Override
public void onAttachFragment(Fragment fragment) {
    super.onAttachFragment(fragment);

    // For each of your Fragments add an if statement here checking for 
    // the class of the Fragment and assign if it to the member variables 
    // if a match is found
    if (fragment instanceof FragmentTab2) {

        this.fragmentTab2 = (FragmentTab2) fragment;

    } else if(fragment instanceof SomeOtherFragment) {

        this.someOtherFragment = (SomeOtherFragment) fragment;

    } else ...
}

使用这种方法,每次要在实例上调用方法时,空检查就更为重要,所以不要忘记始终检查Fragments是否为空!

© www.soinside.com 2019 - 2024. All rights reserved.