使用数组适配器的spinner实现

时间:2014-07-03 06:31:41

标签: android xml

//我的代码有什么问题..没有在模拟器上运行

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Spinner</string>

<string name="hello_world">Hello world!</string>

<string name="action_settings">Settings</string>

<string-array name="country_array">

    <item>Greece</item>

    <item>United Kingdom</item>

    <item>Italy</item>

    <item>France</item>

    <item>Germany</item>

    <item>Turkey</item>

    <item>Poland</item>

    <item>India</item>

</string-array>

// activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.spinner.MainActivity$PlaceholderFragment" >

<Spinner

    android:id="@+id/spinCountry"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginTop="5dp"

    android:paddingLeft="8dp"

    android:popupBackground="@android:color/white"

    android:scrollbars="none"

    android:spinnerMode="dropdown" />

</RelativeLayout>

// MainActivity.java

public class MainActivity extends ActionBarActivity {

Spinner spinCountry;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    spinCountry = (Spinner) findViewById(R.id.spinCountry);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

            android.R.layout.simple_spinner_item, getResources()

                    .getStringArray(R.array.country_array)); 

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinCountry.setAdapter(adapter);


    spinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,

                int position, long id) {
        }

        @Override

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


    if (savedInstanceState == null) {

        getSupportFragmentManager().beginTransaction()

                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.

    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar item clicks here. The action bar will

    // automatically handle clicks on the Home/Up button, so long

    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    if (id == R.id.action_settings) {

        return true;

    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
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;
  }
  }

}

//我的日志猫

07-03 14:57:58.700: W/dalvikvm(1172): threadid=1: thread exiting with uncaught exception 

(group=0xb2a1dba8)

07-03 14:57:58.760: E/AndroidRuntime(1172): FATAL EXCEPTION: main

07-03 14:57:58.760: E/AndroidRuntime(1172): Process: com.example.spinner, PID: 1172

07-03 14:57:58.760: E/AndroidRuntime(1172): java.lang.RuntimeException: Unable to start 

activity ComponentInfo{com.example.spinner/com.example.spinner.MainActivity}: 

java.lang.NullPointerException

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.app.ActivityThread.access$800(ActivityThread.java:135)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.os.Handler.dispatchMessage(Handler.java:102)


07-03 14:57:58.760: E/AndroidRuntime(1172):     at android.os.Looper.loop(Looper.java:136)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.app.ActivityThread.main(ActivityThread.java:5017)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

java.lang.reflect.Method.invokeNative(Native Method)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

java.lang.reflect.Method.invoke(Method.java:515)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at dalvik.system.NativeStart.main(Native Method)

07-03 14:57:58.760: E/AndroidRuntime(1172): Caused by: java.lang.NullPointerException

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

com.example.spinner.MainActivity.onCreate(MainActivity.java:35)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.app.Activity.performCreate(Activity.java:5231)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

07-03 14:57:58.760: E/AndroidRuntime(1172):     at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

07-03 14:57:58.760: E/AndroidRuntime(1172):     ... 11 more

1 个答案:

答案 0 :(得分:1)

尝试这种方式:您应该将String array作为第二个参数传递。

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

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

有关详细信息,请转到Official Docs