这是主要的活动java文件。 我得到了错误 java.lang.NullPointerException:尝试调用虚方法' voidandroid.widget.ListView.setOnItemClickListener(android.widget.AdapterView $ OnItemClickListener)'在null对象引用上 在com.example.dell.syncytium.MainActivity.onCreate(MainActivity.java:76)
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.dell.syncytium.Fragments.AboutUsFragment;
import com.example.dell.syncytium.Fragments.Contact;
import com.example.dell.syncytium.Fragments.Events;
import com.example.dell.syncytium.Fragments.HomeFragment;
import com.example.dell.syncytium.Fragments.Register;
import com.example.dell.syncytium.Fragments.Sponsors;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
DrawerLayout mDrawerLayout;
ListView mListView;
ActionBarDrawerToggle mBarToggle;
private CharSequence mtitle;
private CharSequence mDrawerTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerAdapter navDrawerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mtitle=mDrawerTitle=getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
mListView = (ListView)findViewById(R.id.navDrawerView);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mListView.setOnItemClickListener(new SlideMenuClickListener());
navDrawerAdapter = new NavDrawerAdapter(getApplicationContext(), navDrawerItems);
mListView.setAdapter(navDrawerAdapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mBarToggle =
new ActionBarDrawerToggle
(this,mDrawerLayout,R.string.app_name,R.string.app_name)
{
public void onDrawerClosed(View view){
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View view){
getSupportActionBar().setTitle(mtitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mBarToggle);
if(savedInstanceState == null){
displayView(0);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mBarToggle.onOptionsItemSelected(item)){
return true;
}
switch (item.getItemId()){
case R.id.action_settings:
return true;
default:return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mListView);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new AboutUsFragment();
break;
case 2:
fragment = new Events();
break;
case 3:
fragment = new Sponsors();
break;
case 4:
fragment = new Contact();
break;
case 5:
fragment = new Register();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameContainer, fragment).commit();
// update selected item and title, then close the drawer
mListView.setItemChecked(position, true);
mListView.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mListView);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mDrawerTitle = title;
getSupportActionBar().setTitle(mDrawerTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mBarToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mBarToggle.onConfigurationChanged(newConfig);
}
}
这里是主要的activity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id = "@+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="@+id/navDrawerView"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity = "start"
android:choiceMode="singleChoice"
android:divider="@color/background_material_light"
android:dividerHeight="1dp"
android:background="@color/bright_foreground_material_dark">
</ListView>
答案 0 :(得分:0)
我认为你对listview使用了错误的标识符:navDrawerView
答案 1 :(得分:0)
您必须使用setContentView
方法拨打onCreate
。