简体   繁体   中英

NullPointerException when setting the Event Handler for Spinner

I am having some issues with the android Spinner. Please have a look at the below codes.

talk_settings.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:text="@string/language_locale"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/language_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_toRightOf="@+id/textView1" 
        android:layout_alignBaseline="@+id/textView1"
        android:entries="@array/locale_arrays"
        android:prompt="@string/locate_prompt"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="37dp"
        android:text="@string/pitch" 
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/pitchBar" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"        
        android:layout_below="@+id/textView2"
        android:layout_marginTop="16dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/pitchBar"
        android:layout_marginTop="27dp"
        android:text="@string/speed"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/speedBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="18dp" />

</RelativeLayout>

locale_string.xml (string resources for the spinner)

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="locate_prompt">Select Language</string>

    <string-array name = "locale_arrays">
        <item>English</item>
        <item>Chinese</item>
        <item>French</item>
        <item>Germany</item>
        <item>Italian</item>
        <item>Japanese</item>
        <item>Korean</item>
    </string-array>

</resources>

Java Code

//Event Handler for the language spinner
    private class LanguageSpinnerHandler implements OnItemSelectedListener
    {
        int result = 0;;

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

            if(parent.getItemAtPosition(position).toString()=="English")
            {
                result = tts.setLanguage(Locale.UK);
            }
            else if(parent.getItemAtPosition(position).toString()=="Chinese")
            {
                result = tts.setLanguage(Locale.CHINESE);
            }
            else if(parent.getItemAtPosition(position).toString()=="French")
            {
                result = tts.setLanguage(Locale.FRENCH);
            }
            else if(parent.getItemAtPosition(position).toString()=="Germany")
            {
                result = tts.setLanguage(Locale.GERMANY);
            }
            else if(parent.getItemAtPosition(position).toString()=="Italian")
            {
                result = tts.setLanguage(Locale.ITALIAN);
            }
            else if(parent.getItemAtPosition(position).toString()=="Japanese")
            {
                result = tts.setLanguage(Locale.JAPANESE);
            }
            else
            {
                result = tts.setLanguage(Locale.KOREAN);
            }


            if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
            {
                Toast.makeText(Talk.this, "This Language is Not Supported in Your Device", Toast.LENGTH_LONG).show();
                tts.setLanguage(Locale.UK);
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    }

//show settings
    private void showSettings()
    {
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.talk_settings);
        dialog.setTitle("Settings");
        dialog.setCancelable(true);

        SeekBar pitchBar = (SeekBar)dialog.findViewById(R.id.pitchBar);
        SeekBar speakingSpeedBar = (SeekBar)dialog.findViewById(R.id.speedBar);

        pitchBar.setProgress((int) pitchValue);
        speakingSpeedBar.setProgress((int)speakingSpeedValue);

        pitchBar.setOnSeekBarChangeListener(new PicthBarEvent());
        speakingSpeedBar.setOnSeekBarChangeListener(new SpeakingSpeedBarEvent());

        Spinner languageSpinner = (Spinner)findViewById(R.id.language_spinner);
        languageSpinner.setOnItemSelectedListener(new LanguageSpinnerHandler());


        dialog.show();
    }

I am getting a NullPointerException right in here

languageSpinner.setOnItemSelectedListener(new LanguageSpinnerHandler());

Where I went wrong?

languageSpinner belongs to talk_settings.xml , so you have to look for it inside the dialog view

Spinner languageSpinner = (Spinner)dialog.findViewById(R.id.language_spinner);

also, String comparison in Java should be performed through the equals or equalsIgnoreCase method

Check if languageSpinner is initialized properly.

Spinner languageSpinner = (Spinner)dialog.findViewById(R.id.language_spinner);

findViewById looks for aa view in the current inflated layout. So use the dialog object to initialize spinner

and use .equals to compare strings

parent.getItemAtPosition(position).toString().equals("English")

Try usion

Spinner languageSpinner = (Spinner)dialog.findViewById(R.id.language_spinner);

instead of

Spinner languageSpinner = (Spinner)findViewById(R.id.language_spinner); .

As your spinner is in the talk_settings XML you will need to find the Spinner id in the view in which the XML is inflated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM