2 jonskeet JonSkeet 于 2013.09.10 17:45 提问

如何根据第一个spinner来加载内容?

当在 com.example.bertin.HotelLocation.onItemSelected 上开启 FATAL EXCEPTION: main java.lang.NullPointer Exception 时,获得一个 Log Cat error。
是否是 onItemSelected 的错误呢。代码如下:

package com.example.bertin;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class HotelLocation extends Activity implements OnItemSelectedListener {

    Spinner countrySpinner, citySpinner;
    private ArrayAdapter<String> countryAdapter;
    private ArrayAdapter<String> cityAdapter;

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

        Spinner countrySpinner = (Spinner) findViewById(R.id.spinnerHotelCountry);
        Spinner citySpinner = (Spinner) findViewById(R.id.spinnerHotelCity);

        // Create an ArrayAdapter using the string array and a default spinner
        // layout
        ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter
                .createFromResource(this, R.array.countries,
                        android.R.layout.simple_spinner_item);
        ArrayAdapter<CharSequence> cityAdapter = ArrayAdapter
                .createFromResource(this, R.array.australia,
                        android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of choices appears
        countryAdapter         .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cityAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        countrySpinner.setAdapter(countryAdapter);
        citySpinner.setAdapter(cityAdapter);

        countrySpinner.setOnItemSelectedListener(this);
        // citySpinner.setOnItemSelectedListener(this);
    }
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        String[] citySpinnerArray = null;
        String selected = parent.getItemAtPosition(position).toString();
        if (selected.equals("Australia")) {
            citySpinnerArray = getResources().getStringArray(R.array.australia);
        }
        citySpinner.setAdapter(new ArrayAdapter(this,
                android.R.layout.simple_spinner_item, citySpinnerArray));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.hotel_location, menu);
        return true;
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
}

StackTrace:

09-10 03:42:02.484: E/AndroidRuntime(2160): FATAL EXCEPTION: main

09-10 03:42:02.484: E/AndroidRuntime(2160): java.lang.NullPointerException

09-10 03:42:02.484: E/AndroidRuntime(2160):     at com.example.bertin.HotelLocation.onItemSelected(HotelLocation.java:97)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at android.widget.AdapterView.access$200(AdapterView.java:49)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at android.os.Handler.handleCallback(Handler.java:730)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at android.os.Handler.dispatchMessage(Handler.java:92)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at android.os.Looper.loop(Looper.java:137)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at android.app.ActivityThread.main(ActivityThread.java:5103)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at java.lang.reflect.Method.invokeNative(Native Method)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at java.lang.reflect.Method.invoke(Method.java:525)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

09-10 03:42:02.484: E/AndroidRuntime(2160):     at dalvik.system.NativeStart.main(Native Method)
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!