简体   繁体   中英

LstView findViewByID returning null

I have several objects defined in XML that are returning null when I try to get a handle on them. I saw several post saying to clean the project but that did not help. When I explore the ListView object all the children are null?? So I am at a bit of a loss as to what I am doing wrong. Here is the code that I think is relevant but if you need to see something else let me know and I'll post it.

TIA JB

<Button
    android:id="@+id/btn_NextLift"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="40dp"
    android:text="@string/str_BtnNxtTxt"
    android:onClick="btn_NextLiftClick"
    android:longClickable="true" />

In the activity:

public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.liftinterface);
        ...//More code....
        ...//Still in the OnCreate....
        lstvw_LiftData = (ListView)findViewById(R.id.lstvw_LiftData);
        ...//Image below of this object.....
        //Get a handle on our button
        Button btn_Nxt = (Button)this.findViewById(R.id.btn_NextLift);
        btn_Nxt.setOnLongClickListener(new OnLongClickListener()
        {
         public boolean onLongClick(View v)
         {
        SaveAdvance();

        return true;
          }
        });

在此处输入图片说明

Here is my logcat errors only:

11-13 22:04:57.798: E/AndroidRuntime(787): FATAL EXCEPTION: main
11-13 22:04:57.798: E/AndroidRuntime(787): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.gpgvm.ironmike/org.gpgvm.ironmike.IcyArmActivity}: java.lang.NullPointerException
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.os.Looper.loop(Looper.java:123)
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-13 22:04:57.798: E/AndroidRuntime(787):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 22:04:57.798: E/AndroidRuntime(787):  at java.lang.reflect.Method.invoke(Method.java:507)
11-13 22:04:57.798: E/AndroidRuntime(787):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-13 22:04:57.798: E/AndroidRuntime(787):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-13 22:04:57.798: E/AndroidRuntime(787):  at dalvik.system.NativeStart.main(Native Method)
11-13 22:04:57.798: E/AndroidRuntime(787): Caused by: java.lang.NullPointerException
11-13 22:04:57.798: E/AndroidRuntime(787):  at org.gpgvm.ironmike.IcyArmActivity.onCreate(IcyArmActivity.java:83)
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-13 22:04:57.798: E/AndroidRuntime(787):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-13 22:04:57.798: E/AndroidRuntime(787):  ... 11 more

When I explore the ListView object all the children are null??

Views are not draw until after onResume() has completed. So the ListView will not have any children in onCreate() .

Where is this Button? If it is in the ListView, you need to write a custom adapter to override it's listeners.


Addition

It is in the ListView layout.

You need to extend you current adapter and override getView() to give each row unique listeners like this. Please watch Android's Romain Guy discussion this exact topic in fabulous detail at multiple Google Talk events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM