1
我想修改一箇舊的FragmentActivity承載一個視圖分頁器與3片段中的listview。 Fragment類具有與活動類似的代碼。事實上,在將現有的Android應用程序轉換爲使用片段時,我認爲將代碼從活動的回調方法移動到片段的相應回調方法中就足夠了。但它沒有奏效。我在該行ViewPager NullPointerException修改Fragment中的FragmentActivity
mViewPager.setAdapter(mAppSectionsPagerAdapter);
在這裏得到一個NullPointerException異常是代碼:
public class FragmentAllEvents extends Fragment implements ActionBar.TabListener
{
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
// JSON Node names
private static final String TAG_UID = "uid";
private static final String TAG_LOGO = "logo";
private static final String TAG_POKUID = "pokuid";
static ArrayList<HashMap<String, String>> userList;
ArrayList<HashMap<String, String>> userListTotal;
HashMap<String, String> userSelected;
EventsFunctions eventsFunctions;
UserFunctions userFunctions;
/**
* The {@link ViewPager} that will display the three primary sections of the app, one at a
* time.
*/
ViewPager mViewPager;
static ListView lv;
ActionBar actionBar;
//Context context = this;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.all_events_main, container, false);
Log.e("AllEventsFragmentActivity Report", "Entering AllEventsFragments");
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
//FragmentManager fm = getSupportFragmentManager();
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getFragmentManager());
// Set up the action bar.
actionBar = getActivity().getActionBar();
// Specify that the Home/Up button should not be enabled, since there is no hierarchical
// parent.
actionBar.setHomeButtonEnabled(true);
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager, attaching the adapter and setting up a listener for when the
// user swipes between sections.
mViewPager = (ViewPager) getActivity().findViewById(R.id.pager_main);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
// Defining a listener for pageChange
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
[...]
});
[...]
}
在以前的版本這個類,我延長FragmentAllEvents用FragmentActivity而不是片段。