ListView findViewByID返回null

时间:2012-11-14 18:57:28

标签: android android-layout

我在XML中定义了几个在我尝试获取句柄时返回null的对象。我看到几个帖子说清理项目,但没有帮助。当我探索ListView对象时,所有的子节点都是null?所以我对自己做错了什么感到有点失落。以下是我认为相关的代码,但如果您需要查看其他内容,请告诉我,我会发布。

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" />

在活动中:

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;
          }
        });

enter image description here

这是我的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

1 个答案:

答案 0 :(得分:3)

  

当我探索ListView对象时,所有子节点都为null ??

onResume()完成后之前,不会绘制视图。所以ListView在onCreate()中没有任何子项。

这个按钮在哪里?如果它在ListView中,则需要编写自定义适配器来覆盖它的侦听器。


添加

  

它位于ListView布局中。

您需要扩展当前的适配器并覆盖getView(),以便为每一行提供这样的唯一侦听器。请在多个Google Talk活动中观看Android的Romain Guy对此exact topic的详细讨论。