我正在为自定义微调器创建一个自定义ArrayAdapter。 我在不同的文件中创建自定义ArrayAdapter(以保持井井有条)但我收到了错误。
这是我的活动,其中包含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
提前感谢您的努力。
答案 0 :(得分:1)
在AddEditIncome
中声明数组:
String[] income_names_array,income_description_array,strings;
但它们是null,因为你没有实际制作数组。如果你需要使用它们,请正确制作它们。
但实际上,这个问题是因为您使用null
数组(AddEditIncome
的{{1}}数组变量调用超类构造函数)。
我注意到您在strings
中根本没有使用AddEditIncome.strings
,因此您应该假设MySpinnerAdapterM
的{{1}}变量,而不是考虑初始化:
MySpinnerAdapterM
这也意味着你可以缩短strings
的构造函数定义,前提是你总是要使用这些字符串。