android - LocalActivityManager 在尝试添加选项卡时显示警告和无法在 Activity-Application Stop 中使用 TabHost

标签 android tabs

这是什么错误?我在 LocalActivityManager 上收到警告:

The type LocalActivityManager is deprecated

代码如下:

import android.os.Bundle;
import android.app.Activity;
import android.app.LocalActivityManager; **// Showing Cancel Warning**
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;


public class MainActivity extends Activity {


TabHost tabHost;
@SuppressWarnings("deprecation")
LocalActivityManager mLocalActivityManager; **// Showing Cancel Warning**

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected void onResume() {
    mLocalActivityManager.dispatchResume(); 
    super.onResume();
}

@Override
protected void onPause() {
    mLocalActivityManager.dispatchPause(isFinishing());
    super.onPause();
}

@SuppressWarnings("deprecation")
private void initTabs(Bundle savedInstanceState) {
    tabHost = (TabHost) findViewById(android.R.id.tabhost);     // The activity TabHost
    mLocalActivityManager = new LocalActivityManager(this, false);
    mLocalActivityManager.dispatchCreate(savedInstanceState);
    tabHost.setup(mLocalActivityManager);

    TabHost.TabSpec spec;                               // Resusable TabSpec for each tab
    Intent intent;                                      // Reusable Intent for each tab        

    intent = new Intent("com.plastemart.plastemartandroid");
    spec = tabHost.newTabSpec("text").setIndicator("Latest")
                  .setContent(intent);
    tabHost.addTab(spec);      **// Getting Error Unfortunitly Application Stopped...Why?**

    intent = new Intent("com.plastemart.plastemartandroid");
    spec = tabHost.newTabSpec("text").setIndicator("Latest")
                  .setContent(intent);
    tabHost.addTab(spec);


    intent = new Intent("com.plastemart.plastemartandroid");
    spec = tabHost.newTabSpec("text").setIndicator("Latest")
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent("com.plastemart.plastemartandroid");
    spec = tabHost.newTabSpec("text").setIndicator("Latest")
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(4);
}

我的 XML 如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:context=".MainActivity" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="36dp"
    android:background="@drawable/bg_plastemart_title" >

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Plastemart.Com"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ffffff" />
</RelativeLayout>


<TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

            <HorizontalScrollView
                android:id="@+id/horizontalScrollView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:scrollbars="none" >

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

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

            <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
            </LinearLayout>

        </LinearLayout>
</TabHost>

我哪里错了?我是 Android 的初学者。如果找到解决方案,请详细说明。 我通过在互联网上查找步骤和解决方案来编写上面的代码,但过去 4 天我一直被困在这里。 请帮助我。

最佳答案

此类在 API 级别 13 中已弃用。建议使用 Fragment 和 FragmentManager。如果您不打算使用 Fragment 和 FragmentManager API,那么您需要接受此警告消息。

请理解,这只是一条警告消息,并非错误消息。所以你仍然可以忍受这个。但推荐的选项是使用 Fragment 和 FragmentManager API

您可以在此处查看有关弃用类的更多详细信息 http://developer.android.com/reference/android/app/LocalActivityManager.html

fragment - http://developer.android.com/reference/android/app/Fragment.html fragment 管理器 - http://developer.android.com/reference/android/app/FragmentManager.html

关于android - LocalActivityManager 在尝试添加选项卡时显示警告和无法在 Activity-Application Stop 中使用 TabHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14745597/

相关文章:

android - greenDAO 生成器给出了没有意义的控制台错误

css - Twitter bootstrap 选项卡的内容正在混合

facebook - 仅为我的公司页面创建 Facebook 标签

android - 如何更新 Android 选项卡上的徽章值?

css - 选项卡和隐藏内容

android - 编辑文本并将其放入 ListView

android - 如何在 Android 的不同 View 中并排在一个布局中同时播放多个视频文件

android - 在 onActivityResult 中收到 "content://"Uri 后从 MediaStore 获取 PDF?

android - 在 Android Studio 中从应用程序中删除 AdMob

android - 如何添加带有 fragment 的新选项卡