3

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

LocalActivityManager 类型已弃用

这是代码:

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 天开始就被困在这里。请帮我。

4

1 回答 1

3

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

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

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

片段 - http://developer.android.com/reference/android/app/Fragment.html FragmentManager - http://developer.android.com/reference/android/app/FragmentManager.html

于 2013-03-15T22:12:48.417 回答