我正在为 android 开发 SlidingPaneLayout。我正在尝试在 android 2.3.3 中实现它。但问题是它给膨胀带来了问题。我遵循了一个教程。它的 minSdk 为 10。如果我尝试使用以下版本实现相同的功能。它给出了错误。代码是这样的。
package com.example.slidpanelayout;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.Menu;
import android.view.View;
public class MainActivity extends FragmentActivity {
SlidingPaneLayout Pane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Pane = (SlidingPaneLayout) findViewById(R.id.sp);
Pane.setPanelSlideListener(new PaneListener());
if(!Pane.isSlideable()){
getFragmentManager().findFragmentById(R.id.leftpane).setHasOptionsMenu(false);
getFragmentManager().findFragmentById(R.id.rightpane).setHasOptionsMenu(true);
}
}
private class PaneListener implements SlidingPaneLayout.PanelSlideListener{
@Override
public void onPanelClosed(View arg0) {
// TODO Auto-generated method stub
System.out.println("Panel closed");
getFragmentManager().findFragmentById(R.id.leftpane).setHasOptionsMenu(false);
getFragmentManager().findFragmentById(R.id.rightpane).setHasOptionsMenu(true);
}
@Override
public void onPanelOpened(View arg0) {
// TODO Auto-generated method stub
System.out.println("Panel Opened");
getFragmentManager().findFragmentById(R.id.leftpane).setHasOptionsMenu(true);
getFragmentManager().findFragmentById(R.id.rightpane).setHasOptionsMenu(false);
}
@Override
public void onPanelSlide(View arg0, float arg1) {
// TODO Auto-generated method stub
System.out.println("Sliding");
}
}
@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;
}
}
主要的.xml
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sp">
<fragment
android:id="@+id/leftpane"
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_gravity="left"/>
<fragment
android:id="@+id/rightpane"
android:layout_width="350dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"/>
</android.support.v4.widget.SlidingPaneLayout>
我尝试了互联网上可用的所有建议。请帮帮我。日志是
11-13 02:30:47.439: E/AndroidRuntime(1597): FATAL EXCEPTION: main
11-13 02:30:47.439: E/AndroidRuntime(1597): Process: com.example.slidpanelayout, PID: 1597
11-13 02:30:47.439: E/AndroidRuntime(1597): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slidpanelayout/com.example.slidpanelayout.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.os.Handler.dispatchMessage(Handler.java:102)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.os.Looper.loop(Looper.java:137)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-13 02:30:47.439: E/AndroidRuntime(1597): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 02:30:47.439: E/AndroidRuntime(1597): at java.lang.reflect.Method.invoke(Method.java:515)
11-13 02:30:47.439: E/AndroidRuntime(1597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-13 02:30:47.439: E/AndroidRuntime(1597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-13 02:30:47.439: E/AndroidRuntime(1597): at dalvik.system.NativeStart.main(Native Method)
11-13 02:30:47.439: E/AndroidRuntime(1597): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-13 02:30:47.439: E/AndroidRuntime(1597): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.Activity.setContentView(Activity.java:1928)
11-13 02:30:47.439: E/AndroidRuntime(1597): at com.example.slidpanelayout.MainActivity.onCreate(MainActivity.java:16)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.Activity.performCreate(Activity.java:5243)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-13 02:30:47.439: E/AndroidRuntime(1597): ... 11 more
11-13 02:30:47.439: E/AndroidRuntime(1597): Caused by: java.lang.NullPointerException: name == null
11-13 02:30:47.439: E/AndroidRuntime(1597): at java.lang.VMClassLoader.findLoadedClass(Native Method)
11-13 02:30:47.439: E/AndroidRuntime(1597): at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:350)
11-13 02:30:47.439: E/AndroidRuntime(1597): at java.lang.ClassLoader.loadClass(ClassLoader.java:487)
11-13 02:30:47.439: E/AndroidRuntime(1597): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:436)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:255)
11-13 02:30:47.439: E/AndroidRuntime(1597): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-13 02:30:47.439: E/AndroidRuntime(1597): ... 21 more