本文介绍了LstView findViewByID返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XML中定义多个对象,当我试图让他们的句柄都返回null。我看见几个帖子说要清理的项目,但没有帮助。当我探索的ListView对象所有的孩子都为空?所以,我是有点茫然,我在做什么错。这里是code,我认为是相关的,但如果你需要看到其他的东西让我知道,我会后。

TIA
JB

 <按钮
    机器人:ID =@ + ID / btn_NextLift
    机器人:layout_width =100dp
    机器人:layout_height =50dp
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentRight =真
    机器人:layout_marginRight =30dp
    机器人:layout_marginBottom =40dp
    机器人:文字=@字符串/ str_BtnNxtTxt
    安卓的onClick =btn_NextLiftClick
    机器人:longClickable =真/>

在活动:

 公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.liftinterface);
        ... //更多code ....
        ... //不过在OnCreate ....
        lstvw_LiftData =(ListView控件)findViewById(R.id.lstvw_LiftData);
        ... //低于此目标的图像.....
        //获取手柄上我们的按钮
        按钮btn_Nxt =(按钮)this.findViewById(R.id.btn_NextLift);
        btn_Nxt.setOnLongClickListener(新OnLongClickListener()
        {
         公共布尔onLongClick(视图V)
         {
        SaveAdvance();        返回true;
          }
        });

下面是我的logcat仅错误:

  22 11-13:04:57.798:E / AndroidRuntime(787):致命异常:主要
11-13 22:04:57.798:E / AndroidRuntime(787):了java.lang.RuntimeException:无法启动活动ComponentInfo {org.gpgvm.ironmike / org.gpgvm.ironmike.IcyArmActivity}:显示java.lang.NullPointerException
11-13 22:04:57.798:E / AndroidRuntime(787):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-13 22:04:57.798:E / AndroidRuntime(787):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-13 22:04:57.798:E / AndroidRuntime(787):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
11-13 22:04:57.798:E / AndroidRuntime(787):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
11-13 22:04:57.798:E / AndroidRuntime(787):在android.os.Handler.dispatchMessage(Handler.java:99)
11-13 22:04:57.798:E / AndroidRuntime(787):在android.os.Looper.loop(Looper.java:123)
11-13 22:04:57.798:E / AndroidRuntime(787):在android.app.ActivityThread.main(ActivityThread.java:3683)
11-13 22:04:57.798:E / AndroidRuntime(787):在java.lang.reflect.Method.invokeNative(本机方法)
11-13 22:04:57.798:E / AndroidRuntime(787):在java.lang.reflect.Method.invoke(Method.java:507)
11-13 22:04:57.798:E / AndroidRuntime(787):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
11-13 22:04:57.798:E / AndroidRuntime(787):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-13 22:04:57.798:E / AndroidRuntime(787):在dalvik.system.NativeStart.main(本机方法)
11-13 22:04:57.798:E / AndroidRuntime(787):致:显示java.lang.NullPointerException
11-13 22:04:57.798:E / AndroidRuntime(787):在org.gpgvm.ironmike.IcyArmActivity.onCreate(IcyArmActivity.java:83)
11-13 22:04:57.798:E / AndroidRuntime(787):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-13 22:04:57.798:E / AndroidRuntime(787):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-13 22:04:57.798:E / AndroidRuntime(787):11 ...更多


解决方案

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

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.

这篇关于LstView findViewByID返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 20:48