您好我正在尝试使用微调器下拉列表。但是我在这里做的是代码
时出现奇怪的错误Naryste.java
public class Naryste extends Activity {
private ImageView image;
private String[] states;
private Spinner spinner;
private TypedArray imgs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
states = getResources().getStringArray(R.array.Senioras);
spinner = (Spinner) findViewById(R.id.country_spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
}
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"
android:padding="10dip" >
tools:context=".Naryste" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Pasirinkite"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/country_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">LoginRegister</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_login">Login</string>
<string name="title_activity_register">Register</string>
<string name="title_activity_naryste">Naryste</string>
<string-array name="Senioras">
<item>1 mėn</item>
<item>3 mėn</item>
<item>6 mėn</item>
<item>12 mėn</item>
</string-array>
<string-array name="Vaikas">
<item>1 mėn</item>
<item>3 mėn</item>
<item>6 mėn</item>
<item>12 mėn</item>
</string-array>
<string-array name="Suauges">
<item>1 mėn</item>
<item>3 mėn</item>
<item>6 mėn</item>
<item>12 mėn</item>
</string-array>
</resources>
logcat的
06-02 11:33:37.912 24999-24999/com.tonikamitv.loginregister E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tonikamitv.loginregister/com.tonikamitv.loginregister.Naryste}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.tonikamitv.loginregister.Naryste.onCreate(Naryste.java:30)
at android.app.Activity.performCreate(Activity.java:5203)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
检查您的xml文件是否已在setContentView方法中设置布局文件对于您正在使用微调器的活动是否正确。