gpt4 book ai didi

android - 空指针 findviewbyid

转载 作者:行者123 更新时间:2023-11-29 14:53:36 25 4
gpt4 key购买 nike

我是 Android 编程的新手,我正在尝试通过从主要 Activity 传递微调器 ID 并设置要调用的 ID 来制作动态依赖微调器,当调用子微调器时我遇到了空指针 findviewbyid 的问题,对不起我的乱码和乱码。感谢您的任何帮助

这是我的代码:

public class PickOne extends LinearLayout {
String tag = PickOne.class.getName();
TextView label;
ArrayAdapter<String> aa;
Spinner spinner;
LinearLayout ll;
String title;
Spinner spChild;

public PickOne(Context context,DataSource datasource,SQLiteDatabase db,String labelText,String options,String spinnerID,String parentID,String childID) {
super(context);

View spinnerLayout = View.inflate(context, R.layout.dummy, null);
ll = (LinearLayout) spinnerLayout.findViewById(R.id.dummylayout);

//ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);

ll.setLayoutParams(params);
ll.setPadding(4, 10, 4, 0);

if(parentID.length() > 0){

spinner = new Spinner(context);
spinner = (Spinner) spinnerLayout.findViewById(Integer.valueOf(spinnerID)); //give me null
spinnerInitialized(context,spinner);

if(spinner == null){
Log.i("!!!!!!!!!!!!!!","null.................");
}

label = new TextView(context);
label.setText(labelText);
label.setTextColor(getResources().getColor(R.color.label_color));

title = label.getText().toString();
spinner.setPrompt(title);

}else{
System.out.println("in parentID....." + String.valueOf(parentID));

label = new TextView(context);
label.setText(labelText);
label.setTextColor(getResources().getColor(R.color.label_color));

spinner = new Spinner(context);

options = "Select One|" + options;
String []opts = options.split("\\|");

aa = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,opts);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
spinner.setAdapter(aa);

title = label.getText().toString();
spinner.setPrompt(title);

ll.addView(label);
ll.addView(spinner);

if(childID.length()>0){
spChild = new Spinner(context);
spinnerInitialized(context,spChild);

spChild.setId(Integer.valueOf(childID));//set id to be recalled
spChild.setEnabled(false);
ll.addView(spChild);

spinnerOnItemSelectedListener(context,datasource,db,"assetbrand", "assettype", spinner, spChild);
}
Log.i("childID........",String.valueOf(spChild.getId()));
this.addView(spinnerLayout);
}

}

我的主要 Activity 代码:

int i;
for (i=0;i<theForm.fields.size();i++) {
theForm.fields.elementAt(i).obj = new PickOne(context,datasource,db,(" " + theForm.fields.elementAt(i).getLabel()),theForm.fields.elementAt(i).getOptions(),theForm.fields.elementAt(i).getSpinnerID(),theForm.fields.elementAt(i).getParentID(),theForm.fields.elementAt(i).getChildID());
panel.addView((View) theForm.fields.elementAt(i).obj);
}
}

我的日志

07-16 16:59:07.680: E/AndroidRuntime(1755): FATAL EXCEPTION: main
07-16 16:59:07.680: E/AndroidRuntime(1755): java.lang.NullPointerException: println needs a message
07-16 16:59:07.680: E/AndroidRuntime(1755): at android.util.Log.println_native(Native Method)
07-16 16:59:07.680: E/AndroidRuntime(1755): at android.util.Log.e(Log.java:231)
07-16 16:59:07.680: E/AndroidRuntime(1755): at com.technotalkative.viewstubdemo.Activity_Prospect.DisplayForm(Activity_Prospect.java:490)
07-16 16:59:07.680: E/AndroidRuntime(1755): at com.technotalkative.viewstubdemo.Activity_Prospect.access$0(Activity_Prospect.java:388)
07-16 16:59:07.680: E/AndroidRuntime(1755): at com.technotalkative.viewstubdemo.Activity_Prospect$1.handleMessage(Activity_Prospect.java:222)
07-16 16:59:07.680: E/AndroidRuntime(1755): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 16:59:07.680: E/AndroidRuntime(1755): at android.os.Looper.loop(Looper.java:137)
07-16 16:59:07.680: E/AndroidRuntime(1755): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-16 16:59:07.680: E/AndroidRuntime(1755): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:59:07.680: E/AndroidRuntime(1755): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:59:07.680: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 16:59:07.680: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 16:59:07.680: E/AndroidRuntime(1755): at dalvik.system.NativeStart.main(Native Method)

我觉得这个问题已经够挣扎了,我改为读取静态 xml 以获得依赖的微调器,这段代码的弱点是微调器不能少或从数据库动态添加,这是我的新代码:

View spinnerLayout = View.inflate(context, R.layout.spinner, null);
panel.addView(spinnerLayout);

TextView tv1 = (TextView) spinnerLayout.findViewById(R.id.tv1);
tv1.setText("Asset Type");

final TextView tv2 = (TextView) spinnerLayout.findViewById(R.id.tv2);
tv2.setText("Asset Brand");

TextView tv3 = (TextView) spinnerLayout.findViewById(R.id.tv3);
tv3.setText("Asset Detail");

Spinner spinner1 = (Spinner) spinnerLayout.findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner) spinnerLayout.findViewById(R.id.spinner2);
final Spinner spinner3 = (Spinner) spinnerLayout.findViewById(R.id.spinner3);

spinner1.setPrompt(tv1.getText());
spinner2.setPrompt(tv2.getText());
spinner3.setPrompt(tv3.getText());

String sqlquery;
sqlquery = "select distinct assettype from asset";

spinnerInitialized(spinner1,sqlquery);
spinnerInitialized(spinner2,"");
spinnerInitialized(spinner3,"");

spinnerOnItemSelectedListener("assetbrand", "assettype", spinner1, spinner2);
spinnerOnItemSelectedListener("assetdetail", "assetbrand", spinner2, spinner3);

private void spinnerInitialized(Spinner resource, String sqlquery){
ArrayAdapter<String> da;
String options;
options = "Select One|";
String []opts = options.split("\\|");

if(sqlquery.length()>0){
List<String> lables = datasource.getAllLabels(db, sqlquery);
da = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables);
}else{
resource.setEnabled(false);
da = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, opts);
}

da.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
resource.setAdapter(da);

}

private void spinnerOnItemSelectedListener(final String field, final String wherefield, final Spinner resource, final Spinner destination){

resource.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

String item = (String) parent.getItemAtPosition(pos);
String sqlquery;

sqlquery = "select distinct " + field + " from asset where " + wherefield + " = '" + item + "'";

List<String> lables = datasource.getAllLabels(db, sqlquery);

ArrayAdapter<String> aa = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, lables);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
destination.setAdapter(aa);
spinnerPosition = aa.getPosition("Select One");
destination.setSelection(spinnerPosition);

aa.notifyDataSetChanged();

if(!item.contains("Select One")){
destination.setEnabled(true);
}else{
destination.setEnabled(false);
}

}

@Override
public void onNothingSelected(AdapterView<?> parentView) {

}

});

}

现在我的微调器工作完美:),感谢所有回答我问题的人。

最佳答案

从中删除以下代码:

spinner = new Spinner(context);

并传递 xml 文件中给定的微调器 ID 而不是 Integer.valueOf(spinnerID)

spinner = (Spinner) spinnerLayout.findViewById(R.id.SpinnerId);

关于android - 空指针 findviewbyid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11503408/

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