Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8238633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:52:44+00:00 2026-06-07T19:52:44+00:00

i’m new in android programming, i’m trying make dynamic depended spinner by pass a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:52:46+00:00Added an answer on June 7, 2026 at 7:52 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.