0

我试图在我的 Android + HoloEverywhere 应用程序上实现抽屉菜单。

我使用http://developer.android.com/training/implementing-navigation/nav-drawer.html上描述的方法开始。唯一的区别是我导入了一个 org.holoeverywhere.widget.ListView 而不是经典的 ListView。

这是我的 MainActivity :

import android.support.v4.app.FragmentManager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.ViewById;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.widget.ArrayAdapter;
import org.holoeverywhere.widget.DrawerLayout;
import org.holoeverywhere.widget.ListView;

@EActivity(R.layout.activity_main)
public class MainActivity
    extends Activity {


// Declare Variables
ActionBar mActionBar;
@ViewById(R.id.drawer_layout)
DrawerLayout mDrawer;
@ViewById(R.id.left_drawer)
ListView mDrawerList;
private String[] mPlanetTitles;

@AfterViews
protected void useElements() {


    // Activate Navigation Mode Tabs
    mActionBar = getSupportActionBar();

    // Locate ViewPager in activity_main.xml
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);

    // Activate Fragment Manager
    FragmentManager fm = getSupportFragmentManager();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles);

    // Set the adapter for the list view
    mDrawerList.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_main, menu);
    return super.onCreateOptionsMenu(menu);
}

}

还有我的activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<org.holoeverywhere.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</org.holoeverywhere.widget.DrawerLayout>

编辑

抽屉列表项.xml:

    <!--
  Copyright 2013 The Android Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

这段代码应该向我展示里面有行星列表的抽屉。它在 Android 4.x 设备上运行良好,因为不使用 HoloEverywhere 类。但是在 2.x 上它会崩溃打开,但出现以下异常:

11-25 14:09:24.771: E/AndroidRuntime(21797): FATAL EXCEPTION: main
11-25 14:09:24.771: E/AndroidRuntime(21797): android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.LayoutInflater._createView(LayoutInflater.java:382)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.LayoutInflater.onCreateView(LayoutInflater.java:594)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.LayoutInflater.createViewFromTag(LayoutInflater.java:444)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:529)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.LayoutInflater.inflate(LayoutInflater.java:488)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:184)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.ArrayAdapter.getView(ArrayAdapter.java:256)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.ListAdapterWrapper.getView(ListAdapterWrapper.java:83)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.AbsListView.obtainView(AbsListView.java:1519)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.ListView.makeAndAddView(ListView.java:1749)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.ListView.fillDown(ListView.java:674)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.ListView.fillFromTop(ListView.java:731)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.ListView.layoutChildren(ListView.java:1602)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.AbsListView.onLayout(AbsListView.java:1349)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.ListView.onLayout(ListView.java:552)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.layout(View.java:7320)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:714)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.layout(View.java:7320)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.layout(View.java:7320)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.layout(View.java:7320)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.layout(View.java:7320)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.LinearLayout.setChildFrame(LinearLayout.java:1126)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.LinearLayout.layoutVertical(LinearLayout.java:583)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.LinearLayout.onLayout(LinearLayout.java:1096)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.layout(View.java:7320)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.layout(View.java:7320)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.layout(View.java:7320)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1162)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1882)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.os.Looper.loop(Looper.java:130)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.app.ActivityThread.main(ActivityThread.java:3701)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at java.lang.reflect.Method.invoke(Method.java:507)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at dalvik.system.NativeStart.main(Native Method)
11-25 14:09:24.771: E/AndroidRuntime(21797): Caused by: java.lang.reflect.InvocationTargetException
11-25 14:09:24.771: E/AndroidRuntime(21797):    at java.lang.reflect.Constructor.constructNative(Native Method)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.LayoutInflater._createView(LayoutInflater.java:358)
11-25 14:09:24.771: E/AndroidRuntime(21797):    ... 41 more
11-25 14:09:24.771: E/AndroidRuntime(21797): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x10102fd a=-1}
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.content.res.Resources.loadDrawable(Resources.java:1692)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.view.View.<init>(View.java:1975)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at android.widget.TextView.<init>(TextView.java:370)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.TextView.<init>(TextView.java:133)
11-25 14:09:24.771: E/AndroidRuntime(21797):    at org.holoeverywhere.widget.TextView.<init>(TextView.java:129)
11-25 14:09:24.771: E/AndroidRuntime(21797):    ... 44 more
11-25 14:09:24.831: W/ActivityManager(265):   Force finishing activity com.board/.ui.activity.MainActivity_
11-25 14:09:25.341: W/ActivityManager(265): Activity pause timeout for HistoryRecord{2b3d55d8 com.board/.ui.activity.MainActivity_}
11-25 14:09:34.461: D/dalvikvm(21486): GC_EXPLICIT freed 52K, 49% free 2759K/5379K, external 1685K/2133K, paused 94ms
11-25 14:09:35.391: W/ActivityManager(265): Activity destroy timeout for HistoryRecord{2b3d55d8 com.board/.ui.activity.MainActivity_}
11-25 14:09:39.471: D/dalvikvm(21499): GC_EXPLICIT freed 302K, 45% free 3316K/6023K, external 1757K/2133K, paused 106ms
11-25 14:09:44.461: D/dalvikvm(21544): GC_EXPLICIT freed 143K, 48% free 2862K/5447K, external 1685K/2133K, paused 95ms
11-25 14:09:49.471: D/dalvikvm(21578): GC_EXPLICIT freed 806K, 49% free 3462K/6663K, external 1685K/2133K, paused 108ms
11-25 14:09:59.291: D/dalvikvm(7039): GC_EXPLICIT freed 6K, 46% free 3389K/6215K, external 1685K/2133K, paused 138ms
11-25 14:10:00.131: D/dalvikvm(1091): GC_EXPLICIT freed 3K, 49% free 2786K/5379K, external 1845K/2133K, paused 83ms
11-25 14:10:00.181: D/dalvikvm(343): GC_CONCURRENT freed 777K, 54% free 3267K/7047K, external 2542K/3017K, paused 2ms+9ms
11-25 14:10:00.201: D/dalvikvm(1091): GC_EXPLICIT freed 7K, 49% free 2788K/5379K, external 1981K/2133K, paused 28ms
11-25 14:10:04.161: D/SntpClient(265): request time failed: java.net.SocketTimeoutException: Try again
11-25 14:10:05.331: D/dalvikvm(21245): GC_EXPLICIT freed 179K, 50% free 3314K/6535K, external 1685K/2133K, paused 123ms

如果我评论该行:

mDrawerList.setAdapter(adapter);

在我的 MainActivity 中,它在 2.x 上运行,我得到一个空白抽屉。

4

1 回答 1

1

问题出在drawer_list_item.xml 中。在 Android 2.x 中,以“?android:attr/”开头的样式属性不可用。删除它们可以解决问题。

于 2013-11-27T13:46:25.300 回答