0

我在使用 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());

我哪里出错了?

4

3 回答 3

3

languageSpinner属于 ,所以你必须在视图talk_settings.xml中寻找它dialog

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

此外,Java 中的字符串比较应该通过equalsorequalsIgnoreCase方法进行

于 2013-11-11T10:30:59.100 回答
1

检查是否languageSpinner正确初始化。

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

findViewById在当前的膨胀布局中寻找一个视图。所以使用对话框对象来初始化微调器

并用于.equals比较字符串

parent.getItemAtPosition(position).toString().equals("English")
于 2013-11-11T10:32:01.053 回答
1

尝试使用

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

代替

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

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

于 2013-11-11T10:33:37.943 回答