我在使用 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());
我哪里出错了?