修改后的Tabhost示例在启动时崩溃

时间:2010-11-27 22:24:28

标签: java android android-tabhost

所以我刚刚拥有了自己的Android设备,想要开始一些Android开发,为自己创建一个简单的任务管理器。我的想法是实现在android周围看到的标签UI,所以我偶然发现了这个:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

一切都好。从这一点开始,我需要道歉,因为我不太具体,因为我不知道问题出在哪里。发生的事情非常简单。我运行我的应用程序,我得到一个对话框,说“com.app.appname被强制关闭”。而已。我没有看到UI。

所以,没有进一步说,我实施了:

Overview.java:

public class Overview extends TabActivity {

    /** Called when the activity is first created. */
    @Override
    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

        intent = new Intent().setClass(this, ViewTasks.class);
        spec = tabHost.newTabSpec("tasks").setIndicator("Tasks",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ViewGoals.class);
        spec = tabHost.newTabSpec("goals").setIndicator("Goals",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }
}

main.xml(res / layout)

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="the.correct.package.name"
      android:versionCode="1"
      android:versionName="1.0">
      <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="vxataskmaster">
        <activity android:name="Overview" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" android:enabled="true" android:screenOrientation="unspecified">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.EDIT" />
                <action android:name="android.intent.action.PICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="ViewGoals" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" android:enabled="true" android:screenOrientation="unspecified">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.EDIT" />
                <action android:name="android.intent.action.PICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="ViewTasks" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" android:enabled="true" android:screenOrientation="unspecified">
                <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.EDIT" />
                <action android:name="android.intent.action.PICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="8" />
</manifest> 

据我所知,我修改了原始示例以使用我自己的几个类。这些类只是实现了教程站点上看到的hello world消息。

那么,我做错了什么,拜托?我似乎无法从调试器中得到任何合理的信息......它不会指向我的代码中的任何内容,无论是在模拟器上还是在我的设备上运行。

我为粘贴大量代码而道歉。如果我知道问题出在哪里,我会把它砍掉,但我不会......!

感谢。

编辑:logcat说:

11-27 22:51:32.871: ERROR/AndroidRuntime(10662): java.lang.RuntimeException: Unable to instantiate application correct.root.package.taskmaster.ataskmaster: java.lang.ClassNotFoundException: correct.root.package.taskmaster.ataskmaster in loader dalvik.system.PathClassLoader[/data/app/correct.root.package.taskmaster-1.apk]

2 个答案:

答案 0 :(得分:1)

像Falmarri建议你可能会错过AndroidManifest.xml中的活动。 ViewGoals和ViewGoals是活动吗?如果是这样,请将其添加到清单中,就像您执行概述一样。

LogCat是与Eclipse集成的Android日志工具。转到Debug视图,你应该在那里看到它。

答案 1 :(得分:0)

我修好了。不知怎的,这条线

android:id="@android:id/tabhost"
我的tabhost中缺少

。显然,这对于让事情变得非常重要!上面的答案给了我一些有用的指针(logcat),让我可以推断出这一点。