-1

I'm a newbie to android programming. I'm trying to create a 4 tab application on a machine running Windows Vista x64-Bit using the eclipse IDE. After doing some thorough researching, I came across this tutorial for creating a 4 tab application. I followed the instructions closely, modifying it where necessary to make my application.

When I attempted launching it in an Android Gingerbread 2.3.3 emulator, i kept getting a forced quit. What am I doing wrong? All suggestions are welcome. Thanks in advance.

Here is my MainActivity.java code (keep in mind that the extra imports are for future purposes)

package com.riverboys.riverbook_android;

import android.annotation.TargetApi;
import android.app.TabActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle; // Makes an Android application an Android application
import android.os.StrictMode;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; // For Button element
import android.widget.TabHost; // For Tab element
import android.widget.TabHost.TabSpec;
//import android.widget.EditText; // For EditText element (to take text input)
import android.widget.TextView; // For TextView element
import com.riverboys.riverbook_android.CustomHttpClient;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends TabActivity {
    // Called when the activity is first created
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /* Show the Up button in the action bar.
        setupActionBar();*/

        Resources rsrcsRiverbook = getResources();
        TabHost tbhstRiverbook = getTabHost();
        // No need to call TabHost.Setup()

        // First (Favorites) tab
        Intent intntTab1 = new Intent().setClass(this, FavoritesActivity.class);
        TabSpec tbspcTab1 = tbhstRiverbook
                .newTabSpec("Favorites")
                .setIndicator("Favorites",  rsrcsRiverbook.getDrawable(R.drawable.icon_tab_favorites))
                .setContent(intntTab1);

        // Second (Search) tab
        Intent intntTab2 = new Intent().setClass(this, SearchActivity.class);
        TabSpec tbspcTab2 = tbhstRiverbook
                .newTabSpec("Search")
                .setIndicator("Search", rsrcsRiverbook.getDrawable(R.drawable.icon_tab_search))
                .setContent(intntTab2);

        // Third (Map) tab
        Intent intntTab3 = new Intent().setClass(this, MapActivity.class);
        TabSpec tbspcTab3 = tbhstRiverbook
                .newTabSpec("Map")
                .setIndicator("Map", rsrcsRiverbook.getDrawable(R.drawable.icon_tab_map))
                .setContent(intntTab3);

        // Fourth (River Log) tab
        Intent intntTab4 = new Intent().setClass(this, RiverLogActivity.class);
        TabSpec tbspcTab4 = tbhstRiverbook
                .newTabSpec("River Log")
                .setIndicator("River Log", rsrcsRiverbook.getDrawable(R.drawable.icon_tab_river_log))
                .setContent(intntTab4);

        //
        tbhstRiverbook.addTab(tbspcTab1);
        tbhstRiverbook.addTab(tbspcTab2);
        tbhstRiverbook.addTab(tbspcTab3);
        tbhstRiverbook.addTab(tbspcTab4);

        // Set Favorites tab as default (zero based)
        tbhstRiverbook.setCurrentTab(0);
    }
}

and here is my activity_main.xml code

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="0dp">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="0dp" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </LinearLayout>
</TabHost>

The code in the activity of the 4 tabs is identical to the following

package com.riverboys.riverbook_android;

import android.annotation.TargetApi;
import android.app.TabActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle; // Makes an Android application an Android application
import android.os.StrictMode;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; // For Button element
import android.widget.TabHost; // For Tab element
import android.widget.TabHost.TabSpec;
//import android.widget.EditText; // For EditText element (to take text input)
import android.widget.TextView; // For TextView element
import com.riverboys.riverbook_android.CustomHttpClient;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FavoritesActivity extends Activity {
    // Called when the activity is first created
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorites);
    }
}

and the code in their corresponding layout xml file is identical to the following

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FavoritesActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

This is the error information I get in logcat when the app attempts to launch in the emulator

