1

i'm new in android programming, i'm trying make dynamic depended spinner by pass a spinner id from main activity and set id to be recalled, i got a trouble with null pointer findviewbyid when recall child spinner, sorry for my mess words and code. Thank you for any help

here is my code:

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);
    }

}

my main activity code :

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);                  
            }
        } 

my 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)

i feel enough struggling for this problem, i changed to read static xml to get depended spinner, weakness of this code is spinner can't less or added dynamically from database, here is my new code :

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) {

        }

    });

}

now my spinner work perfect :), thank you to all you guys who has response my question.

4

3 回答 3

5

Remove below code from it:

spinner = new Spinner(context);

and pass id of spinner given in xml file instead of Integer.valueOf(spinnerID)

spinner = (Spinner) spinnerLayout.findViewById(R.id.SpinnerId);
于 2012-07-16T11:37:11.950 回答
1

This line seems to be the suspect

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

give the id of the spinner from xml here

EDIT: after seeing your logcat output:

http://code.google.com/p/android/issues/detail?id=9211

check your Log.e("", "") in Activity_Prospect.java:490

于 2012-07-16T11:38:37.970 回答
0

Try

context.findViewById(R.id.your_view)
于 2012-07-16T11:36:56.277 回答