【问题标题】:Why is this crashing? And How Do I Debug?为什么会崩溃?以及如何调试?
【发布时间】:2011-08-16 05:58:57
【问题描述】:

我过去一直使用 Xcode,所以现在我正在尝试学习 Android,我正在使用 Eclipse

我遵循了http://developer.android.com/resources/tutorials/views/hello-tabwidget.html 中列出的所有步骤,但是当我在我的 LG Revolution (Froyo 2.2.1) 上实际运行代码时,我崩溃了。

我不确定如何调试,但我不知道为什么这甚至会崩溃。任何帮助将不胜感激。

我对所有 3 个选项卡都使用了相同的图像(这是我所做的唯一修改,但我认为它不会崩溃)

这是我的代码

package com.oneorangetree.shit;

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

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

        // Resource object to get drawable
        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

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

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

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

        tabHost.setCurrentTab(2);
    }
}

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.oneorangetree.shit"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloTabWidgetActivity"
                  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>

    </application>
</manifest>

【问题讨论】:

  • 我可以保证在你发布一些代码之前你不会得到任何答案。
  • 查找崩溃的最佳方法是检查 LogCat。那基本上是androids系统日志。您可以在 SDK/tools 文件夹中的 ddms 应用程序中找到它,或者通过 Window-&gt;Add view-&gt;other-&gt;LogCat 在 eclipse 中找到它。那里应该有一个异常(以红色打印,带有 AndroidRuntime 标签)。拥有后,请发布并添加相关代码。
  • 我的问题的答案将为您解答stackoverflow.com/questions/2209406/…
  • IOW,示例不完整 - 找到描述如何完成它的答案,您将启动并运行
  • @KevinDTimm - 你也许是对的。您能否发布您的 Manifest 文件作为答案,以便我接受并给予您信任?

标签: java android android-tabhost


【解决方案1】:

查看http://code.google.com/p/android/issues/detail?id=4183 中的信息并在我原来的问题中通过Ted 实施修复 - 下面是清单,但也有其他错误:

<activity android:name=".AlbumsActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar">
</activity>
<activity android:name=".ArtistsActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar">
</activity>
<activity android:name=".SongsActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar">
</activity>

【讨论】:

    【解决方案2】:

    大胆猜测:您是否将所有活动都添加到清单中?一项活动等于清单中的一项提及。

    当您创建项目时,您的主要活动默认添加到此文件中,以便系统知道可以启动您的主要活动。现在,每次您在项目中添加新活动时,您都必须在清单中添加此活动:

    <activity android:name=".HelloTabWidget" android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar">
    

    如果不是通过单击文件的左边距添加一些断点,则单击调试按钮。最后,您打开调试透视图,并使用 F8 在断点之间导航。

    希望对你有帮助,我也去过:)。

    【讨论】:

      猜你喜欢
      • 2020-05-01
      • 2018-07-12
      • 2010-10-10
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多