Android Spinners,改变适配器

时间:2012-01-10 20:51:37

标签: android

好吧,所以我是Android编程的新手,到目前为止我的经验非常有趣且具有挑战性。但是我担心我现在遇到了第一个我无法克服的问题。

简单地说,我想做的就是拥有2个Spinners: 1用于国家选择 1省/州选择

我想要完成的是当用户选择他/她的国家时,使用正确的适配器更新省/州Spinner。目前我只使用2个国家进行测试。

当我启动Activity时,我收到异常并且我的应用程序崩溃了。

这是我的代码,任何指针都会受到赞赏!

    public class ManageAccountActivity extends Activity {

    final ArrayAdapter<CharSequence> adapterSexe = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.sex_array_fr, android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> adapterProvince = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.province_array_fr, android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> adapterStates = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.state_array_fr, android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> adapterCountry = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.country_array_fr, android.R.layout.simple_spinner_item);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_management);

        Spinner spinSexe = (Spinner) findViewById(R.id.spin_sex);
        Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
        Spinner spinCountry = (Spinner) findViewById(R.id.spin_country);

        adapterSexe.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapterProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapterStates.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinSexe.setAdapter(adapterSexe);
        spinProvince.setAdapter(adapterProvince);
        spinCountry.setAdapter(adapterCountry);

        spinCountry.setOnItemSelectedListener(new CountryOnItemSelectedListener());   
    }   

    public class CountryOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            setContentView(R.layout.account_management);
            Spinner spinProvince = (Spinner) view.findViewById(R.id.spin_province);

            if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
                spinProvince.setAdapter(adapterProvince);
            } else {
                spinProvince.setAdapter(adapterStates);
            }
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
}

这是我收到的LogCat消息。

  

01-10 20:41:01.024:E / AndroidRuntime(1275):   java.lang.RuntimeException:无法实例化活动   ComponentInfo {gggolf.android.minutegolf / gggolf.android.minutegolf.ManageAccountActivity}:   显示java.lang.NullPointerException

3 个答案:

答案 0 :(得分:1)

为什么setContentView(R.layout.account_management);方法中有onItemSelected()?这不应该是必要的。

此外,您应该在活动的onCreate()方法中实例化适配器,并将实际活动实例作为上下文传递。

应该从

更改用于在select侦听器中检索Spinner对象的代码
Spinner spinProvince = (Spinner) view.findViewById(R.id.spin_province); 

进入

Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);

在select侦听器中对本地视图对象调用findViewById()将返回NULL,因为该视图不包含Spinner。

答案 1 :(得分:1)

尝试使用Activity实施AdapterView.OnItemSelectedListener本身。请注意,我已移动Spinners并遗漏了部分代码并将其替换为注释 - 请确保在必要时将其包含在内...

public class ManageAccountActivity extends Activity
    implements AdapterView.OnItemSelectedListener {

    // Your ArrayAdapters as before

    Spinner spinSexe = null;
    Spinner spinProvince = null;
    Spinner spinCountry = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_management);

        spinSexe = (Spinner) findViewById(R.id.spin_sex);
        spinProvince = (Spinner) findViewById(R.id.spin_province);
        spinCountry = (Spinner) findViewById(R.id.spin_country);

        // Call setDropDownViewResource on your ArrayAdapters

        // Call setAdapter on your Spinners

        spinCountry.setOnItemSelectedListener(this);   
    }

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
            spinProvince.setAdapter(adapterProvince);
        } else {
            spinProvince.setAdapter(adapterStates);
        }
    }

    public void onNothingSelected(AdapterView parent) {

    }
}

答案 2 :(得分:1)

你无法直接在类attribut上实例化ArrayAdapter,因为createFromResource()使用Context,而且它目前不存在,而是在onCreate()方法中执行。

另外,你的侦听器中出现 spin province 错误,你无法在 view 局部变量上调用findViewById,因为它不是你的布局,而是一个膨胀android.R.layout.simple_spinner_item

好方法:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);

        if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
            spinProvince.setAdapter(adapterProvince);
        } else {
            spinProvince.setAdapter(adapterStates);
        }
    }