gpt4 book ai didi

android - findViewById 在扩展的 LinearLayout 中返回 null

转载 作者:行者123 更新时间:2023-11-29 17:31:32 29 4
gpt4 key购买 nike

我有一个自定义线性布局,如下所示:

public class CheckedLinearLayout extends LinearLayout implements Checkable {
private RadioButton mRadio;

public CheckedLinearLayout(Context context, AttributeSet attributeSet){
super(context, attributeSet);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.list_item, null);
this.mRadio = (RadioButton)findViewById(R.id.radio_button);
}
}

但是 findViewById 总是返回 null。

xml:

<?xml version="1.0" encoding="utf-8"?>
<derongan.upper.CheckedLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/count_type"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />

<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="New Counter"
android:visibility="gone"/>

<RadioButton
android:id="@+id/radio_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</derongan.upper.CheckedLinearLayout>

为什么 findViewById 总是返回 null,我该如何正确调用它?我是否夸大了观点错误?

编辑:

当我添加

View view = inflater.inflate(...)

我收到一个新错误(这似乎与内存问题有关?)

09-19 23:53:22.760     982-1015/? E/SurfaceFlinger﹕ ro.sf.lcd_density must 

be defined as a build property
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] JNI ERROR (app bug): local reference table overflow (max=512)
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] local reference table dump:
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] Last 10 entries (of 512):
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 511: 0x12c918b0 derongan.upper.UpperWidgetConfigure
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 510: 0x12d735e0 java.lang.String "derongan.upper.C... (34 chars)
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 509: 0x12c918b0 derongan.upper.UpperWidgetConfigure
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 508: 0x12e74800 derongan.upper.CheckedLinearLayout
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 507: 0x12e74400 derongan.upper.CheckedLinearLayout
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 506: 0x12e74000 derongan.upper.CheckedLinearLayout
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 505: 0x12e73c00 derongan.upper.CheckedLinearLayout
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 504: 0x12e73800 derongan.upper.CheckedLinearLayout
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 503: 0x12e73400 derongan.upper.CheckedLinearLayout
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 502: 0x12e72c00 derongan.upper.CheckedLinearLayout
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] Summary:
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 503 of derongan.upper.CheckedLinearLayout (503 unique instances)
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 2 of derongan.upper.UpperWidgetConfigure (1 unique instances)
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 2 of java.lang.Class (2 unique instances)
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 1 of java.lang.String[] (3 elements)
09-19 23:53:22.856 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115] 4 of java.lang.String (4 unique instances)
09-19 23:53:22.857 11757-11757/derongan.upper A/art﹕ art/runtime/indirect_reference_table.cc:115]
09-19 23:53:23.520 11757-11757/derongan.upper A/art﹕ art/runtime/barrier.cc:90] Check failed: count_ == 0 (count_=-1, 0=0) Attempted to destroy barrier with non zero count
09-19 23:53:23.520 11757-11757/derongan.upper A/art﹕ art/runtime/runtime.cc:366] Runtime aborting --- recursively, so no thread-specific detail!
09-19 23:53:23.520 11757-11757/derongan.upper A/art﹕ art/runtime/runtime.cc:366]
09-19 23:53:23.520 11757-11757/derongan.upper A/libc﹕ Fatal signal 6 (SIGABRT), code -6 in tid 11757 (derongan.upper)
09-19 23:53:23.658 4490-4490/? A/DEBUG﹕ *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
09-19 23:53:23.658 4490-4490/? A/DEBUG﹕ Build fingerprint: 'generic_x86/sdk_google_phone_x86/generic_x86:6.0/MRA44C/2166767:eng/test-keys'
09-19 23:53:23.658 4490-4490/? A/DEBUG﹕ Revision: '0'
09-19 23:53:23.658 4490-4490/? A/DEBUG﹕ ABI: 'x86'
09-19 23:53:23.658 4490-4490/? A/DEBUG﹕ pid: 11757, tid: 11757, name: derongan.upper >>> derongan.upper <<<
09-19 23:53:23.658 4490-4490/? A/DEBUG﹕ signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
09-19 23:53:23.661 4490-4490/? A/DEBUG﹕ Abort message: 'art/runtime/indirect_reference_table.cc:115] JNI ERROR (app bug): local reference table overflow (max=512)'
09-19 23:53:23.661 4490-4490/? A/DEBUG﹕ eax 00000000 ebx 00002ded ecx 00002ded edx 00000006
09-19 23:53:23.661 4490-4490/? A/DEBUG﹕ esi b77b8c50 edi 00000002
09-19 23:53:23.661 4490-4490/? A/DEBUG﹕ xcs 00000073 xds 0000007b xes 0000007b xfs 00000007 xss 0000007b
09-19 23:53:23.661 4490-4490/? A/DEBUG﹕ eip b73815d6 ebp 00002ded esp bfe3adb0 flags 00200206
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ backtrace:
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #00 pc 000845d6 /system/lib/libc.so (tgkill+22)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #01 pc 00081618 /system/lib/libc.so (pthread_kill+70)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #02 pc 00027205 /system/lib/libc.so (raise+36)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #03 pc 000209e4 /system/lib/libc.so (abort+80)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #04 pc 0051753b /system/lib/libart.so (art::Runtime::Abort()+377)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #05 pc 0014d9f3 /system/lib/libart.so (art::LogMessage::~LogMessage()+1343)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #06 pc 00147f36 /system/lib/libart.so (art::Barrier::~Barrier()+966)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #07 pc 0056027f /system/lib/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)+271)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #08 pc 005176b8 /system/lib/libart.so (art::Runtime::Abort()+758)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #09 pc 0014d9f3 /system/lib/libart.so (art::LogMessage::~LogMessage()+1343)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #10 pc 002e70ac /system/lib/libart.so (art::IndirectReferenceTable::Add(unsigned int, art::mirror::Object*)+860)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #11 pc 00664ec1 /system/lib/libart.so (art::RememberForGcArgumentVisitor::Visit()+89)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #12 pc 0066523b /system/lib/libart.so (art::QuickArgumentVisitor::VisitArguments() (.constprop.257)+223)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #13 pc 00669ed4 /system/lib/libart.so (artQuickResolutionTrampoline+1060)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #14 pc 0013d7fd /system/lib/libart.so (art_quick_resolution_trampoline+77)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #15 pc 005cbc10 /data/app/derongan.upper-1/oat/x86/base.odex (android.view.View android.support.v4.app.FragmentActivity.onCreateView(java.lang.String, android.content.Context, android.util.AttributeSet)+84)
09-19 23:53:23.663 4490-4490/? A/DEBUG﹕ #16 pc 7416d2b7 /data/dalvik-cache/x86/system@framework@boot.oat
09-19 23:53:23.830 4490-4490/? A/DEBUG﹕ Tombstone written to: /data/tombstones/tombstone_09
09-19 23:53:23.830 4490-4490/? E/DEBUG﹕ AM write failed: Broken pipe
09-19 23:53:23.856 1291-1331/system_process E/InputDispatcher﹕ channel 'd69290f derongan.upper/derongan.upper.UpperWidgetConfigure (server)' ~ Channel is unrecoverably broken and will be disposed!
09-19 23:53:24.095 2203-2203/com.android.launcher3 E/Launcher﹕ Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the widget configuration activity.

最佳答案

这里有两件事你必须做。

首先,在您的代码中,替换:

inflater.inflate(R.layout.list_item, null);

与:

inflater.inflate(R.layout.list_item, this);

通过这种方式,您将告诉充气器将充气 View 附加到您的层次结构中。提供一个 null 参数将使膨胀的 View 在没有父级的情况下 float 。

其次,在您的 xml 标记中,替换:

derongan.upper.CheckedLinearLayout

用一个简单的

LinearLayout

通过这样做,您将避免递归地重新创建您的 View (不幸的是,在您的自定义 View 布局上调用 inflate 将调用它的 xml 构造函数,并且在同一个构造函数中调用 inflate 会给您带来溢出错误)。只需在 xml 中使用 LinearLayout 或为您的 View 提供两种类型的构造函数。

关于android - findViewById 在扩展的 LinearLayout 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32666106/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com