Android fragments 修改片段中的片段活动时ViewPager NullPointerException
我正在尝试修改一个旧的片段活动,该活动承载一个片段中包含3个listview的视图寻呼机。 Fragment类具有类似于活动的代码。事实上,在将现有Android应用程序转换为使用片段时,我认为将代码从活动的回调方法移动到片段的相应回调方法就足够了。但它不起作用。我在线路上得到一个NullPointerExceptionAndroid fragments 修改片段中的片段活动时ViewPager NullPointerException,android-fragments,android-viewpager,Android Fragments,Android Viewpager,我正在尝试修改一个旧的片段活动,该活动承载一个片段中包含3个listview的视图寻呼机。 Fragment类具有类似于活动的代码。事实上,在将现有Android应用程序转换为使用片段时,我认为将代码从活动的回调方法移动到片段的相应回调方法就足够了。但它不起作用。我在线路上得到一个NullPointerException mViewPager.setAdapter(mAppSectionsPagerAdapter); 代码如下: public class FragmentAllEvents e
mViewPager.setAdapter(mAppSectionsPagerAdapter);
代码如下:
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扩展片段实现ActionBar.TabListener
{
应用分区SPAGERAAdapter映射分区SPAGERAAdapter;
//JSON节点名称
私有静态最终字符串标记\u UID=“UID”;
私有静态最终字符串标记_LOGO=“LOGO”;
私有静态最终字符串标记_POKUID=“POKUID”;
静态ArrayList用户列表;
ArrayList userListTotal;
HashMap用户选择;
事件函数事件函数;
用户函数用户函数;
/**
*{@link ViewPager}将显示应用程序的三个主要部分,一次一个
*时间。
*/
ViewPager mViewPager;
静态ListView lv;
ActionBar ActionBar;
//上下文=这个;
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
视图v=充气机。充气(R.layout.all_events_main,container,false);
Log.e(“AllEventsFragmentActivity报告”,“输入AllEventsFragments”);
//创建适配器,该适配器将为三个主要部分中的每个部分返回一个片段
//应用程序的名称。
//FragmentManager fm=getSupportFragmentManager();
mAppSectionsPagerAdapter=新的AppSectionsPagerAdapter(getFragmentManager());
//设置操作栏。
actionBar=getActivity().getActionBar();
//指定不应启用Home/Up按钮,因为没有层次结构
//家长。
actionBar.setHomeButtonEnabled(真);
//指定我们将在操作栏中显示选项卡。
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u选项卡);
//设置ViewPager,连接适配器并设置侦听器,以便
//用户在分区之间滑动。
mViewPager=(ViewPager)getActivity().findViewById(R.id.pager_main);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
//定义页面更改的侦听器
mViewPager.setOnPageChangeListener(新的ViewPager.SimpleOnPageChangeListener()
{
[...]
});
[...]
}
在这个类的早期版本中,我使用FragmentActivity而不是Fragment来扩展FragmentAllEvents。看起来,mViewPager未填充且为空。如果
pager\u main
位于all\u events\u main
布局中,则需要使用v.findViewById(R.id.pager\u main)