我是Android新手,我一直在尝试使用jfeinstein滑动菜单和可扩展列表。 我的问题是每次将findViewById用于expandablelist
时我都会变为null这是我的代码
主要活动
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.app = (App) getApplication();
app.menu = new SlidingMenu(app);
app.menu.setMode(SlidingMenu.LEFT);
app.menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
app.menu.setFadeDegree(0.50f);
app.menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
app.menu.setShadowDrawable(R.drawable.shadow);
app.menu.setShadowWidthRes(R.dimen.shadow_width);
app.menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
app.menu.setEnabled(true);
app.adapter = new SlideMenuList(this, app, parent, child);
app.listView = (ExpandableListView) app.menu.findViewById(R.id.slide_list);
app.listView.setAdapter(app.adapter);
app.listView.setOnChildClickListener(app.adapter);
app.listView.setOnGroupExpandListener(app.adapter);
android.support.v7.app.ActionBar ac = getSupportActionBar();
ac.setHomeButtonEnabled(true);
init();
}catch(Exception e){
e.printStackTrace();
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/splash_screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jaggz.gurudawaraapp.MainActivity" >
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</FrameLayout>
</RelativeLayout>
expandable_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/slide_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ExpandableListView>
错误
11-23 15:24:28.187: W/System.err(13349): java.lang.NullPointerException
11-23 15:24:28.187: W/System.err(13349): at com.jaggz.gurudwaraapp.MainActivity.onCreate(MainActivity.java:64)
11-23 15:24:28.187: W/System.err(13349): at android.app.Activity.performCreate(Activity.java:5312)
11-23 15:24:28.187: W/System.err(13349): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
11-23 15:24:28.187: W/System.err(13349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
11-23 15:24:28.187: W/System.err(13349): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
11-23 15:24:28.187: W/System.err(13349): at android.app.ActivityThread.access$800(ActivityThread.java:156)
11-23 15:24:28.187: W/System.err(13349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
11-23 15:24:28.187: W/System.err(13349): at android.os.Handler.dispatchMessage(Handler.java:102)
11-23 15:24:28.187: W/System.err(13349): at android.os.Looper.loop(Looper.java:157)
11-23 15:24:28.187: W/System.err(13349): at android.app.ActivityThread.main(ActivityThread.java:5872)
11-23 15:24:28.187: W/System.err(13349): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 15:24:28.187: W/System.err(13349): at java.lang.reflect.Method.invoke(Method.java:515)
11-23 15:24:28.187: W/System.err(13349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-23 15:24:28.187: W/System.err(13349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
11-23 15:24:28.187: W/System.err(13349): at dalvik.system.NativeStart.main(Native Method)
请帮忙
答案 0 :(得分:0)
尝试将 expandable_list.xml 的代码移至 activity_main.xml
答案 1 :(得分:0)
试试这个:
app.menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
app.menu.setEnabled(true);
app.adapter = new SlideMenuList(this, app, parent, child);
**app.listView = (ExpandableListView) app.menu.findViewById(R.id.slidingmenu_view);**
app.listView.setAdapter(app.adapter);
app.listView.setOnChildClickListener(app.adapter);
app.listView.setOnGroupExpandListener(app.adapter);