开发者

One button and a list view layout problem

开发者 https://www.devze.com 2023-04-03 04:04 出处:网络
So I am trying to create a program that tracks gps coordinates and then display them real time in a list view on the side of my main screen, the layout looks as follows:

So I am trying to create a program that tracks gps coordinates and then display them real time in a list view on the side of my main screen, the layout looks as follows:

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

{} = button

LV = list view scrolling vertical

What I have in on create is:

    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 file is as follows:

<?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>

The program is failing, and I am not sure what is incorrect here is the log file:

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/A开发者_开发百科ndroidRuntime(294):     ... 11 more

Apprently there is a null exception at the following line:

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

I have no clue why. Help is very much appreciated.


ok try this bottomList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, AllLocals)); and make sure that u initialize the AllLocals before u call this method

AllLocals = new ArrayList<String>();

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号