主活动中定义的微调器为null

时间:2014-10-17 06:22:45

标签: android

我使用entries选项在xml文件中填充了一个带有项目的微调器。我已经将主活动类中的微调器定义为" s1"。在调试模式下,我发现s1为空。这就是setOnItemSelectedListener不适用于微调器的原因。选择项目时,不显示Toast消息。我给了mainactivity.java,activity_main.xml和strings.xml文件。

MainActivity.java

public class MainActivity extends ActionBarActivity{
String[] presidents; 
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
} 

presidents = getResources().getStringArray(R.array.presidents);

Spinner s1 = (Spinner) findViewById(R.id.spinner1);


ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.presidents, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(s1 != null){
s1.setAdapter(adapter);

s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), presidents[index], Toast.LENGTH_SHORT).show();

}
public void onNothingSelected(AdapterView<?> arg0){

}
});
}


}
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

activity_main.xml中

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop= "true"
android:entries="@array/presidents" />

的strings.xml

<resources>
<string-array name = "presidents">
<item>eisenhower</item>
<item>kennedy</item>
</string-array>   
</resources>

3 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

<强> activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<强> MainActivity.java

public class MainActivity extends ActionBarActivity {

    private Spinner s1;
    private String[] presidents;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        s1 = (Spinner) findViewById(R.id.spinner1);
        presidents = getResources().getStringArray(R.array.presidents);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.presidents, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        if(s1 != null){
            s1.setAdapter(adapter);
            s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3)
                {
                    Toast.makeText(MainActivity.this, presidents[arg2], Toast.LENGTH_SHORT).show();

                }
                public void onNothingSelected(AdapterView<?> arg0){

                }
            });
        }
    }
}

答案 1 :(得分:0)

您的setContentView(R.layout.activity_main);错误,请改用R.layout.fragment_main

答案 2 :(得分:0)

您已在班级级别初始化了Spinner。此时,Spinner对象为null。您应该在onCreate()方法中初始化您的Spinner。所以你可以这样:

Spinner s1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // Only here is the layout inflated and only after this can Android see the Spinner

    s1 = (Spinner) findViewById(R.id.spinner);
    // Other statements
}