使用字符串数组中的微调器:NullPointer 异常
Posted
技术标签:
【中文标题】使用字符串数组中的微调器:NullPointer 异常【英文标题】:Using Spinner from String Array:NullPointer Exception 【发布时间】:2015-11-01 12:12:44 【问题描述】:我是 android 新手,不太了解。请帮我解决这个问题
I have a spinner which goes to another class when an option is selected and then clicked on a button.我在strings.xml
file 中给出了字符串数组。
<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):在 com.naysotech.smartrajagiri.CalendarFragment.onCreateView(CalendarFragment.java:58) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.app.Fragment.performCreateView(Fragment.java:1700) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.app.BackStackRecord.run(BackStackRecord.java:684) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.os.Handler.handleCallback(Handler.java:733) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.os.Handler.dispatchMessage(Handler.java:95) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 android.os.Looper.loop(Looper.java:136) 08-09 09:14:30.857: E/Android 运行时(1728):在 android.app.ActivityThread.main(ActivityThread.java:5017) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 java.lang.reflect.Method.invokeNative(Native Method) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 java.lang.reflect.Method.invoke(Method.java:515) 08-09 09:14:30.857: E/Android 运行时(1728):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-09 09:14:30.857:E/AndroidRuntime(1728):在 dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
【参考方案1】:您应该启动微调器:
spinner1 = (Spinner)rootView.findViewById(R.id.spinner1);
在您尝试为其设置适配器之前:
spinner1.setAdapter(dataAdapter);
【讨论】:
以上是关于使用字符串数组中的微调器:NullPointer 异常的主要内容,如果未能解决你的问题,请参考以下文章