Android Hello Tab小部件示例问题

时间:2011-04-25 17:43:44

标签: android android-tabhost

我正在尝试从Android SDK教程中实现“标签布局”示例: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

我注意到很多人在实现这个问题时遇到了问题。我想我已经阅读了google / stackoverflow上的所有可能的链接并尝试/检查了所有内容,但仍无法使其正常工作。我仍然继续收到错误“应用程序Hello Tab Widget意外停止。请再试一次。”在发布会上。

我在这里上传了整个项目(Android 2.3.3): https://www.yousendit.com/download/VnBxak85UnFCSnF4dnc9PQ

此外,这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.kaushal.hellotabwidget"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".hellotabwidget"
                  android:label="@string/app_name">
             <!--       android:theme="@android:style/Theme.NoTitleBar">-->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".AlbumsActivity"></activity> 
        <activity android:name=".ArtistsActivity"></activity> 
        <activity android:name=".SongsActivity"></activity> 
    </application>
</manifest>

HelloTabWidget.java文件,

package com.kaushal.hellotabwidget;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class HelloTabWidget extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        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, ArtistsActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, AlbumsActivity.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                          res.getDrawable(R.drawable.ic_tab_albums))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SongsActivity.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                          res.getDrawable(R.drawable.ic_tab_songs))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
}

应用程序启动时的示例logcat,

    04-25 13:10:42.105 D/AndroidRuntime(  671): 
        04-25 13:10:42.105 D/AndroidRuntime(  671): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
        04-25 13:10:42.105 D/AndroidRuntime(  671): CheckJNI is ON
        04-25 13:10:43.135 D/AndroidRuntime(  671): Calling main entry com.android.commands.pm.Pm
        04-25 13:10:43.195 D/AndroidRuntime(  671): Shutting down VM
        04-25 13:10:43.215 I/AndroidRuntime(  671): NOTE: attach of thread 'Binder Thread #3' failed
        04-25 13:10:43.215 D/dalvikvm(  671): GC_CONCURRENT freed 100K, 72% free 296K/1024K, external 0K/0K, paused 3ms+5ms
        04-25 13:10:43.225 D/jdwp    (  671): adbd disconnected
        04-25 13:10:43.975 D/AndroidRuntime(  681): 
        04-25 13:10:43.975 D/AndroidRuntime(  681): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
        04-25 13:10:43.975 D/AndroidRuntime(  681): CheckJNI is ON
        04-25 13:10:45.065 D/AndroidRuntime(  681): Calling main entry com.android.commands.am.Am
        04-25 13:10:45.135 I/ActivityManager(   67): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.kaushal.hellotabwidget/.hellotabwidget } from pid 681
        04-25 13:10:45.205 D/AndroidRuntime(  681): Shutting down VM
        04-25 13:10:45.285 I/AndroidRuntime(  681): NOTE: attach of thread 'Binder Thread #3' failed
        04-25 13:10:45.315 D/dalvikvm(  681): GC_CONCURRENT freed 101K, 69% free 318K/1024K, external 0K/0K, paused 3ms+84ms
        04-25 13:10:45.385 D/jdwp    (  681): adbd disconnected
        04-25 13:10:45.456 I/ActivityManager(   67): Start proc com.kaushal.hellotabwidget for activity com.kaushal.hellotabwidget/.hellotabwidget: pid=690 uid=10037 gids={1015}
        04-25 13:10:46.815 D/AndroidRuntime(  690): Shutting down VM
        04-25 13:10:46.815 W/dalvikvm(  690): threadid=1: thread exiting with uncaught exception (group=0x40015560)
        04-25 13:10:46.845 E/AndroidRuntime(  690): FATAL EXCEPTION: main
        04-25 13:10:46.845 E/AndroidRuntime(  690): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kaushal.hellotabwidget/com.kaushal.hellotabwidget.hellotabwidget}: java.lang.ClassNotFoundException: com.kaushal.hellotabwidget.hellotabwidget in loader dalvik.system.PathClassLoader[/data/app/com.kaushal.hellotabwidget-2.apk]
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.os.Handler.dispatchMessage(Handler.java:99)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.os.Looper.loop(Looper.java:123)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.app.ActivityThread.main(ActivityThread.java:3683)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at java.lang.reflect.Method.invokeNative(Native Method)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at java.lang.reflect.Method.invoke(Method.java:507)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at dalvik.system.NativeStart.main(Native Method)
        04-25 13:10:46.845 E/AndroidRuntime(  690): Caused by: java.lang.ClassNotFoundException: com.kaushal.hellotabwidget.hellotabwidget in loader dalvik.system.PathClassLoader[/data/app/com.kaushal.hellotabwidget-2.apk]
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
        04-25 13:10:46.845 E/AndroidRuntime(  690):     ... 11 more
        04-25 13:10:46.855 W/ActivityManager(   67):   Force finishing activity com.kaushal.hellotabwidget/.hellotabwidget
        04-25 13:10:47.375 W/ActivityManager(   67): Activity pause timeout for HistoryRecord{40745f70 com.kaushal.hellotabwidget/.hellotabwidget}
        04-25 13:10:58.235 W/ActivityManager(   67): Activity destroy timeout for HistoryRecord{40745f70 com.kaushal.hellotabwidget/.hellotabwidget}
        04-25 13:11:05.205 D/dalvikvm(   67): GC_CONCURRENT freed 515K, 42% free 4437K/7623K, external 884K/1297K, paused 10ms+10ms
        0

请告诉我这里我做错了什么。一直试图解决这个问题超过一天但是无法使其发挥作用。

这是我第一次发布Stackoverflow。如果我错过任何发布规则,请原谅我:)。感谢所有人的帮助。

1 个答案:

答案 0 :(得分:3)

在你的清单中,替换:

<activity android:name=".hellotabwidget"

使用:

<activity android:name=".HelloTabWidget"

活动名称区分大小写(毕竟它们是类名)。