设置Spinner的事件处理程序时出现NullPointerException

时间:2013-11-11 10:29:46

标签: java android event-handling nullpointerexception android-spinner

我对android Spinner有一些问题。请看下面的代码。

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 (微调器的字符串资源)

<?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代码

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

我在这里获得了NullPointerException

languageSpinner.setOnItemSelectedListener(new LanguageSpinnerHandler());

我哪里出错?

3 个答案:

答案 0 :(得分:3)

languageSpinner属于talk_settings.xml,因此您必须在dialog视图中查找

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

另外,Java中的字符串比较应该通过equalsequalsIgnoreCase方法执行

答案 1 :(得分:1)

检查languageSpinner是否已正确初始化。

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

findViewById在当前膨胀的布局中查找视图。因此,使用dialog对象初始化微调器

并使用.equals比较字符串

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

答案 2 :(得分:1)

尝试使用

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

而不是

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

由于您的微调器位于talk_settings XML中,因此您需要在XML膨胀的视图中找到Spinner ID。