我在我的中使用了一个微调器ListActivity
并获得了一个NullPointerException
atsetAdapter
方法。我试图在 google 和 stackoverflow 上研究类似的问题,并尝试了他们的建议,但没有用。但是,此代码适用于我的另一个应用程序。
options = getResources().getStringArray(R.array.options_array);
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, options);
s1.setAdapter(adapter); // Here is the error
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected item : " + options[index],
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
我的微调器 xml
<Spinner
android:id="@+id/spinner1"
android:layout_width="25dip"
android:layout_height="25dip"
android:entries="@array/options_array"
android:prompt="@string/spin_prompt"
android:background="@drawable/expander_ic_maximized"
android:layout_alignParentRight="true"
android:layout_below="@id/name_text"
/>
日志猫
05-04 09:23:48.492: ERROR/AndroidRuntime(343): Caused by: java.lang.NullPointerException
05-04 09:23:48.492: ERROR/AndroidRuntime(343): at com.amannain.android.missedcalls.MissedCallsSpinActivity.runSpinner(MissedCallsSpinActivity.java:31)
05-04 09:23:48.492: ERROR/AndroidRuntime(343): at com.amannain.android.missedcalls.MissedCallsSpinActivity.onCreate(MissedCallsSpinActivity.java:21)