我正在尝试从联系人列表中选择联系人并将其添加到数据库。但是,每当我在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
传递到FragmentTab2
到ChildTab1
的方法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);
问题最有可能在这里:
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
是否为空!