在片段中填充微调器时出错
我有一个包含两个片段的活动。其中一个有一个旋转器。当我填充它时,应用程序崩溃了。 我不知道为什么。在 Android 开发人员中,它很困惑如何在片段中实现它,它似乎与正常活动不同。
谢谢!
public class InstrumentsFrag extends Fragment {
TextView tv1;
Spinner sp;
String[] os = {"Cupcake v1.5", "Donut v1.6", "Éclair v2.0/2.1", "Froyo v2.2",
"Gingerbread v2.2", "Honeycomb v3.0/3.1"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.instruments, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = (Spinner) getActivity().findViewById(
R.id.spinner1);
ArrayAdapter <CharSequence>adapter = ArrayAdapter.createFromResource( getActivity(), R.array.sections , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}
I have a Activity with two fragments. One of them hava a spinner. When I populate it the app crashes.
I don't know why. In android developers it's confused how do it in fragments, it seems is diferent like normal activity.
Thanks!
public class InstrumentsFrag extends Fragment {
TextView tv1;
Spinner sp;
String[] os = {"Cupcake v1.5", "Donut v1.6", "Éclair v2.0/2.1", "Froyo v2.2",
"Gingerbread v2.2", "Honeycomb v3.0/3.1"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.instruments, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = (Spinner) getActivity().findViewById(
R.id.spinner1);
ArrayAdapter <CharSequence>adapter = ArrayAdapter.createFromResource( getActivity(), R.array.sections , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,我的应用程序在填充微调器时崩溃了。就我而言,我填充的微调器是在片段的 XML 文件中定义的,因此技巧是使用 findViewById() 来查找它。我发现在您的情况下,您尝试使用 getActivity():
我还尝试使用 getActivity() 和 getView(),两者都导致崩溃(分别为空微调器和空指针异常)。
我最终通过将 getActivity() 替换为片段的视图来使其工作。我通过在调用 onCreateView() 时填充微调器来做到这一点。以下是我的最终代码中的一些片段:
因此,我将片段视图传递到我的函数中,并引用该视图来配置微调器。效果非常好。 (快速免责声明 - 我自己是 Android 新手,因此如果更有经验的人需要的话,也许上面的一些术语可以得到纠正或澄清。)
希望它有所帮助!
I had the same problem where my app crashed when populating a spinner. In my case, the spinner I was populating was defined in the fragment's XML file, so the trick was getting findViewById() to find it. I see in your case you tried to use getActivity():
I also tried using both getActivity() and getView(), and both caused crashes (null spinner, and NULL Pointer exception respectively).
I finally got this to work by replacing getActivity() with the fragment's view. I did this by populating the spinner when onCreateView() is called. Here are some snippets from my final code:
So I passed the fragment view into my function and referenced that view to configure the spinner. That worked perfectly. (And quick disclaimer - I'm new to Android myself, so perhaps some of the above terminology can be corrected or clarified if needed by more experienced people.)
Hope it helps!