二进制XML文件行#4:<item>标记需要一个&#39;持续时间&#39;属性</项目>

时间:2013-03-21 21:46:40

标签: android tabs duration

我是android编程的新手。我正在尝试使用eclipse IDE在运行Windows Vista x64-Bit的计算机上创建一个4选项卡应用程序。在做了一些彻底的研究之后,我遇到了this教程,用于创建一个4选项卡应用程序。我仔细按照说明进行了修改,并在必要时对其进行了修改。

当我尝试在Android Gingerbread 2.3.3模拟器中启动它时,我不断被强制退出。我究竟做错了什么?欢迎所有建议。提前谢谢。

这是我的MainActivity.java代码(请记住,额外的导入是为了将来的目的)

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);
    }
}

这是我的activity_main.xml代码

<?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>

4个标签的活动中的代码与以下内容相同

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);
    }
}

并且相应布局xml文件中的代码与以下内容相同

<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>

当应用程序尝试在模拟器中启动时,这是我在logcat中获得的错误信息

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
编辑:这是icon_tab_favorites中包含的代码,它类似于其他三个

<?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>

编辑:为了将来的目的,这里是正确的代码形式:)

<?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>

1 个答案:

答案 0 :(得分:3)

首先,您正在使用的编程方法 - TabActivity使用标签的活动 - 已被弃用超过两年。

其次,您的特定问题来自XML drawable res/drawable/icon_tab_favorites.xml,它格式不正确。您可以通过读取堆栈跟踪来判断,特别是最后两行“由...引起”:

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

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