我正在尝试将图像 + 文本放在 Spinner 中,但我在 setAdapter 上有一个 NullPointerException。整个下午我都在试图解决这个问题,但我无法弄清楚问题出在哪里。帮助将不胜感激!这是代码:
res/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_bar_main"
android:title="@string/action_bar_main_add"
android:showAsAction="always|withText"
android:actionLayout="@layout/action_bar_main"
/>
</menu>
res/layout/action_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dip">
<Spinner
android:id="@+id/spinnerActionBar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/btnActionBarMainAdd"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="@string/action_bar_main_add"
android:drawableRight="@drawable/plus_32x32"
android:drawablePadding="4dp"
android:textColor="@color/green"
android:layout_alignParentRight="true"/>
</RelativeLayout>
res/layout/action_bar_spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="@+id/imgSpinnerActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip" />
<TextView
android:id="@+id/txtSpinnerActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="10dip"/>
</LinearLayout>
src/package/Events.java
public class Events extends Activity {
final String[] spinnerCategories = {
"Events",
"Calendar",
"Map",
"Notes",
"Metronome",
"Gallery",
"Pay",
"Recorder",
"Tuner"};
final Integer[] spinnerImages = {
R.drawable.pen_32x32,
R.drawable.calendar_32x32,
R.drawable.map_pin_20x32,
R.drawable.document_24x32,
R.drawable.equalizer_32x32,
R.drawable.camera_32x32,
R.drawable.money_48x48,
R.drawable.note_48x48 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("DEBUG","--> Entrato in Events.onCreate()");
// ACTIONBAR: Customizza lo Spinner aggiungendo immagine + testo
Spinner mySpinner = (Spinner)findViewById(R.id.spinnerActionBar);
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, R.layout.action_bar_spinner_row, spinnerCategories, spinnerImages);
mySpinner.setAdapter(adapter);
Log.d("DEBUG","<-- Uscendo da Events.onCreate()");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// ACTIONBAR: Nasconde il titolo e l'icona
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowCustomEnabled(true);
// ACTIONBAR: Aggiunge oggetti all'actionbar, se è presente
MenuInflater mMenuInflater = getMenuInflater();
mMenuInflater.inflate(R.menu.main, menu);
return true;
}
}
src/package/CustomSpinnerAdapter.java
public class CustomSpinnerAdapter extends ArrayAdapter<String>{
String[] spinnerCategories;
Integer[] spinnerImages;
LayoutInflater inflater;
public CustomSpinnerAdapter(Context context, int textViewResourceId,
String[] spinnerCategories, Integer[] spinnerImages) {
super(context, textViewResourceId, spinnerCategories);
Log.d("DEBUG","--> Entrato nel costruttore di CustomSpinnerAdapter");
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.spinnerCategories = spinnerCategories.clone();
this.spinnerImages = spinnerImages.clone();
Log.d("DEBUG","<-- Uscendo dal costruttore di CustomSpinnerAdapter");
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
Log.d("DEBUG","--> Entrato in CustomSpinnerAdapter.getCustomView()");
View row = inflater.inflate(R.layout.action_bar_spinner_row, parent, false);
TextView label=(TextView)row.findViewById(R.id.txtSpinnerActionBar);
label.setText(spinnerCategories[position]);
ImageView icon=(ImageView)row.findViewById(R.id.imgSpinnerActionBar);
icon.setImageResource(spinnerImages[position]);
Log.d("DEBUG","<-- Uscendo da CustomSpinnerAdapter.getCustomView()");
return row;
}
}
编辑:我忘了发布控制台输出:
04-17 20:10:11.414: I/Process(22701): Sending signal. PID: 22701 SIG: 9
04-17 20:10:43.264: E/Trace(22838): error opening trace file: No such file or directory (2)
04-17 20:10:43.314: D/DEBUG(22838): --> Entrato in Events.onCreate()
04-17 20:10:43.324: D/DEBUG(22838): --> Entrato nel costruttore di CustomSpinnerAdapter
04-17 20:10:43.324: D/DEBUG(22838): <-- Uscendo dal costruttore di CustomSpinnerAdapter
04-17 20:10:43.324: D/AndroidRuntime(22838): Shutting down VM
04-17 20:10:43.324: W/dalvikvm(22838): threadid=1: thread exiting with uncaught exception (group=0x415d5930)
04-17 20:10:43.324: E/AndroidRuntime(22838): FATAL EXCEPTION: main
04-17 20:10:43.324: E/AndroidRuntime(22838): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.romagnoli.andrea.myrehearsals/it.romagnoli.andrea.myrehearsals.Events}: java.lang.NullPointerException
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.os.Looper.loop(Looper.java:137)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-17 20:10:43.324: E/AndroidRuntime(22838): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 20:10:43.324: E/AndroidRuntime(22838): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 20:10:43.324: E/AndroidRuntime(22838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-17 20:10:43.324: E/AndroidRuntime(22838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-17 20:10:43.324: E/AndroidRuntime(22838): at dalvik.system.NativeStart.main(Native Method)
04-17 20:10:43.324: E/AndroidRuntime(22838): Caused by: java.lang.NullPointerException
04-17 20:10:43.324: E/AndroidRuntime(22838): at it.romagnoli.andrea.myrehearsals.Events.onCreate(Events.java:43)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.app.Activity.performCreate(Activity.java:5104)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-17 20:10:43.324: E/AndroidRuntime(22838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-17 20:10:43.324: E/AndroidRuntime(22838): ... 11 more