我正在尝试在Android Studio中创建导航菜单。但是我总是在listview声明中得到NullPointerException,或者类似于this。
这是我的代码。
主要活动
public class Java_Main扩展了ActionBarActivity {
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayoutCekmece;
ListView liste;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarMain);
toolbar.setBackgroundColor(Color.GREEN);
setSupportActionBar(toolbar);
drawerLayoutCekmece = (DrawerLayout) findViewById(R.id.drawer12);
liste = (ListView) drawerLayoutCekmece.findViewById(R.id.listeDrawer);
liste.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.gunler)));
liste.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
drawerLayoutCekmece.closeDrawer(liste);
}
});
toggle = new ActionBarDrawerToggle(this,drawerLayoutCekmece , toolbar , R.string.string_pazartesi , R.string.string_sali)
{
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
syncState();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
syncState();
}
};
drawerLayoutCekmece.setDrawerListener(toggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);}
activity_main.xml中
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/my_toolbar"
android:id="@+id/toolbarMain"
/>
<include
layout="@layout/layout_drawer"
android:id="@+id/drawer12"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewProgram"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinnerGunler"
android:layout_marginBottom="20dp"
android:layout_marginTop="15dp"
android:lineSpacingExtra="2dp"
android:textSize="20dp" />
<Spinner
android:id="@+id/spinnerGunler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/gunler"
android:layout_alignParentLeft="true" />
</RelativeLayout>
和layout_drawer.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400px" android:id="@+id/drawer" android:layout_height="800px">
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:entries="@array/gunler"
android:id="@+id/listeDrawer"/>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
感谢您的帮助。 抱歉英语不好。