无法在Android 2.3.3版本上运行Jeremy Feinstein滑动菜单

时间:2013-11-29 14:04:25

标签: java android android-fragments android-activity

我使用Jeremy Feinstein库实现了Sliding菜单。 哪个在Android版本上运行得非常好:4.x.x

但是当我尝试在Android版本2.3.x上执行时,应用程序崩溃并出现以下错误。

11-29 19:25:52.628: E/AndroidRuntime(26836): FATAL EXCEPTION: main
11-29 19:25:52.628: E/AndroidRuntime(26836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.variouscal.calculators/com.variouscal.calculators.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.os.Looper.loop(Looper.java:130)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.app.ActivityThread.main(ActivityThread.java:3687)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at java.lang.reflect.Method.invoke(Method.java:507)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at dalvik.system.NativeStart.main(Native Method)
11-29 19:25:52.628: E/AndroidRuntime(26836): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.setMenu(SlidingMenu.java:375)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at com.variouscal.calculators.MainActivity.onCreate(MainActivity.java:30)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-29 19:25:52.628: E/AndroidRuntime(26836):    ... 11 more
11-29 19:25:52.628: E/AndroidRuntime(26836): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.variouscal.calculators-1.apk]
11-29 19:25:52.628: E/AndroidRuntime(26836):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-29 19:25:52.628: E/AndroidRuntime(26836):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
11-29 19:25:52.628: E/AndroidRuntime(26836):    ... 18 more

以下是 MainActivity.java SlidingMenuFragment.java

MainActivity.java

package com.variouscal.calculators;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class MainActivity extends Activity {

    private SlidingMenu slidingMenu;

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

        slidingMenu = new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
        slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        slidingMenu.setFadeDegree(0.35f);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.slidingmenu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.slidingMenu.toggle();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        if (slidingMenu.isMenuShowing()) {
            slidingMenu.toggle();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ( keyCode == KeyEvent.KEYCODE_MENU ) {
            this.slidingMenu.toggle();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

SlidingMenuFragment.java

package com.variouscal.calculators;

import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

public class SlidingMenuFragment extends Fragment implements ExpandableListView.OnChildClickListener {

    private ExpandableListView sectionListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        List<Section> sectionList = createMenu();

        View view = inflater.inflate(R.layout.slidingmenu_fragment, container, false);
        this.sectionListView = (ExpandableListView) view.findViewById(R.id.slidingmenu_view);
        this.sectionListView.setGroupIndicator(null);

        SectionListAdapter sectionListAdapter = new SectionListAdapter(this.getActivity(), sectionList);
        this.sectionListView.setAdapter(sectionListAdapter);

        this.sectionListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
              @Override
              public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
              }
            });

        this.sectionListView.setOnChildClickListener(this);

        int count = sectionListAdapter.getGroupCount();
        for (int position = 0; position < count; position++) {
            this.sectionListView.expandGroup(position);
        }

        return view;
    }

    private List<Section> createMenu() {
        List<Section> sectionList = new ArrayList<Section>();

        Section oDemoSection = new Section("Calculators");
        oDemoSection.addSectionItem(101,"Love Calculator", "slidingmenu_friends");
        oDemoSection.addSectionItem(102, "Age Calculator", "slidingmenu_airport");
        oDemoSection.addSectionItem(103, "BMI Calculator", "slidingmenu_airport");

        Section oGeneralSection = new Section("General");
        oGeneralSection.addSectionItem(201, "Settings", "slidingmenu_settings");
        oGeneralSection.addSectionItem(202, "Rate this app", "slidingmenu_rating");
        oGeneralSection.addSectionItem(203, "Eula", "slidingmenu_eula");
        oGeneralSection.addSectionItem(204, "Quit", "slidingmenu_quit");

        sectionList.add(oDemoSection);
        sectionList.add(oGeneralSection);
        return sectionList;
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {

        switch ((int)id) {
        case 101:
            Intent intent = new Intent("com.variouscal.calculators.LoveCalculator");
            startActivity(intent);
            break;
        case 102:
            Intent intent1 = new Intent("com.variouscal.calculators.AgeCalculator");
            startActivity(intent1);
            break;
        case 103:
            Intent intent2 = new Intent("com.variouscal.calculators.BmiCalculator");
            startActivity(intent2);
            break;
        case 202:
            //TODO
            break;
        case 203:
            //TODO
            break;
        case 204:
            //TODO
            break;
        }

        return false;
    }
}

如何使其适用于Android版本:2.3.x。

由于

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码。这就是我做到的,它对我有用。希望这段代码可以帮助你。

package com.example.calculators;

import android.os.Bundle;
import android.view.Menu;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setBehindContentView(R.layout.slidingmenu);
        getSlidingMenu().setMode(SlidingMenu.LEFT);
        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        getSlidingMenu().setFadeDegree(0.35f);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

这是我的slidingmenu.xml

<?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragback"
        android:name="com.example.calculators.ListFrag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>