为什么程序一直提示错误
这是我的Java文件
package com.example.spinner; import java.util.ArrayList; import java.util.List; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.os.Build; public class MainActivity extends Activity { private TextView textview; private Spinner spinner; private List<String>list; private ArrayAdapter<String>adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview=(TextView) findViewById(R.id.textview); spinner=(Spinner) findViewById(R.id.spinner); //1:设置数据源、 list=new ArrayList<String>(); list.add("北京"); list.add("上海"); list.add("深圳"); list.add("广州"); //2:新建数组适配器 adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); //3:设置下拉列表样式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //4:加载适配器 spinner.setAdapter(adapter); } }
这是我的xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" android:textColor="#ff0000" /> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner" /> </LinearLayout>
这是我的日志
07-18 14:52:31.343: D/AndroidRuntime(1593): Shutting down VM 07-18 14:52:31.343: W/dalvikvm(1593): threadid=1: thread exiting with uncaught exception (group=0xa61ea908) 07-18 14:52:31.347: E/AndroidRuntime(1593): FATAL EXCEPTION: main 07-18 14:52:31.347: E/AndroidRuntime(1593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spinner/com.example.spinner.MainActivity}: java.lang.NullPointerException 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.app.ActivityThread.access$600(ActivityThread.java:141) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.os.Handler.dispatchMessage(Handler.java:99) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.os.Looper.loop(Looper.java:137) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.app.ActivityThread.main(ActivityThread.java:5041) 07-18 14:52:31.347: E/AndroidRuntime(1593): at java.lang.reflect.Method.invokeNative(Native Method) 07-18 14:52:31.347: E/AndroidRuntime(1593): at java.lang.reflect.Method.invoke(Method.java:511) 07-18 14:52:31.347: E/AndroidRuntime(1593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-18 14:52:31.347: E/AndroidRuntime(1593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-18 14:52:31.347: E/AndroidRuntime(1593): at dalvik.system.NativeStart.main(Native Method) 07-18 14:52:31.347: E/AndroidRuntime(1593): Caused by: java.lang.NullPointerException 07-18 14:52:31.347: E/AndroidRuntime(1593): at com.example.spinner.MainActivity.onCreate(MainActivity.java:49) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.app.Activity.performCreate(Activity.java:5104) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 07-18 14:52:31.347: E/AndroidRuntime(1593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 07-18 14:52:31.347: E/AndroidRuntime(1593): ... 11 more