我是Android新手,不太了解。请帮我解决这个问题
我有一个微调器,当选择一个选项然后单击一个按钮时,它转到另一个类。我在strings.xml
文件中给出了字符串数组。
<string-array name="month_arrays">
<item>2015 June</item>
<item>2015 July</item>
<item>2015 August</item>
<item>2015 September</item>
<item>2015 October</item>
<item>2015 November</item>
<item>2015 December</item>
<item>2016 January</item>
<item>2016 February</item>
<item>2016 March</item>
</string-array>
我在片段类中使用它并在ArrayList中使用getStringArrays()
,然后将其存储在ArrayAdapter中。
public class CalendarFragment extends Fragment {
public CalendarFragment(){}
TextView t1;
TextView t2;
Spinner spinner1;
Button btnSubmit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
t1=(TextView)rootView.findViewById(R.id.textView1);
t2=(TextView)rootView.findViewById(R.id.textView2);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "font.otf");
t1.setTypeface(font);
t2.setTypeface(font);
String[] regionsArray = getResources().getStringArray(R.array.month_arrays);
ArrayList<String> list = new ArrayList<String>(Arrays.asList(regionsArray));
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1 = (Spinner)rootView.findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
btnSubmit = (Button)rootView.findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),
"OnClickListener : " +
"\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()), // This is Testing if it gets Selected
Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
但是当我进入此菜单并且应用程序获得强制关闭时,我收到NullPointer异常。
logcat的
08-09 09:14:30.857:E / AndroidRuntime(1728):致命异常:主要 08-09 09:14:30.857:E / AndroidRuntime(1728):流程: com.naysotech.smartrajagiri,PID:1728 08-09 09:14:30.857: E / AndroidRuntime(1728):java.lang.NullPointerException 08-09 09:14:30.857:E / AndroidRuntime(1728):at com.naysotech.smartrajagiri.CalendarFragment.onCreateView(CalendarFragment.java:58) 08-09 09:14:30.857:E / AndroidRuntime(1728):at android.app.Fragment.performCreateView(Fragment.java:1700)08-09 09:14:30.857:E / AndroidRuntime(1728):at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 08-09 09:14:30.857:E / AndroidRuntime(1728):at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 08-09 09:14:30.857:E / AndroidRuntime(1728):at android.app.BackStackRecord.run(BackStackRecord.java:684)08-09 09:14:30.857:E / AndroidRuntime(1728):at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 08-09 09:14:30.857:E / AndroidRuntime(1728):at android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:443)08-09 09:14:30.857:E / AndroidRuntime(1728):at android.os.Handler.handleCallback(Handler.java:733)08-09 09:14:30.857:E / AndroidRuntime(1728):at android.os.Handler.dispatchMessage(Handler.java:95)08-09 09:14:30.857:E / AndroidRuntime(1728):at android.os.Looper.loop(Looper.java:136)08-09 09:14:30.857: E / AndroidRuntime(1728):at android.app.ActivityThread.main(ActivityThread.java:5017)08-09 09:14:30.857:E / AndroidRuntime(1728):at java.lang.reflect.Method.invokeNative(Native Method)08-09 09:14:30.857:E / AndroidRuntime(1728):at java.lang.reflect.Method.invoke(Method.java:515)08-09 09:14:30.857: E / AndroidRuntime(1728):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 08-09 09:14:30.857:E / AndroidRuntime(1728):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)08-09 09:14:30.857:E / AndroidRuntime(1728):at dalvik.system.NativeStart.main(原生方法)
答案 0 :(得分:0)
你应该启动微调器:
spinner1.setAdapter(dataAdapter);
在尝试为其设置适配器之前:
table