簡體   English   中英

Android Studio-如何在視圖布局中膨脹的tabhost中的tab1中找到EditText?

[英]Android Studio - How to find EditText in tab1 within tabhost inflated on View layout?

我有嚴重的問題。 我正在嘗試從Tabhost中的tab1獲取EditText,但是它一直給我錯誤:

Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.findViewById(int)' on a null object reference.

我已經以許多不同的方式嘗試過我的代碼,但沒有成功! 這是我的代碼:

        final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View control_layout = inflater.inflate(R.layout.control_layout,null,false);
        TabHost tbhst = (TabHost)control_layout.findViewById(R.id.tabHost);
        tbhst.setup();
        TabHost.TabSpec tb1 = tbhst.newTabSpec("TAB 1");
        TabHost.TabSpec tb2 = tbhst.newTabSpec("TAB 2");
        TabHost.TabSpec tb3 = tbhst.newTabSpec("TAB 3");
        tb1.setContent(R.id.tab1);
        tb2.setContent(R.id.tab2);
        tb3.setContent(R.id.tab3);
        tb1.setIndicator("CMD");tb2.setIndicator("Chat");tb3.setIndicator("Control");tbhst.addTab(tb3);tbhst.addTab(tb2);tbhst.addTab(tb1);
        LinearLayout gff = (LinearLayout)tbhst.findViewById(R.id.ggf);
        FrameLayout tbs_content = (FrameLayout)gff.findViewById(android.R.id.tabcontent);
        LinearLayout tbs1 = (LinearLayout)tbs_content.findViewById(R.id.tab1);
        final EditText command_resualt_box = (EditText) tbs1.findViewById(R.id.editText5);

更新 :如@popvitsj所說,也許我正在犯錯,但我嘗試在layout / control.xml中進行更改,但這給了我另一個錯誤,這是屏幕截圖: 截圖

這是layout / control.xml中的代碼:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/control_q">

<TabHost
    android:layout_width="fill_parent"
    android:layout_height="410dp"
    android:id="@+id/tabHost"
    android:layout_gravity="center_horizontal">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/ggf">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"></TabWidget>

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/linearLayout"
                    android:layout_gravity="center_horizontal">

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="35dp"
                        android:id="@+id/editText9"
                        android:inputType="text"
                        android:imeOptions="actionDone"
                        android:background="@android:color/black"
                        android:textColor="#ffff0c00"
                        android:layout_marginTop="5dp" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        android:inputType="textMultiLine"
                        android:ems="10"
                        android:id="@+id/editText10"
                        android:enabled="false"
                        android:background="@android:color/black"
                        android:textColor="#ffff0c00"
                        android:autoLink="all"
                        android:layout_marginTop="5dp"
                        android:scrollbars="horizontal|vertical"
                        android:linksClickable="true"
                        android:editable="false" />

                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:weightSum="1"
                    android:id="@+id/linearLayout2" >

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal" >

                        <EditText
                            android:layout_width="260dp"
                            android:layout_height="40dp"
                            android:id="@+id/editText11"
                            android:background="@android:color/black"
                            android:textColor="#ffff0c00"
                            android:layout_marginLeft="5dp"
                            android:layout_marginTop="2.5dp" />

                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/button11"
                            android:id="@+id/button12"
                            android:background="@android:color/black"
                            android:textColor="#ffff0c00"
                            android:layout_marginLeft="5dp" />
                    </LinearLayout>

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        android:enabled="false"
                        android:inputType="textMultiLine"
                        android:background="@android:color/black"
                        android:textColor="#ffff0c00"
                        android:autoLink="all"
                        android:ems="10"
                        android:id="@+id/editText12"
                        android:layout_gravity="center_horizontal"
                        android:layout_weight="0.53"
                        android:layout_marginTop="5dp" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

        </FrameLayout>

    </LinearLayout>
</TabHost>
</LinearLayout>

注意 :此代碼給出了錯誤,您可以在上面的屏幕截圖中查看。

    FrameLayout tbs_content = (FrameLayout)gff.findViewById(android.R.id.tabcontent);
    LinearLayout tbs1 = (LinearLayout)tbs_content.findViewById(R.id.tab1);

這些行的第二行給出NullPointerException。 因此tbs_content為null,因為它無法在第一行加載它。

似乎您在指定ID時犯了一個錯誤: android.R.id.tabcontent最有可能是R.id.tabcontent

android.R是系統資源的R類。 R通常是您自己生成的類(除非您導入android.R)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM