然而,我看了这个问题,它没有帮助解决我的问题:
我在我的应用程序中使用ActionBarSherlock(版本4.4)以便在旧版本中兼容。我的应用程序在4.1.2上运行完美,但它在2.3.3上崩溃了。该应用程序一直运行到mDrawerLayout.closeDrawer(mDrawerList);
,然后崩溃。我无法弄清楚是什么导致它在旧版本上崩溃。我觉得这是因为它试图使用那个版本中不存在的东西,但我无法确定它的用途。有没有人看到任何看似错误的代码?
logcat中的错误:
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x10102fd a=-1}
Logcat错误
08-15 21:01:04.967: E/AndroidRuntime(789): FATAL EXCEPTION: main
08-15 21:01:04.967: E/AndroidRuntime(789): android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
08-15 21:01:04.967: E/AndroidRuntime(789): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.ListView.makeAndAddView(ListView.java:1745)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.ListView.fillDown(ListView.java:670)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.ListView.fillFromTop(ListView.java:727)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.ListView.layoutChildren(ListView.java:1598)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.View.layout(View.java:7175)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.View.layout(View.java:7175)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.View.layout(View.java:7175)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.View.layout(View.java:7175)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.View.layout(View.java:7175)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.View.layout(View.java:7175)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.os.Looper.loop(Looper.java:123)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-15 21:01:04.967: E/AndroidRuntime(789): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 21:01:04.967: E/AndroidRuntime(789): at java.lang.reflect.Method.invoke(Method.java:507)
08-15 21:01:04.967: E/AndroidRuntime(789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-15 21:01:04.967: E/AndroidRuntime(789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-15 21:01:04.967: E/AndroidRuntime(789): at dalvik.system.NativeStart.main(Native Method)
08-15 21:01:04.967: E/AndroidRuntime(789): Caused by: java.lang.reflect.InvocationTargetException
08-15 21:01:04.967: E/AndroidRuntime(789): at java.lang.reflect.Constructor.constructNative(Native Method)
08-15 21:01:04.967: E/AndroidRuntime(789): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
08-15 21:01:04.967: E/AndroidRuntime(789): ... 35 more
08-15 21:01:04.967: E/AndroidRuntime(789): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x10102fd a=-1}
08-15 21:01:04.967: E/AndroidRuntime(789): at android.content.res.Resources.loadDrawable(Resources.java:1681)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.view.View.<init>(View.java:1951)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.TextView.<init>(TextView.java:344)
08-15 21:01:04.967: E/AndroidRuntime(789): at android.widget.TextView.<init>(TextView.java:337)
08-15 21:01:04.967: E/AndroidRuntime(789): ... 38 more
来自values文件夹的styles.xml:
<resources>
<style name="AppBaseTheme" parent="Theme.Sherlock.Light.DarkActionBar">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
Java文件
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG,"Set Content View");
mTitle = (String) getTitle();
ChemTitles = getResources().getStringArray(R.array.chemistrycalcstrings);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, ChemTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Log.d(TAG,"Set Display Home");
getSupportActionBar().setHomeButtonEnabled(true);
Log.d(TAG,"Set Home Button");
Log.d(TAG,"Setting ActbarDrawerToggle");
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view){
getSupportActionBar().setTitle(mTitle);
getSherlock().dispatchInvalidateOptionsMenu();
Log.d(TAG,"Drawer CLosing");
}
public void onDrawerOpened(View drawerView){
getSupportActionBar().setTitle(mDrawerTitle);
getSherlock().dispatchInvalidateOptionsMenu();
Log.d(TAG,"Drawer Opening");
}
};
Log.d(TAG,"Set ActbarDrawerToggle");
mDrawerLayout.setDrawerListener(mDrawerToggle);
Log.d(TAG,"Set ActbarDrawerToggle Listener");
if (savedInstanceState == null){
selectItem(0);
}
}
private void selectItem(int position){
SherlockFragment frag = new TestFrament();
Bundle args = new Bundle();
args.putInt(TestFrament.ITEM_SEL, position);
frag.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
mDrawerList.setItemChecked(position, true);
setTitle(ChemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
修改/更新
我仔细检查了列表视图mDrawerList
所涉及的所有部分,并且应用程序直接启动了一个工作操作栏,抽屉里没有列表视图。
布局activity_main.xml
<android.support.v4.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">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<LinearLayout
android:id="@+id/content_frame"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
drawer_list_item.xml
<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"/>
test_fragment_layout.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragTextSherlock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:padding="32dp" />
TestFragment.java
public class TestFrament extends SherlockFragment {
public static final String ITEM_SEL = "item_selected";
private TextView fragtv;
public TestFrament() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_fragment_layout, container, false);
int i = getArguments().getInt(ITEM_SEL);
fragtv = (TextView) rootView.findViewById(R.id.fragTextSherlock);
fragtv.setText(String.valueOf(i));
return rootView;
}
}
答案 0 :(得分:5)
问题是编译器没有告诉我我使用的项目不属于兼容性,而且在2.3.3中不可用
看看我必须删除的行:
drawer_list_item.xml
<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" <-Removed
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator" <-Removed
android:minHeight="?android:attr/listPreferredItemHeightSmall"/> <-Removed
答案 1 :(得分:5)
要支持较低版本,而不是删除以下三个参数,
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
您实际上可以用相同的值/资源替换。
可以从中获得等效值 https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
现在,
a) android:textAppearance="@android:style/TextAppearance.Medium"
<强> b)中强>
在上面的项目中浏览hdpi,mdpi等资源并获取名为 list_activated_holo.9.png的文件
最后
android:background="@drawable/activated_background_holo_dark"
c)从获得的等效值中,我们知道 listPreferredItemHeightSmall 是48dip
android:minHeight="48dip"
答案 2 :(得分:1)
这通常是由于res文件夹中的可绘制资源不可用而发生的。有时你只是将drawable从其他目录复制到你的项目的drawable文件夹,但忘记从IDE的项目浏览器中添加这个drawable。