java - 无法解析 TabHost 方法上的符号 android

标签 java android android-layout android-studio

我是 Android 开发新手,我正在使用三个选项卡进行 TabHost 布局,因此我制作了 xml 文件:

<TabHost
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ntm">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"/>

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

                <LinearLayout
                    android:id="@+id/Present"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TableLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TableRow
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <TextView
                                android:text="Simple"
                                android:gravity="center"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/textView24"
                                android:layout_weight="1"/>

                            <TextView
                                android:text="Continuous"
                                android:gravity="center"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/textView23"
                                android:layout_weight="1"/>

                            <TextView
                                android:text="Perfect Simple"
                                android:gravity="center"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/textView15"
                                android:layout_weight="1"/>

                            <TextView
                                android:text="Perfect Continuous"
                                android:gravity="center"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/textView8"
                                android:layout_weight="1"/>
                        </TableRow>

                        <TableRow
                            android:layout_width="match_parent"
                            android:layout_height="match_parent" />

                    </TableLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/Past"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:text="NTM1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView25" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/Future"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:text="NTM2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView26" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

这是我在相关 Activity 中初始化选项卡的代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time);

    }
    TabHost tab =(TabHost) findViewById(R.id.ntm);
    tab.setup();

    TabHost.TabSpec spec1 = tab.newTabSpec("PRESENT");
    spec1.setIndicator("PRESENT");
    spec1.setContent(R.id.Present);
    tab.addTab(spec1);

    TabHost.TabSpec spec2 = tab.newTabSpec("PAST");
    spec2.setIndicator("PAST");
    spec2.setContent(R.id.Past);
    tab.addTab(spec2);

    TabHost.TabSpec spec3 = tab.newTabSpec("FUTURE");
    spec3.setIndicator("FUTURE");
    spec3.setContent(R.id.Future);
    tab.addTab(spec3);

我收到此错误:

cannot resolve symbol

在每个方法上,例如 tab.setup() 或适用于 TabHostTabSpec 对象的所有方法。

我尝试将代码复制/粘贴到主 Activity 中,没有出现任何错误,但应用程序立即崩溃

最佳答案

我发现了我的错误,我没有将初始化代码放入方法onCreate我没有更多错误

关于java - 无法解析 TabHost 方法上的符号 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42422865/

相关文章:

java - 使用 gdata 检索 Youtube 缩略图 URL?

android - minHeight 不适用于 Linearlayout 中的权重 =“1”

android - 我可以在 Android 布局中为文本添加下划线吗?

java - 在哪里可以找到从 tomcat :run? 的 JSP 生成的 Servlet

java - Spring Boot 延迟 Flyway 初始化循环依赖

android - 如何在父 RecyclerView 中有一个 ListView/RecyclerView?

android - 单击通知打开文件?

java - 在 Eclipse 中运行多个文件

java - 我可以在迭代 HashTable 时更改其对象的内部结构吗?

android - AdMob "Not enough space to show ad"错误