gpt4 book ai didi

java - 自定义 ArrayAdapter 用于不同文件的自定义微调器

转载 作者:行者123 更新时间:2023-11-30 03:46:09 28 4
gpt4 key购买 nike

我正在为自定义微调器制作自定义 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

预先感谢您的努力。

最佳答案

AddEditIncome 中声明数组:

String[] income_names_array,income_description_array,strings;

但它们为空,因为您实际上没有创建数组。如果您需要使用它们,请妥善制作它们。

实际上,这个问题是因为您使用 null 数组(AddEditIncomestrings 数组变量)调用父类(super class)构造函数.

我注意到你在 MySpinnerAdapterM 中根本没有使用 AddEditIncome.strings,所以你应该通过 MySpinnerAdapterM strings 变量而不是考虑它已初始化:

public MySpinnerAdapterM(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, strings);
//rest of code

这也意味着您可以缩短 MySpinnerAdapterM 的构造函数定义,前提是您始终要使用这些字符串。

关于java - 自定义 ArrayAdapter 用于不同文件的自定义微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14967769/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com