私はその ViewPager で FragmentActivity を使用しています。アダプタの呼び出しと public Fragment getItem(int position); を取得しています。が呼び出されていないため、フラグメントが null になっています。そのため、onActivityResult(...) でアダプターをリセットした後に現在表示されているフラグメントを取得しようとすると、null になります。
これは私がonCreateでやっていることです:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.blocked_list);
lable = getIntent().getStringExtra("ItemName");
blocked_list_lable = (TextView) findViewById(R.id.blocked_list_lable);
add_Contact = (Button) findViewById(R.id.add);
mAdapter = new CallManagerFragmentAdapter(getSupportFragmentManager(), lable);
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
addNewContact = new AddNewContactPopup(this);
addNewContact.setPopDimentions((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 320, getResources()
.getDisplayMetrics()), (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 280, BlackList.this.getResources()
.getDisplayMetrics()));
blocked_list_lable.setText(lable);
setListener(add_Contact);
}
これは私が onActivityResult でやっていることです:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CALLS_SMS_LOGS || requestCode == CONTACTS_LIST) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("dbUpdateSucess");
if (result.equals("Success")) {
Logger.log("Info", TAG,
"Update from call logs is Sucess...");
//Cursor cursor = getCursor(lable);
mAdapter = new CallManagerFragmentAdapter(getSupportFragmentManager(), lable);
mPager.setAdapter(mAdapter);
//adapter = new BlackListAdapter(this, cursor);
//blocked_List.setAdapter(adapter);
return;
} else {
Logger.log("Error", TAG, "Update from call logs failed...");
}
}
}
}
これが私の FragmentAdapter コードです。
class CallManagerFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
protected static final String[] CONTENT = new String[] { "Call Logs", "Blacklist No"};
protected static final String[] BLACK_LIST_SMS_CONTENT = new String[] { "SMS Logs", "Blacklist No"};
protected static final String[] WHITELIST_CALLS_CONTENT = new String[] { "Call Logs", "Whitelist No"};
protected static final String[] WHITELIST_SMS_CONTENT = new String[] { "SMS Logs", "Whitelist No"};
protected static final String[] PRIVATE = new String[] { "SMS Logs", "Call Logs", "Private No"};
protected static final int[] ICONS = new int[] {
R.drawable.perm_group_calendar,
R.drawable.perm_group_camera/*,
R.drawable.perm_group_device_alarms,
R.drawable.perm_group_location*/
};
private int mCount = CONTENT.length;
private String lable;
public HashMap<Integer, CallManagerFragment> fragmentMap;
public CallManagerFragmentAdapter(FragmentManager fm, String lable) {
super(fm);
this.lable = lable;
fragmentMap = new HashMap<Integer, CallManagerFragment>();
}
@Override
public Fragment getItem(int position) {
String content;
CallManagerFragment fragment = CallManagerFragment.newInstance(content, lable);
fragmentMap.put(position, fragment);
return fragment;
}
public CallManagerFragment getFragment(int key) {
return fragmentMap.get(key);
}
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
fragmentMap.remove(position);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
if(lable.equals("Whitelist Calls")) {
return CallManagerFragmentAdapter.WHITELIST_CALLS_CONTENT[position % WHITELIST_CALLS_CONTENT.length];
} else if (lable.equals("Whitelist SMS")) {
return WHITELIST_SMS_CONTENT[position % WHITELIST_SMS_CONTENT.length];
} else if(lable.equals("Private")) {
return PRIVATE[position % PRIVATE.length];
} else if(lable.equals("Blacklist SMS")) {
return BLACK_LIST_SMS_CONTENT[position % BLACK_LIST_SMS_CONTENT.length];
}
return CallManagerFragmentAdapter.CONTENT[position % CONTENT.length];
}
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}