所以这是我的fPretraga课程:
public class FragmentPretraga extends android.app.Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.izgled_pretraga, container, false);
}
}
这就是功能
try
{
Spinner oblasti = (Spinner) fPretraga.getView().findViewById(R.id.spinnerOblasti);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.oblasti, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
oblasti.setAdapter(adapter);
EditText sranje = (EditText) fPretraga.getView().findViewById(R.id.editBrojDjaka);
sranje.setText("0");
}
catch(NullPointerException e)
{
Log.e("LOAD_TAG","Šta se desilo?",e);
}
该函数应该加载变量(在这种情况下,使用预设的字符串值数组来膨胀Spinner,并将一个文本框的文本设置为其默认的“0”值)。我的假设是NPE被抛出,因为getView()没有返回我期望的View,即我写的onCreateView是错误的。因为id“spinnerOblasti”存在于“izgled_pretraga.xml”文件中。有什么想法吗? 提前谢谢。
LogCat日志:
04-16 14:17:19.866:D / gralloc_goldfish(541):未检测到GPU仿真的仿真器。
04-16 14:18:50.356:E / LOAD_TAG(541):Štase desilo?
04-16 14:18:50.356:E / LOAD_TAG(541):java.lang.NullPointerException
04-16 14:18:50.356:E / LOAD_TAG(541):at com.markosoft.maturko.Maturko.UcitajPromenljive(Maturko.java:214)
04-16 14:18:50.356:E / LOAD_TAG(541):at com.markosoft.maturko.Maturko $ MyTabListener.onTabSelected(Maturko.java:181)
04-16 14:18:50.356:E / LOAD_TAG(541):at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:512)
04-16 14:18:50.356:E / LOAD_TAG(541):at com.android.internal.app.ActionBarImpl $ TabImpl.select(ActionBarImpl.java:893)
04-16 14:18:50.356:E / LOAD_TAG(541):at com.android.internal.widget.ScrollingTabContainerView $ TabClickListener.onClick(ScrollingTabContainerView.java:488)
04-16 14:18:50.356:E / LOAD_TAG(541):在android.view.View.performClick(View.java:3480)
04-16 14:18:50.356:E / LOAD_TAG(541):在android.view.View $ PerformClick.run(View.java:13983)
04-16 14:18:50.356:E / LOAD_TAG(541):在android.os.Handler.handleCallback(Handler.java:605)
04-16 14:18:50.356:E / LOAD_TAG(541):在android.os.Handler.dispatchMessage(Handler.java:92)
04-16 14:18:50.356:E / LOAD_TAG(541):在android.os.Looper.loop(Looper.java:137)
04-16 14:18:50.356:E / LOAD_TAG(541):在android.app.ActivityThread.main(ActivityThread.java:4340)
04-16 14:18:50.356:E / LOAD_TAG(541):at java.lang.reflect.Method.invokeNative(Native Method)
04-16 14:18:50.356:E / LOAD_TAG(541):at java.lang.reflect.Method.invoke(Method.java:511)
04-16 14:18:50.356:E / LOAD_TAG(541):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
04-16 14:18:50.356:E / LOAD_TAG(541):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-16 14:18:50.356:E / LOAD_TAG(541):at dalvik.system.NativeStart.main(Native Method)
04-16 14:18:50.746:D / dalvikvm(541):GC_CONCURRENT释放214K,4%免费10102K / 10439K,暂停3ms + 4ms
答案 0 :(得分:0)
在onCreateView中,你必须这样做:
View view = inflater.inflate(R.layout.izgled_pretraga, null);
oblasti =(Spinner) view.findViewById(R.id.spinnerOblasti);
return view;
然后你可以到处调用旋转器......