我有一个自定义线性布局,如下所示:
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.
答案 0 :(得分:3)
你必须在这做两件事。
首先,在您的代码中,替换:
inflater.inflate(R.layout.list_item, null );
使用:
inflater.inflate(R.layout.list_item, this );
这样您就会告诉inflater将膨胀的视图附加到您的层次结构中。给出一个null参数会使膨胀的视图在没有父项的情况下浮动。
其次,在您的xml标记中,替换:
derongan.upper.CheckedLinearLayout
用简单的
的LinearLayout
通过执行此操作,您将避免以递归方式重新创建视图(不幸的是,在自定义视图布局上调用inflate会调用它的xml构造函数,并且在同一构造函数中进行膨胀调用会给出溢出错误)。只需在xml中使用LinearLayout或为视图提供两种类型的构造函数。
答案 1 :(得分:0)
尝试以下代码,如果相同list_item布局中的radiobutton
,它将对您有所帮助 LayoutInflater inflater = LayoutInflater.from(context);
View customvw= inflater.inflate(R.layout.list_item, null);
this.mRadio = (RadioButton)customvw.findViewById(R.id.radio_button);
答案 2 :(得分:0)
您正在对视图进行充气,但未将其附加到CheckedLinearLayout
。
只需将inflater.inflate(R.layout.list_item, null);
替换为inflater.inflate(R.layout.list_item, this);
,即将XML扩展到视图中。
答案 3 :(得分:0)
在xml自定义线性布局中添加以下行
android:id="@+id/linearLayout1"
像这样
<?xml version="1.0" encoding="utf-8"?>
<derongan.upper.CheckedLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
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>
多数民众赞成