我想实现谷歌在http://developer.android.com/training/implementing-navigation/nav-drawer.html#top提供的导航抽屉。
所以,我从那里下载了样本并使用了v7支持库中的appcompat库。
Changed扩展了MainActivity的Activity以扩展ActionBarActivity。 并将所有提供错误的内容更改为需要最低API等级11的支持库等效(getActionBar()---> getSupportActionBar(),getFragmentmanger()到getsupportFragmentmanager())
但现在我的应用程序在onCreate()方法的第一行super.onCreate(savedInstanceState);
崩溃了。
当我尝试调试时,它会向我提供找不到源的错误并打开ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1953
import java.util.Locale;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
/**
* This example illustrates a common usage of the DrawerLayout widget
* in the Android support library.
* <p/>
* <p>When a navigation (left) drawer is present, the host activity should detect presses of
* the action bar's Up affordance as a signal to open and close the navigation drawer. The
* ActionBarDrawerToggle facilitates this behavior.
* Items within the drawer should fall into one of two categories:</p>
* <p/>
* <ul>
* <li><strong>View switches</strong>. A view switch follows the same basic policies as
* list or tab navigation in that a view switch does not create navigation history.
* This pattern should only be used at the root activity of a task, leaving some form
* of Up navigation active for activities further down the navigation hierarchy.</li>
* <li><strong>Selective Up</strong>. The drawer allows the user to choose an alternate
* parent for Up navigation. This allows a user to jump across an app's navigation
* hierarchy at will. The application should treat this as it treats Up navigation from
* a different task, replacing the current task stack using TaskStackBuilder or similar.
* This is the only form of navigation drawer that should be used outside of the root
* activity of a task.</li>
* </ul>
* <p/>
* <p>Right side drawers should be used for actions, not navigation. This follows the pattern
* established by the Action Bar that navigation should be to the left and actions to the right.
* An action should be an operation performed on the current contents of the window,
* for example enabling or disabling a data overlay on top of the current content.</p>
*/
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_websearch:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}
的logcat
04-04 00:46:07.531: D/AndroidRuntime(719): Shutting down VM
04-04 00:46:07.531: W/dalvikvm(719): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-04 00:46:07.730: E/AndroidRuntime(719): FATAL EXCEPTION: main
04-04 00:46:07.730: E/AndroidRuntime(719): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slidingmenu/com.example.slidingmenu.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-04 00:46:07.730: E/AndroidRuntime(719): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.os.Looper.loop(Looper.java:137)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-04 00:46:07.730: E/AndroidRuntime(719): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 00:46:07.730: E/AndroidRuntime(719): at java.lang.reflect.Method.invoke(Method.java:511)
04-04 00:46:07.730: E/AndroidRuntime(719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-04 00:46:07.730: E/AndroidRuntime(719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-04 00:46:07.730: E/AndroidRuntime(719): at dalvik.system.NativeStart.main(Native Method)
04-04 00:46:07.730: E/AndroidRuntime(719): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-04 00:46:07.730: E/AndroidRuntime(719): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
04-04 00:46:07.730: E/AndroidRuntime(719): at com.example.slidingmenu.MainActivity.onCreate(MainActivity.java:83)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.app.Activity.performCreate(Activity.java:4465)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-04 00:46:07.730: E/AndroidRuntime(719): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-04 00:46:07.730: E/AndroidRuntime(719): ... 11 more
在清单中进行更改后,Logcat
04-04 00:57:19.443: E/AndroidRuntime(365): FATAL EXCEPTION: main
04-04 00:57:19.443: E/AndroidRuntime(365): android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
04-04 00:57:19.443: E/AndroidRuntime(365): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.ListView.makeAndAddView(ListView.java:1727)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.ListView.fillDown(ListView.java:652)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.ListView.fillFromTop(ListView.java:709)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.ListView.layoutChildren(ListView.java:1580)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.View.layout(View.java:7035)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:767)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.View.layout(View.java:7035)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.View.layout(View.java:7035)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.View.layout(View.java:7035)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.View.layout(View.java:7035)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.View.layout(View.java:7035)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.os.Looper.loop(Looper.java:123)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 00:57:19.443: E/AndroidRuntime(365): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 00:57:19.443: E/AndroidRuntime(365): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 00:57:19.443: E/AndroidRuntime(365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 00:57:19.443: E/AndroidRuntime(365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 00:57:19.443: E/AndroidRuntime(365): at dalvik.system.NativeStart.main(Native Method)
04-04 00:57:19.443: E/AndroidRuntime(365): Caused by: java.lang.reflect.InvocationTargetException
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.TextView.<init>(TextView.java:321)
04-04 00:57:19.443: E/AndroidRuntime(365): at java.lang.reflect.Constructor.constructNative(Native Method)
04-04 00:57:19.443: E/AndroidRuntime(365): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
04-04 00:57:19.443: E/AndroidRuntime(365): ... 35 more
04-04 00:57:19.443: E/AndroidRuntime(365): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/abc_ic_ab_back_holo_dark.png from drawable resource ID #0x0
04-04 00:57:19.443: E/AndroidRuntime(365): at android.content.res.Resources.loadDrawable(Resources.java:1714)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.view.View.<init>(View.java:1885)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.widget.TextView.<init>(TextView.java:327)
04-04 00:57:19.443: E/AndroidRuntime(365): ... 39 more
04-04 00:57:19.443: E/AndroidRuntime(365): Caused by: java.io.FileNotFoundException: res/drawable-mdpi/abc_ic_ab_back_holo_dark.png
04-04 00:57:19.443: E/AndroidRuntime(365): at android.content.res.AssetManager.openNonAssetNative(Native Method)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.content.res.AssetManager.openNonAsset(AssetManager.java:405)
04-04 00:57:19.443: E/AndroidRuntime(365): at android.content.res.Resources.loadDrawable(Resources.java:1706)
04-04 00:57:19.443: E/AndroidRuntime(365): ... 42 more
activity_main.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.
-->
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<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. -->
<FrameLayout
android:id="@+id/content_frame"
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
<!--
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"/>
fragment_planet.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.
-->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp" />
答案 0 :(得分:5)
你应该使用Theme.AppCompat作为主题。
请使用manifest.xml更新您的theme.xml并设置主题。
theme.xml:
<!-- Application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
的manifest.xml:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"...
我建议你忘记Android 2.2 :)没有人再使用它了。请尝试清理项目。
您无法在API14 reference下使用android:attr/textAppearanceListItemSmall
。
答案 1 :(得分:0)
检查您的Android清单,您使用的是Holo主题还是类似的东西? 尝试将其放在清单文件中的活动标记中:
android:theme="@style/Theme.AppCompat"
答案 2 :(得分:0)
我解决了这个问题。 这是我的 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:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff" />
你不能使用这些属性。在Android 2.2中
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"