【问题标题】:findViewById() is returning null for custom view - AndroidfindViewById() 为自定义视图返回 null - Android
【发布时间】:2013-02-27 20:36:46
【问题描述】:

首先让我这样说:我已经查看了有关此问题的所有其他线程并梳理了 Google 的答案,但没有任何帮助。

我有一个布局,其中包含在运行时添加的自定义视图。我的自定义视图有 super(context, attrs) 构造函数。

自定义视图:

public CustomLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize some variables...
}

我使用以下方法向我的活动添加视图:

public void addNewView (boolean isEmpty) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < amount; i++) {
    if (isEmpty) {
        View v = (ViewGroup) inflater.inflate(R.layout.my_custom_layout, layoutContainer, true);
        viewTracker++;
    }
    }
}

如果我在上面的 addNewView() 方法中将 findViewById 用于我的自定义视图,那么一切都很好。但是,如果我在视图膨胀后(显然在setContentView 之后)尝试在活动中的其他任何地方使用 findViewById,我会得到一个空指针。我也尝试过someView.findViewById(),但它不起作用。我难住了。有什么想法吗?

Logcat 显示一个空指针,但仅此而已:

02-16 06:56:10.681: E/AndroidRuntime(11360): FATAL EXCEPTION: main
02-16 06:56:10.681: E/AndroidRuntime(11360): java.lang.NullPointerException
02-16 06:56:10.681: E/AndroidRuntime(11360): at com.nutrifit.Fragments.WorkoutPlannerPopup.resetOtherView(WorkoutPlannerPopup.java:404)
02-16 06:56:10.681: E/AndroidRuntime(11360): at com.nutrifit.customviews.CustomLinearLayout.onTouchEvent(CustomLinearLayout.java:43)
02-16 06:56:10.681: E/AndroidRuntime(11360): at android.view.View.dispatchTouchEvent(View.java:7239)
02-16 06:56:10.681: E/AndroidRuntime(11360): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
02-16 06:56:10.681: E/AndroidRuntime(11360): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
02-16 06:56:10.681: E/AndroidRuntime(11360): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)

这是我在 XML 中的自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<com.nutrifit.customviews.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/exercise_container_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/wpp_background_border" >

<ImageView android:id="@+id/delete_button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/wpp_background_border" />
...more android child views

</com.nutrifit.customviews.CustomLinearLayout>

【问题讨论】:

  • 您确定 setContentView 分配正确并且没有抛出未捕获的异常吗?发布 logcat 的输出和所有代码。
  • @Kerry 据我所知,setContentView 分配正确。当我尝试使用 findViewById 时,抛出的唯一异常是空指针。您希望我发布哪个代码?包含此内容的活动是 450 多行代码。
  • 请将您的 xml 粘贴到您声明自定义视图的位置。
  • 谢谢,看起来不错,但我不确定我是否理解您的问题。您想在运行时添加许多自定义线性布局及其内容吗?或者您想在自定义线性布局中添加一些子元素?
  • @MichałZ。肯定的事。抱歉,我不是很清楚。我有几个类似上面的 XML 布局。在我的活动中,我有根布局。我想要的是在运行时将上述布局添加到我的活动的根布局中,然后稍后在我的代码中引用它们。但是,当我尝试在自定义视图(甚至上面布局中的 android 视图)上使用 findViewById 时,我得到一个空指针。我认为这可能与 LayoutInflater 有关,但我不确定。

标签: android nullpointerexception android-custom-view


【解决方案1】:

addNewView() 方法在哪里被调用?据我了解,如果您在自定义视图的构造函数中执行findViewById(),您可以获得null。我还没有找到权威参考说明何时可以安全地在自定义视图中调用findViewById(),但我通常在onFinishInflate() 或更高版本中这样做。

【讨论】:

  • 通过单击按钮调用addNewView() 方法。所以,布局应该是膨胀的。我解决了这个问题。我最终发现它与我的自定义视图有关。但是,我不知道在我的自定义视图中会发生什么 - 我有正确的构造函数,并且视图没有其他特别之处。不过感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多