03-21 21:10:14.136: D/AndroidRuntime(338): Shutting down VM
03-21 21:10:14.136: W/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-21 21:10:14.186: E/AndroidRuntime(338): FATAL EXCEPTION: main
03-21 21:10:14.186: E/AndroidRuntime(338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.riverboys.riverbook_android/com.riverboys.riverbook_android.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/icon_tab_favorites.xml from drawable resource ID #0x7f020005
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.os.Looper.loop(Looper.java:123)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-21 21:10:14.186: E/AndroidRuntime(338):  at java.lang.reflect.Method.invokeNative(Native Method)
03-21 21:10:14.186: E/AndroidRuntime(338):  at java.lang.reflect.Method.invoke(Method.java:507)
03-21 21:10:14.186: E/AndroidRuntime(338):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-21 21:10:14.186: E/AndroidRuntime(338):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-21 21:10:14.186: E/AndroidRuntime(338):  at dalvik.system.NativeStart.main(Native Method)
03-21 21:10:14.186: E/AndroidRuntime(338): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/icon_tab_favorites.xml from drawable resource ID #0x7f020005
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.content.res.Resources.loadDrawable(Resources.java:1697)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.content.res.Resources.getDrawable(Resources.java:581)
03-21 21:10:14.186: E/AndroidRuntime(338):  at com.riverboys.riverbook_android.MainActivity.onCreate(MainActivity.java:47)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-21 21:10:14.186: E/AndroidRuntime(338):  ... 11 more
03-21 21:10:14.186: E/AndroidRuntime(338): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'duration' attribute
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:256)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
03-21 21:10:14.186: E/AndroidRuntime(338):  at android.content.res.Resources.loadDrawable(Resources.java:1694)
03-21 21:10:14.186: E/AndroidRuntime(338):  ... 15 more
03-21 21:10:18.925: I/Process(338): Sending signal. PID: 338 SIG: 9
03-21 21:10:22.785: D/AndroidRuntime(349): Shutting down VM
03-21 21:10:22.785: W/dalvikvm(349): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-21 21:10:22.815: E/AndroidRuntime(349): FATAL EXCEPTION: main
03-21 21:10:22.815: E/AndroidRuntime(349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.riverboys.riverbook_android/com.riverboys.riverbook_android.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/icon_tab_favorites.xml from drawable resource ID #0x7f020005
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.os.Looper.loop(Looper.java:123)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-21 21:10:22.815: E/AndroidRuntime(349):  at java.lang.reflect.Method.invokeNative(Native Method)
03-21 21:10:22.815: E/AndroidRuntime(349):  at java.lang.reflect.Method.invoke(Method.java:507)
03-21 21:10:22.815: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-21 21:10:22.815: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-21 21:10:22.815: E/AndroidRuntime(349):  at dalvik.system.NativeStart.main(Native Method)
03-21 21:10:22.815: E/AndroidRuntime(349): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/icon_tab_favorites.xml from drawable resource ID #0x7f020005
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.content.res.Resources.loadDrawable(Resources.java:1697)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.content.res.Resources.getDrawable(Resources.java:581)
03-21 21:10:22.815: E/AndroidRuntime(349):  at com.riverboys.riverbook_android.MainActivity.onCreate(MainActivity.java:47)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-21 21:10:22.815: E/AndroidRuntime(349):  ... 11 more
03-21 21:10:22.815: E/AndroidRuntime(349): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'duration' attribute
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:256)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
03-21 21:10:22.815: E/AndroidRuntime(349):  at android.content.res.Resources.loadDrawable(Resources.java:1694)
03-21 21:10:22.815: E/AndroidRuntime(349):  ... 15 more
03-21 21:10:25.485: I/Process(349): Sending signal. PID: 349 SIG: 9

EDIT: Here's the code contained in the icon_tab_favorites, it's similar to the other three

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- When selected, you should use bg with grey -->
    <item android:drawable="@drawable/ic_tab_favorites"
          android:state_selected="true" />
    <!-- When not selected, you should use bg with white -->
    <item android:drawable="@drawable/ic_tab_favorites" />
</animation-list>

EDIT: For future purposes, here's the correct form of the code :)

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <!-- When selected, you should use bg with grey -->
    <item android:drawable="@drawable/ic_tab_favorites"
          android:state_selected="true"
          android:duration="200" />

    <!-- When not selected, you should use bg with white -->
    <item android:drawable="@drawable/ic_tab_favorites"
          android:duration="200" />
</animation-list>
4

1 回答 1

3

首先,您正在使用的编程方法——一个TabActivity使用选项卡的活动——已经被弃用了两年多。

其次,您的特定问题来自res/drawable/icon_tab_favorites.xml格式错误的 XML 可绘制对象。您可以通过阅读堆栈跟踪来判断这一点,尤其是最后两行“Caused by”:

03-21 21:10:14.186: E/AndroidRuntime(338): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/icon_tab_favorites.xml from drawable resource ID #0x7f020005
03-21 21:10:14.186: E/AndroidRuntime(338): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'duration' attribute

请编辑该文件以将duration属性放在第 4 行,并考虑检查该文件的其余部分是否存在任何类似类型的问题。

于 2013-03-21T21:52:48.790 回答