我是android编程的新手,我尝试通过从主活动传递一个微调器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);
}
}
我的主要活动代码:
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);
}
}
我的logcat
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来获得依赖的微调器,这个代码的弱点是spinner可以减少或者从数据库中动态添加,这是我的新代码:
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) {
}
});
}
现在我的微调工作非常完美:),感谢所有回复我问题的人。
答案 0 :(得分:5)
从中删除以下代码:
spinner = new Spinner(context);
并传递xml文件中给出的微调器的id,而不是Integer.valueOf(spinnerID)
spinner = (Spinner) spinnerLayout.findViewById(R.id.SpinnerId);
答案 1 :(得分:1)
这条线似乎是嫌疑人
spinner = (Spinner) spinnerLayout.findViewById(Integer.valueOf(spinnerID)); //give me null
从xml这里给出微调器的id
编辑: 看到你的logcat输出后:
http://code.google.com/p/android/issues/detail?id=9211
检查Activity_Prospect.java:490
中的Log.e(“”,“”)答案 2 :(得分:0)
尝试
context.findViewById(R.id.your_view)