2

こんにちは、minSDKVersion 8 から 17 で Android Navigation Drawer を使用しています。プログラムでは、アクション バー コンポーネントを使用してナビゲーション ドロワーを使用しています。4.1v Android デバイスでプログラムを実行すると正常に実行されますが、2.3v Android デバイスで同じプログラムを実行しようとすると、例外が表示されます。これが私のスニペットの詳細です。

MainActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends ActionBarActivity{

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mEventOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = "Event Option";//getTitle();
        mEventOptions = getResources().getStringArray(R.array.event_options);
        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, mEventOptions));
        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(false);/*!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()) {

        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 selected item and title, then close the drawer

        switch (position) {

        case 0:

            getFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new MainActivityFragment2())
            .commit();
            break;

        case 1:

            getFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new MainActivityFragment2())
            .commit();
            break;

        case 2:

            getFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new MainActivityFragment2())
            .commit();
            break;

        case 3:

            getFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new MainActivityFragment2())
            .commit();
            break;

        }
        mDrawerList.setItemChecked(position, true);
        setTitle(mEventOptions[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);
    }

}

カタログ:

10-07 02:06:29.338: E/AndroidRuntime(387): FATAL EXCEPTION: main
10-07 02:06:29.338: E/AndroidRuntime(387): java.lang.NoSuchMethodError: in.wptrafficanalyzer.actionbarcompatdemo.MainActivity.getFragmentManager
10-07 02:06:29.338: E/AndroidRuntime(387):  at in.wptrafficanalyzer.actionbarcompatdemo.MainActivity.selectItem(MainActivity.java:144)
10-07 02:06:29.338: E/AndroidRuntime(387):  at in.wptrafficanalyzer.actionbarcompatdemo.MainActivity.onCreate(MainActivity.java:74)
10-07 02:06:29.338: E/AndroidRuntime(387):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-07 02:06:29.338: E/AndroidRuntime(387):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-07 02:06:29.338: E/AndroidRuntime(387):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-07 02:06:29.338: E/AndroidRuntime(387):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-07 02:06:29.338: E/AndroidRuntime(387):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-07 02:06:29.338: E/AndroidRuntime(387):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 02:06:29.338: E/AndroidRuntime(387):  at android.os.Looper.loop(Looper.java:130)
10-07 02:06:29.338: E/AndroidRuntime(387):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-07 02:06:29.338: E/AndroidRuntime(387):  at java.lang.reflect.Method.invokeNative(Native Method)
10-07 02:06:29.338: E/AndroidRuntime(387):  at java.lang.reflect.Method.invoke(Method.java:507)
10-07 02:06:29.338: E/AndroidRuntime(387):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-07 02:06:29.338: E/AndroidRuntime(387):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-07 02:06:29.338: E/AndroidRuntime(387):  at dalvik.system.NativeStart.main(Native Method)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.actionbarcompatdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name="in.wptrafficanalyzer.actionbarcompatdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity$PlanetFragment"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest> 

activity_main.xml

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

1 に答える 1

3

getFragmentManagerの出現箇所をすべて に置き換えますgetSupportFragmentManager。それに応じて、アプリの他の部分を置き換える必要がある場合があります。

于 2013-10-07T07:22:34.293 に答える