Android getBaseContext()=片段错误

Android getBaseContext()=片段错误,android,listview,fragment,Android,Listview,Fragment,编辑:我设法让它工作了,谢谢你的帮助 我试图遵循指南,但即使我将几乎所有的代码都更改为与指南相同的代码,我仍然会遇到错误和崩溃。当我启动应用程序时,它会自动关闭 这是我的指南:要在Android中使用片段中的上下文,您应该首先获得包含当前片段的活动的上下文。所以这条线 StringArrayAdapter ad = new StringArrayAdapter(getApplicationContext(), aList, R.layout.list_design, from, to); 应该

编辑:我设法让它工作了,谢谢你的帮助

我试图遵循指南,但即使我将几乎所有的代码都更改为与指南相同的代码,我仍然会遇到错误和崩溃。当我启动应用程序时,它会自动关闭


这是我的指南:

要在Android中使用
片段
中的
上下文
,您应该首先获得包含当前
片段的
活动的上下文。所以这条线

StringArrayAdapter ad = new StringArrayAdapter(getApplicationContext(), aList, R.layout.list_design, from, to);
应该是:

StringArrayAdapter ad = new StringArrayAdapter(getActivity().getApplicationContext(), aList, R.layout.list_design, from, to);
StringArrayAdapter ad = new StringArrayAdapter(mMyStringArray, getActivity().getApplicationContext());
检查这是否是您的问题,如果不只是发布运行该代码的堆栈跟踪

编辑:

看到适配器的代码,我可以看出
活动中对其进行的初始化不正确。应该是:

StringArrayAdapter ad = new StringArrayAdapter(getActivity().getApplicationContext(), aList, R.layout.list_design, from, to);
StringArrayAdapter ad = new StringArrayAdapter(mMyStringArray, getActivity().getApplicationContext());

其中,
mMyStringArray
是要显示的字符串数组,例如:
TitleList
取决于您的代码。

getApplicationContext()如何?还是活动的上下文?您得到的是哪个错误?对于“getApplicationContext()”我得到的错误是:“MyFragment1类型的方法getApplicationContext()未定义”您可以在定义
StringArrayAdapter
的位置显示整个
活动吗?我已经添加了ActivityDescription资源路径位置类型方法getApplicationContext()未定义Fragment1UG Fragment1UG.java/NewGames/src/com/jg/NewGames第71行java ProblemDescription资源路径位置类型构造函数StringArrayAdapter(上下文、列表、int、字符串[]、int[])的类型是否存在未定义的碎片1ug.java/NewGames/src/com/jg/NewGames第71行java问题我如何发布堆栈跟踪?它是否在设备上构建/运行应用程序之前给您一个错误,或者您的应用程序在成功构建后崩溃?是的//////(抱歉,不能只键入“是”):)所以请在您的问题中发布它的代码。正如我所猜测的,您没有在适配器的构造函数中设置正确的变量。