操作栏中的微调器:setAdapter上的NullPointerException(Android - Java)

时间:2013-04-17 18:32:26

标签: java android nullpointerexception spinner

我正在尝试将图像+文本放在Spinner中,但我在setAdapter上有一个NullPointerException。 这是整个下午,我正在努力解决这个问题,但我无法弄清楚问题出在哪里。 非常感谢帮助!这是代码:

RES /菜单/ 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 /布局/ 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 /布局/ 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 /包/ 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 /包/ 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

2 个答案:

答案 0 :(得分:1)

<强>首先,

删除ArrayAdapter的通用参数,即ArrayAdapter<String>删除<String>,并直接指定值而不是克隆

<强>第二

您的spinnerCategories9个元素,而spinnerImages8,因此适配器的长度为9,当您尝试读取您将获得的8索引值

ArrayIndexOutOfBoundsException

remove spinnerCategories一个元素来自addspinnerImages一个图像到{{1}}

答案 1 :(得分:0)

如果你想在操作栏中使用动作布局的微调器,你需要阅读onCreateOptionsMenu中的微调器。

可能是这样的

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    View v = (View) menu.findItem(R.id.action_bar_main).getActionView();

    Spinner spinner= ( Spinner) v.findViewById(R.id.spinnerActionBar);
}

看看这个Sample