简体   繁体   English

LstView findViewByID返回null

[英]LstView findViewByID returning null

I have several objects defined in XML that are returning null when I try to get a handle on them. 我有几个用XML定义的对象,当我尝试获取它们的句柄时它们返回null。 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?? 当我浏览ListView对象时,所有子级都为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 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: 这只是我的logcat错误:

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?? 当我浏览ListView对象时,所有子级都为null?

Views are not draw until after onResume() has completed. onResume()完成之前不会绘制视图。 So the ListView will not have any children in onCreate() . 因此ListView在onCreate()中将没有任何子级。

Where is this Button? 这个按钮在哪里? If it is in the ListView, you need to write a custom adapter to override it's listeners. 如果它在ListView中,则需要编写一个自定义适配器以覆盖它的侦听器。


Addition 加成

It is in the ListView layout. 它在ListView布局中。

You need to extend you current adapter and override getView() to give each row unique listeners like this. 您需要扩展当前的适配器,并覆盖getView()以便为每一行提供唯一的侦听器,如下所示。 Please watch Android's Romain Guy discussion this exact topic in fabulous detail at multiple Google Talk events. 请观看Android的Romain Guy在多个Google Talk活动中详细讨论这个确切的主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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