我正在为自定义微调器制作自定义 ArrayAdapter。我在不同的文件中制作了我的自定义 ArrayAdapter(以保持井井有条),但出现错误。
这是我的 Activity,它有 Spinner:
public class AddEditIncome extends Activity
{
private long rowID; // id of income being edited, if any
String[] income_names_array,income_description_array,strings;
//called when the Activity is first started
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); // call super's onCreate
setContentView(R.layout.add_income);
// --- setting spinner for income type ---
Spinner s1=(Spinner)findViewById(R.id.spinnerInCategory);
ArrayAdapter<String> adapter = new MySpinnerAdapterM(AddEditIncome.this, R.layout.spinner_row, strings);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
int index = arg0.getSelectedItemPosition();
inCategory=income_names_array[index];
}
@Override
public void onNothingSelected(AdapterView<?> arg0) { }
});
// --- End of setting spinner---
这是我在另一个文件中的自定义 ArrayAdapter:
public class MySpinnerAdapterM extends ArrayAdapter<String> {
Context mContext;
int mResource;
String[] list;
String[] strings = {"Salary","Gift","Rent","Freelancer"};
String[] subs = {"Your monthly salary","Its nice to get such gifts..",
"Money from renting house", "Thats a difficult job"};
int arr_images[] =
{ R.drawable.salary,R.drawable.gift, R.drawable.rent,R.drawable.freelancer};
public MySpinnerAdapterM(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
this.mContext=context;
this.mResource=textViewResourceId;
this.list=objects;
}
@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) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
View row=inflater.inflate(R.layout.spinner_row, parent, false);
TextView label=(TextView)row.findViewById(R.id.company);
label.setText(strings[position]);
TextView sub=(TextView)row.findViewById(R.id.sub);
sub.setText(subs[position]);
ImageView icon=(ImageView)row.findViewById(R.id.spinner_image);
icon.setImageResource(arr_images[position]);
return row;
}
}
我得到那个错误日志:
02-19 23:24:45.212: E/AndroidRuntime(25529): FATAL EXCEPTION: main
02-19 23:24:45.212: E/AndroidRuntime(25529): java.lang.RuntimeException: Unable to start activity ComponentInfo{development.nk.esoda/development.nk.esoda.AddEditIncome}: java.lang.NullPointerException
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.os.Looper.loop(Looper.java:123)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-19 23:24:45.212: E/AndroidRuntime(25529): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 23:24:45.212: E/AndroidRuntime(25529): at java.lang.reflect.Method.invoke(Method.java:521)
02-19 23:24:45.212: E/AndroidRuntime(25529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-19 23:24:45.212: E/AndroidRuntime(25529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-19 23:24:45.212: E/AndroidRuntime(25529): at dalvik.system.NativeStart.main(Native Method)
02-19 23:24:45.212: E/AndroidRuntime(25529): Caused by: java.lang.NullPointerException
02-19 23:24:45.212: E/AndroidRuntime(25529): at java.util.Arrays$ArrayList.<init>(Arrays.java:49)
02-19 23:24:45.212: E/AndroidRuntime(25529): at java.util.Arrays.asList(Arrays.java:171)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
02-19 23:24:45.212: E/AndroidRuntime(25529): at development.nk.esoda.MySpinnerAdapterM.<init>(MySpinnerAdapterM.java:24)
02-19 23:24:45.212: E/AndroidRuntime(25529): at development.nk.esoda.AddEditIncome.onCreate(AddEditIncome.java:57)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-19 23:24:45.212: E/AndroidRuntime(25529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-19 23:24:45.212: E/AndroidRuntime(25529): ... 11 more
预先感谢您的努力。