2015-09-19 31 views
1

返回null我有一個看起來像這樣一個自定義的線性佈局:findViewById在擴展的LinearLayout

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/[email protected]@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

試試下面的代碼它會幫助你,如果在同一個list_item佈局的單選按鈕

LayoutInflater inflater = LayoutInflater.from(context); 
      View customvw= inflater.inflate(R.layout.list_item, null); 
      this.mRadio = (RadioButton)customvw.findViewById(R.id.radio_button); 
+2

哦,inflater返回一個視圖?我假設它在某種情況下采取了行動,並會設置一些東西。這可能會更有意義 – Derongan

+0

@Derongan是inflater返回查看您可以在這裏得到更多的信息,如果你需要http://developer.android.com/reference/android/view/LayoutInflater.html 如果我的答案幫助你,你可以upvote或接受它。謝謝 – Pavan

+0

我試過這個,並得到一個新的錯誤。添加到原始問題 – Derongan

0

您正在膨脹視圖,但未附加到您的CheckedLinearLayout

只需將inflater.inflate(R.layout.list_item, null);替換爲inflater.inflate(R.layout.list_item, this);,這會將XML擴展到視圖中。在你的XML定製的線性佈局

android:id="@+id/linearLayout1" 

+0

這也沒有效果 – Derongan

0

添加以下行這樣

<?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> 

這就是的它

+0

這對我來說不起作用 – Derongan

3

有兩件事情你必須做在這裏。

首先,在你的代碼,替換:

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

與:

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

這樣您就可以告訴充氣器將充氣的視圖附加到您的層次結構中。給出一個null參數將使得浮動視圖在沒有父項的情況下浮動。

其次,在你的XML標記,更換:

derongan.upper。CheckedLinearLayout

用一個簡單的

的LinearLayout

通過這樣做,你會避免遞歸重新創建視圖(調用膨脹的自定義視圖佈局不幸的是調用它的XML構造,並具有在同一個構造函數中的充氣調用會給你那個溢出錯誤)。只需在xml中使用LinearLayout或爲視圖提供兩種類型的構造函數。

+0

關於遞歸重新創建我自己的視圖的最後一點是我直到你指出它纔得到的東西。謝謝,不知道它是否有助於原始海報,但它幫助了我。 –

相關問題