Reputation: 810
I have a custom linear layout that looks like this:
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);
}
}
However the findViewById is always returning null.
The 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>
Why is the findViewById always returning null, and how can I properly call it? Am I inflating the view wrong?
Edit:
When I add
View view = inflater.inflate(...)
I get a new error (that seems to have to do with memory problems?)
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@[email protected]
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.
Upvotes: 1
Views: 876
Reputation:
There are two things you must do here.
First, in your code, replace:
inflater.inflate(R.layout.list_item, null);
with:
inflater.inflate(R.layout.list_item, this);
This way you will tell the inflater to attach the inflated view to your hierarchy. Giving a null parameter will leave the inflated view floating without a parent.
Secondly, In your xml tag, replace:
derongan.upper.CheckedLinearLayout
with a simple
LinearLayout
By doing this you will avoid recursively recreating your view (calling inflate on your custom view layout will unfortunately call it's xml constructor, and having an inflate call in the same constructor will give you that overflow error). Just use a LinearLayout in the xml or provide two types of constructors for your view.
Upvotes: 3
Reputation: 898
add following line in your xml custom linear layout
android:id="@+id/linearLayout1"
like this
<?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>
thats's it
Upvotes: 0
Reputation: 8828
You're inflating the view but not attaching it to your CheckedLinearLayout
.
Simply replace inflater.inflate(R.layout.list_item, null);
with inflater.inflate(R.layout.list_item, this);
, which inflates the XML into the view.
Upvotes: 0
Reputation: 5136
try the following code it will help you if radiobutton in same list_item layout
LayoutInflater inflater = LayoutInflater.from(context);
View customvw= inflater.inflate(R.layout.list_item, null);
this.mRadio = (RadioButton)customvw.findViewById(R.id.radio_button);
Upvotes: 0