LstView findViewByID返回null [英] LstView findViewByID returning null

查看:107
本文介绍了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 ...更多


解决方案

  

在我探索的ListView对象所有的孩子都为空??


视图不会画到的之后 onResume()已完成。所以在ListView不会有任何儿童的onCreate()

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


加入


  

这是在ListView布局。


您需要延长您当前适配器并重写 getView()给每一行这样的独特的听众。敬请收看Android的罗曼盖伊讨论这个确切的话题在美妙的细节谷歌多次对话活动。

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