一个按钮和列表视图布局问题

时间:2011-09-07 22:32:45

标签: android nullpointerexception android-listview

所以我试图创建一个跟踪gps坐标的程序,然后在主屏幕侧面的列表视图中实时显示它们,布局如下:

_______
|{} |LV|
|   |  |
|   |  |
|   |  |
|___|__|

{} =按钮
LV =列表视图滚动垂直

我所创造的是:

    public void onCreate(Bundle bundleIn) {
        super.onCreate(bundleIn);

        setContentView(R.layout.main);
        retrieveLocationButton = (Button)findViewById(R.id.retrieve_location_button);
        bottomList = (ListView)findViewById(R.id.myListView);
        AllLocals.add("1");
        bottomList.setAdapter(new ArrayAdapter<String>(this.getApplicationContext(), R.id.simple_text, AllLocals));

        //viewCollectedData = (Button)findViewById(R.id.view_lat_lon_data);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //setListAdapter(new ArrayAdapter<Location>(this,android.R.layout.simple_list_item_1,AllLocals));

XML文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

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

            <Button android:layout_height="wrap_content" 
            android:text="Get Local" 
            android:id="@+id/retrieve_location_button" 
            android:layout_width="wrap_content">
            </Button>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
          <TextView android:id="@+id/simple_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
          />
          <ListView android:id="@+id/myListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
          />

    </LinearLayout>

</LinearLayout>

程序失败了,我不确定这里的错误是日志文件:

09-07 15:37:20.301: ERROR/AndroidRuntime(294): FATAL EXCEPTION: 
main
09-07 15:37:20.301: ERROR/AndroidRuntime(294): java.lang.RuntimeException: 
Unable to start activity ComponentInfo{geolocation.test/geolocation.test.GeoLocationActivity}: java.lang.NullPointerException
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-07 15:37:20.301: 
ERROR/AndroidRuntime(294):     
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 15:37:20.301: 
ERROR/AndroidRuntime(294):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     at android.os.Looper.loop(Looper.java:123)
09-07 15:37:20.301: 
ERROR/AndroidRuntime(294):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     at java.lang.reflect.Method.invoke(Method.java:521)
09-07 15:37:20.301: 
ERROR/AndroidRuntime(294):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     at dalvik.system.NativeStart.main(Native Method)
09-07 15:37:20.301: ERROR/AndroidRuntime(294): 
Caused by: java.lang.NullPointerException
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     
at geolocation.test.GeoLocationActivity.onCreate(GeoLocationActivity.java:47)
09-07 15:37:20.301: 
ERROR/AndroidRuntime(294):     
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 15:37:20.301: 
ERROR/AndroidRuntime(294):     
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 15:37:20.301: ERROR/AndroidRuntime(294):     ... 11 more

显然在以下行有一个空例外:

bottomList.setAdapter(new ArrayAdapter<String>(this.getApplicationContext(), R.id.simple_text, AllLocals));

我不知道为什么。非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

好的尝试一下  bottomList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, AllLocals));并确保在调用此方法之前初始化AllLocals

AllLocals = new ArrayList<String>();