如何正确引用Spinner条目?

时间:2015-04-10 03:25:07

标签: java android spinner listeners

我一直在制作Android应用,它使用Spinner列表显示几个城市的名称。我需要程序来更改微调器下方的TextView以显示数组中所选项的位置。我为同一个

编写了以下代码
public class MainScreen extends Activity {
String[] cityArray = { "Agra ", "Ahmedabad", "Alappuzha", "Amritsar"};
Spinner list = (Spinner) findViewById(R.id.ListView1);
TextView nam = (TextView)findViewById(R.id.textView2);

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_screen);

      ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, cityArray);
      list.setAdapter(adapter);


list.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parentView, View view,int position, long id) 
    {
        nam.setText(list.getSelectedItemPosition());
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        nam.setText("None Selected");
    }

});
}
}

运行程序时没有错误,但应用程序只需在打开后立即强制关闭。

logcat的:

04-10 08:49:30.423: I/dalvikvm(561): threadid=3: reacting to signal 3
04-10 08:49:30.723: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
04-10 08:49:30.783: I/dalvikvm(561): threadid=3: reacting to signal 3
04-10 08:49:30.823: D/AndroidRuntime(561): Shutting down VM
04-10 08:49:30.823: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-10 08:49:30.844: E/AndroidRuntime(561): FATAL EXCEPTION: main
04-10 08:49:30.844: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.droidrish.droidweather/com.droidrish.droidweather.MainScreen}: java.lang.NullPointerException
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.os.Looper.loop(Looper.java:137)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-10 08:49:30.844: E/AndroidRuntime(561):  at java.lang.reflect.Method.invokeNative(Native Method)
04-10 08:49:30.844: E/AndroidRuntime(561):  at java.lang.reflect.Method.invoke(Method.java:511)
04-10 08:49:30.844: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-10 08:49:30.844: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-10 08:49:30.844: E/AndroidRuntime(561):  at dalvik.system.NativeStart.main(Native Method)
04-10 08:49:30.844: E/AndroidRuntime(561): Caused by: java.lang.NullPointerException
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.Activity.findViewById(Activity.java:1794)
04-10 08:49:30.844: E/AndroidRuntime(561):  at com.droidrish.droidweather.MainScreen.<init>(MainScreen.java:15)
04-10 08:49:30.844: E/AndroidRuntime(561):  at java.lang.Class.newInstanceImpl(Native Method)
04-10 08:49:30.844: E/AndroidRuntime(561):  at java.lang.Class.newInstance(Class.java:1319)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
04-10 08:49:30.844: E/AndroidRuntime(561):  ... 11 more
04-10 08:49:30.844: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
04-10 08:49:31.416: I/dalvikvm(561): threadid=3: reacting to signal 3
04-10 08:49:31.443: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
04-10 08:49:31.583: I/dalvikvm(561): threadid=3: reacting to signal 3
04-10 08:49:31.603: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
04-10 08:49:33.722: I/Process(561): Sending signal. PID: 561 SIG: 9

我是否正确使用OnItemSelected侦听器?我期待程序将TextView的文本更改为Spinner中项目的位置。

2 个答案:

答案 0 :(得分:2)

Spinner和textview应该在onCreate中初始化,如下所示:

public class MainScreen extends Activity {

      String[] cityArray = { "Agra ", "Ahmedabad", "Alappuzha", "Amritsar"};
      Spinner list;
      TextView nam;

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_screen);

      list = (Spinner)findViewById(R.id.ListView1);
      nam = (TextView)findViewById(R.id.textView2);

然后改变这个:

 nam.setText(list.getSelectedItemPosition());

为:

nam.setText(list.getSelectedItem().toString());

如果您只想要索引,请执行以下操作:

nam.setText(String.valueOf(list.getSelectedItemPosition()));

答案 1 :(得分:1)

这是问题所在:

Spinner list = (Spinner) findViewById(R.id.ListView1);
TextView nam = (TextView)findViewById(R.id.textView2);

将这些内容onCreate()移到setContentView()下方。