1
我正在爲自定義微調器自定義ArrayAdapter。 我使我的自定義ArrayAdapter在一個不同的文件(以保持良好的組織),但我得到一個錯誤。自定義ArrayAdapter自定義微調器在不同的文件
這是我的活動它有微調:
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
提前感謝您的努力。
我初始化字符串數組,但仍然有錯誤..我不能寫超(上下文,textViewResourceId,字符串);因爲在字符串我得到一個錯誤 – 2013-02-19 21:42:02
@ N.K。什麼是錯誤? – 2013-02-19 21:43:51
02-19 23:45:40.862:E/AndroidRuntime(25873):致命例外:主 02-19 23:45:40.862:E/AndroidRuntime(25873):java.lang.NullPointerException 02-19 23:45 :40.862:E/AndroidRuntime(25873):\t at development.nk.esoda.AddEditIncome $ 2.onItemSelected(AddEditIncome.java:66) 02-19 23:45:40.862:E/AndroidRuntime(25873):\t at android。 Widget.AdapterView.access $ 200(AdapterView.java:42) – 2013-02-19 21:48:10