强制关闭我的tabhost

时间:2012-01-23 02:57:16

标签: java android android-tabhost

为什么代码中有错误?我在相同的包中调用两个类。一个是“BatteryInformation.java”,它扩展了Activity,它的功能是获取Battery Info,下一个是“TaskKiller.java”,它还扩展了Activity,它的功能是杀死正在运行的应用程序。这两个类没有错误,我没有这个UI运行它,它运作良好。你能帮帮我吗?

public class BatteryDoctorSaverActivity extends Activity {
    TabHost tabHost;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         tabHost = (TabHost) findViewById(R.id.tabhost);

        // Resources res = getResources(); // Resource object to get Drawables
        TabHost.TabSpec spec; // Resusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, BatteryInformation.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Battery Information").setIndicator("")
                .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, TaskKiller.class);
        spec = tabHost.newTabSpec("Task Killer").setIndicator("TaskKiller")
                .setContent(intent);
        tabHost.addTab(spec);
            tabHost.setCurrentTab(0);

    }
}

这是LogCat:

01-23 10:30:55.949: D/AndroidRuntime(12278): Shutting down VM
01-23 10:30:55.949: W/dalvikvm(12278): threadid=1: thread exiting with uncaught exception (group=0x40018578)
01-23 10:30:55.949: E/AndroidRuntime(12278): FATAL EXCEPTION: main
01-23 10:30:55.949: E/AndroidRuntime(12278): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=batterydoctorsaver.thesis.BatteryInformation }
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.app.Activity.startActivityForResult(Activity.java:2827)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.app.Activity.startActivity(Activity.java:2933)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at batterydoctorsaver.thesis.BatteryDoctorSaverActivity$1.onClick(BatteryDoctorSaverActivity.java:22)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.view.View.performClick(View.java:2485)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.view.View$PerformClick.run(View.java:9080)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.os.Handler.handleCallback(Handler.java:587)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.os.Looper.loop(Looper.java:130)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at java.lang.reflect.Method.invokeNative(Native Method)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at java.lang.reflect.Method.invoke(Method.java:507)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-23 10:30:55.949: E/AndroidRuntime(12278):    at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮助我。我将这个用户界面叠加了近2天。我不知道该怎么办。请帮我。感谢帮助:))

@Lucifer这是我的AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="batterydoctorsaver.thesis"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".BatteryDoctorSaverActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <activity
        android:label="@string/app_name"
        android:name=".BatteryInformation" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <!-- <activity android:name = ".BatteryInformation"/> -->

</manifest>

@Lucifer先生,先生:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="batterydoctorsaver.thesis"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".BatteryDoctorSaverActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".BatteryInformation" />
        <activity android:name=".TaskKiller" />
    </application>

</manifest>

这是我的代码,可能是BatterDoctorSaverActivity.java:

package batterydoctorsaver.thesis;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import batterydoctorsaver.thesis.BatteryInformation;
import batterydoctorsaver.thesis.TaskKiller;

public class BatteryDoctorSaverActivity extends TabActivity {
    TabHost tabHost;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tabHost = getTabHost();
        tabHost.setup();
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("Battery Information")
                .setContent(new Intent(this, BatteryInformation.class)));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Task Killer")
                .setContent(new Intent(this, TaskKiller.class)));

        // tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Updates").setContent(
        // new Intent(this, DownloadList.class)));

    }
}

1 个答案:

答案 0 :(得分:1)

您需要将活动添加到AndroidManifest.xml文件中。所有活动都必须有该文件的条目。

从代码中删除此行

<activity
        android:label="@string/app_name"
        android:name=".BatteryInformation" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

而是使用以下行。

<activity> android:name=.BatteryInformation />