问题
I have a spinner with a custom adapter that I used to set the height of the first element in the spinner drop down to zero. I do this to have a default message in my spinner (first element) without the user being able to click it, since it's not visible.
package org.digitalhealthagency.elaj.util;
import java.util.ArrayList;
import org.digitalhealthagency.elaj.gui.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter implements SpinnerAdapter{
Context context;
int textViewResourceId;
ArrayList arrayList;
public CustomAdapter(Context context, int textViewResourceId, ArrayList arrayList) {
super(context, textViewResourceId, arrayList);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.arrayList = arrayList;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
convertView = vi.inflate(R.layout.spinner_item, null);
}
TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
textView.setText(arrayList.get(position).toString());//after changing from ArrayList<String> to ArrayList<Object>
if (position == 0) {
textView.setHeight(0);
}
else{
textView.setHeight(100);
}
return convertView;
}
}
In the getDropDownView
I am trying to inflate with spinner_item.xml instead of android.R.layout.simple_spinner_dropdown_item
. My spinner_item looks as follows,
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
android:gravity="right|center_vertical"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingRight="10dp"
android:ellipsize="marquee" />
And in my activity code, I use the adapter as follows,
CustomAdapter adapter = new CustomAdapter(this, R.layout.spinner, subSpecialities);
adapter.setDropDownViewResource(R.layout.spinner_item);
mSubSpecialtySpinner.setAdapter(adapter);
I get the following error in my log cat,
07-10 10:49:34.103: E/AndroidRuntime(518): FATAL EXCEPTION: main
07-10 10:49:34.103: E/AndroidRuntime(518): java.lang.NullPointerException
07-10 10:49:34.103: E/AndroidRuntime(518): at org.digitalhealthagency.elaj.util.CustomAdapter.getDropDownView(CustomAdapter.java:40)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:569)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:565)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.ListView.onMeasure(ListView.java:1155)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-10 10:49:34.103: E/AndroidRuntime(518): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.os.Looper.loop(Looper.java:137)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-10 10:49:34.103: E/AndroidRuntime(518): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 10:49:34.103: E/AndroidRuntime(518): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 10:49:34.103: E/AndroidRuntime(518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-10 10:49:34.103: E/AndroidRuntime(518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-10 10:49:34.103: E/AndroidRuntime(518): at dalvik.system.NativeStart.main(Native Method)
Could anyone please help me?
回答1:
textView is null so NPE
..change it in R.id.text1
instead of android.R.id.text1
TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
^^^^^^^^
来源:https://stackoverflow.com/questions/11410028/android-custom-adapter-with-custom-spinner-drop-down-xml-layout-giving-error