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 8911657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:00:10+00:00 2026-06-15T04:00:10+00:00

My app 2 spinners ,which basically are drop down lists allowing users to select

  • 0

My app 2 spinners ,which basically are drop down lists allowing users to select values. Spinner1 has 4options in dropdown and Spinner2 has 5options in dropdown.

If user selects option1 from spinner1 and option1 from spinner2 , a bulleted list [in the HTML webapp version of this app , i use li tags] should be displayed in a webview in a fragment in rest of the view.The image will make things more clear.
Now every time i try to select values from both the spinners and click submit ,the application crashes.

I am attaching the code below

This is for Startup Activity-

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

    private Spinner spinner1, spinner2;
    private Button btnSubmit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addItemsOnSpinner2();
        addListenerOnButton();
        addListenerOnSpinnerItemSelection();

    }

    //add items into spinner dynamically
    public void addItemsOnSpinner2() {

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        List<String> list = new ArrayList<String>();
        list.add("Equipment");
        list.add("Indications");
        list.add("Illustration");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter);
    }

    public void addListenerOnSpinnerItemSelection(){

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }

    //get the selected dropdown list value
    public void addListenerOnButton() {

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);

        btnSubmit = (Button) findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new OnClickListener() {

            /*@Override
            public void onClick(View v) {

                Toast.makeText(MyAndroidAppActivity.this,
                        "OnClickListener : " + 
                        "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
                        "\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
                        Toast.LENGTH_SHORT).show();
            }*/

             @Override
                public void onClick(View v) {

                     WebViewerFragment wvf = (WebViewerFragment) getFragmentManager().findFragmentById(R.id.webviewfrag);
                       if (wvf != null && wvf.getView() != null) { 
                            wvf.updateWebView(spinner1.getSelectedItemPosition(), spinner2.getSelectedItemPosition());
                       }
                }



        });

    }



}

This is the method for webview

import android.R.string;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Toast;


public class WebViewerFragment extends Fragment {

    private WebView mWebView;
    // a two dimensional array representing the data to put in the WebView
     String mData[][]= new String[4][5]; 
     {
        for (int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                mData[i][j]="<html><body><h1>Temp1</h1></body></html>";

            }
        }
     }

/*     Toast.makeText(this, mData[i][j],Toast.LENGTH_SHORT);   
*/     



 /*    int[][] a1 = {
              { 1, 2, 3, },
              { 4, 5, 6, },
            };
   {  {},{}};


  */

/*     String SurgEqui="<html><body><h2>Equipment</h2><ul><li><p>IV catheter :Be certain that IV is in place and flushing easily</p></li></body><html>";
*/           







    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mainView = (View) inflater.inflate(R.layout.frag_layout, container, false);
        mWebView = (WebView) mainView.findViewById(R.id.webView1); 
        return mainView;
    }

    public void updateWebView(int firstSelection, int secondSelection) {
         if (firstSelection == AdapterView.INVALID_POSITION || secondSelection == AdapterView.INVALID_POSITION) {
              return;  
         } else {
              if (mWebView != null) {
                  mWebView.loadData(mData[firstSelection][secondSelection].replace("+", "%20"), "text/html", "utf-8");
                  // mWebView.loadData(mData[firstSelection][secondSelection], "text/html", null);
                   //wv.loadData(URLEncoder.encode(myString).replace("+", "%20"), "text/html, "utf-8");

              }
         }

    }


}

This is the layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     >

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="101dp"
        android:text="@string/submit" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/country_arrays" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/spinner1" />

    <fragment
        android:id="@+id/webviewfrag"
        android:name="android.webkit.WebViewFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnSubmit"
        android:layout_marginLeft="246dp"
        android:layout_marginTop="244dp"
       />







    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btnSubmit" />

</RelativeLayout>
  • 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-15T04:00:12+00:00Added an answer on June 15, 2026 at 4:00 am

    You should include the logcat with the error. However there are two issues I have noticed:

    1. You initialized spinner2 inside addItemsOnSpinner2() and then you call addListenerOnButton() where you have overwritten spinner2. That probably results with NullPointerException
    2. You call addListenerOnSpinnerItemSelection() after calling addListenerOnButton() where you have referenced to spinner1 which is instantieted in addListenerOnButton(). That will also give you nullPointerException.
      To fix it try:

    1.Remove:

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    

    from addListenerOnButton() method.

    2.Call addListenerOnSpinnerItemSelection() before addListenerOnButton():

    addListenerOnSpinnerItemSelection();
    addListenerOnButton();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app, I insert pairs of spinners into a linearlayout, which is inside
In my app I'm using a spinners to let the user select an item
I am developing an android app in which I have 3 spinners populated from
I am working on an app that has multiple edittext fields and spinners and
Im going to write some android app, which will basically consists of two activities.
I am creating an app which has two navigation bars at the top: previous
I've got an app with three spinners, they have unique IDs and I'm trying
Ok so I am working on an android app and I have implanted spinners...
So basically my app starts out with a TabView, then through the options menu
I'm trying to do an app with listView, which is need to be filled

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.