我是Android新手并使用NavigationView
创建导航抽屉并获取java.lang.NullPointerException
。首先是一个启动画面,几秒钟后会有一个Home Activity
,其中会有Navigation Drawer
。
SplashScreenActivity.java
package com.zenica.app.zenica;
import android.annotation.TargetApi;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.transition.Explode;
import android.transition.Transition;
import android.view.Window;
import android.os.Handler;
@TargetApi(21)
public class SplashScreenActivity extends AppCompatActivity {
private static final int SPLASH_TIME_OUT = 3000; // time out for the splash in ms
private Handler mHandlerSplash;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
int currentapiVersion= Build.VERSION.SDK_INT;
if(currentapiVersion== Build.VERSION_CODES.LOLLIPOP)
{
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
Transition ts = new Explode();
ts.setStartDelay(2000);
//set the duration
ts.setDuration(5000);
getWindow().setEnterTransition(ts);
//set an exit transition so it is activated when the current activity exits
getWindow().setExitTransition(ts);
setContentView(R.layout.splash_main_activity);
launchSecondActivity();
}
else
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_main_activity);
mContext = this;
mHandlerSplash = new Handler();
mHandlerSplash.postDelayed(new Runnable() {
@Override
public void run() {
Intent mIntent = new Intent(mContext, HomeActivity.class);
startActivity(mIntent);
finish();
}},SPLASH_TIME_OUT);
}
}
public void launchSecondActivity() {
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
}
}
HomeActivity.java
package com.zenica.app.zenica;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.transition.Slide;
import android.transition.Transition;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
/**
* Created by Neha on 03-02-2016.
*/
public class HomeActivity extends AppCompatActivity {
private Toolbar mtoolar;
DrawerLayout dLayout;
private NavigationView navView;
@TargetApi(21)
@Override
public void onCreate(Bundle savedInstanceState) {
int currentapiVersion= Build.VERSION.SDK_INT;
if(currentapiVersion== Build.VERSION_CODES.LOLLIPOP) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
Transition ts = new Slide();
ts.setDuration(3000);
getWindow().setEnterTransition(ts);
getWindow().setExitTransition(ts);
// setContentView(R.layout.navigation_drawer);
}
else
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.navigation_drawer);
}
setToolbar();
setNavigationDrawer();
/* result = new DrawerBuilder()
.withActivity(this)
.withToolbar(mtoolar)
.addDrawerItems(
new PrimaryDrawerItem().withName("Home".toUpperCase()).withIdentifier(1).withCheckable(true),
new PrimaryDrawerItem().withName("New Cars".toUpperCase()).withIdentifier(2).withCheckable(true),
new PrimaryDrawerItem().withName("Book A Test Drive".toUpperCase()).withIdentifier(3).withCheckable(true),
new PrimaryDrawerItem().withName("Our Locations".toUpperCase()).withIdentifier(4).withCheckable(true),
new PrimaryDrawerItem().withName("Sell A Car".toUpperCase()).withIdentifier(5).withCheckable(true),
new PrimaryDrawerItem().withName("Pay Bills".toUpperCase()).withIdentifier(6).withCheckable(true),
new PrimaryDrawerItem().withName("About Zenica".toUpperCase()).withIdentifier(7).withCheckable(true),
new PrimaryDrawerItem().withName("Connect".toUpperCase()).withIdentifier(8).withCheckable(true),
new PrimaryDrawerItem().withName("About App".toUpperCase()).withIdentifier(9).withCheckable(true) )
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
if (drawerItem != null) {
if (drawerItem.getIdentifier() == 1) {
//switchFragment(new HomeFragment(), false);
} else if (drawerItem.getIdentifier() == 2) {
//switchFragment(new AboutFragment(), true);
} else if (drawerItem.getIdentifier() == 3) {
//switchFragment(new TemplesFragment(), true);
} else if (drawerItem.getIdentifier() == 4) {
//switchFragment(new PagerAdapter(), true);
} else if (drawerItem.getIdentifier() == 5) {
//switchFragment(new MemoriesFragment(), true);
} else if (drawerItem.getIdentifier() == 6) {
//switchFragment(new EventsFragment(), true);
} else if (drawerItem.getIdentifier() == 7) {
//switchFragment(new VideosFragment(), true);
} else if (drawerItem.getIdentifier() == 8) {
//switchFragment(new PhotosFragment(), true);
} else if (drawerItem.getIdentifier() == 9) {
//switchFragment(new FeedbackFragment(), true);
}
result.setSelectionByIdentifier(drawerItem.getIdentifier(), false);
}
return false;
}
})
.withSavedInstance(savedInstanceState)
.build();*/
}
private void setToolbar()
{
setContentView(R.layout.navigation_drawer);
mtoolar=(Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mtoolar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(false);
}
private void setNavigationDrawer() {
dLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
navView=(NavigationView) findViewById(R.id.navigation);
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Fragment frag = null;
int itemId = menuItem.getItemId();
if (itemId == R.id.home_item) {
// frag = new Fragment1();
}
else if (itemId == R.id.about_app) {
// frag = new Fragment2();
}
/* if (frag != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag);
transaction.commit();
dLayout.closeDrawers();
return true;
}*/
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
String btnName = null;
switch(id) {
case R.id.action_settings:
btnName = "Settings";
break;
case R.id.action_help:
btnName = "Help";
break;
case R.id.about_zenica:
btnName="About Zenica";
}
System.out.print(btnName);
return super.onOptionsItemSelected(item);
}
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme" >
</android.support.v7.widget.Toolbar>
navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeActivity" >
<include layout="@layout/toolbar" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
app:itemTextColor="#333"
app:itemIconTint="#333"
app:menu="@menu/navigation_items" />
</android.support.v4.widget.DrawerLayout>
navigation_items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/home_item"
android:title="Home"
/>
<item android:id="@+id/cars"
android:title="New Cars"
/>
<item android:id="@+id/test_drive"
android:title="Book A Test Drive"
/>
<item android:id="@+id/locations"
android:title="Our Locations"
/>
<item android:id="@+id/sell"
android:title="Sell A Car"
/>
<item android:id="@+id/pay_bill"
android:title="Pay Bills"
/>
<item android:id="@+id/about_zenica"
android:title="About Zenica"
/>
<item android:id="@+id/connect"
android:title="Connect"
/>
<item android:id="@+id/about_app"
android:title="About App"
/>
</group>
</menu>