我成功设置了NavigationView
抽屉,并且非常喜欢实施的简便性。但是,我需要我的标题图片,它是LinearLayout
的一部分,我将其作为app:headerLayout
传递给点击时动画,我无法实现此目的。我需要你的帮助。
这是我的activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- This DrawerLayout has two children at the root -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_view"
app:itemTextAppearance="@style/TextAppearance.AppCompat.Subhead"
/>
</android.support.v4.widget.DrawerLayout>
这是我的MainActivity:
public class MainActivity extends AppCompatActivity implements AnimationFragment.OnFragmentInteractionListener, GameFragment.OnFragmentInteractionListener, TextSendFragment.OnFragmentInteractionListener, FriendFragment.OnFragmentInteractionListener{
//private DrawerLayout mDrawer;
private Toolbar toolbar;
private DrawerLayout dlDrawer;
private ActionBarDrawerToggle drawerToggle;
private NavigationView nvDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Find our drawer view
dlDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
// Tie DrawerLayout events to the ActionBarToggle
dlDrawer.setDrawerListener(drawerToggle);
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
View headerView = getLayoutInflater().inflate(R.layout.nav_header, nvDrawer, false);
nvDrawer.addHeaderView(headerView);
/* TODO get the IMAGE and make it clickable */
headerImage = (ImageView) findViewById(R.id.nav_header_image);
headerImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Logout",
Toast.LENGTH_LONG).show();
}});
/*
headerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Logout",
Toast.LENGTH_LONG).show();
}
});
*/
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
/* Logout is a special case which launches a new activity so we separate it. */
if(menuItem.getItemId() == R.id.nav_fifth_fragment){
Toast.makeText(getApplicationContext(), "Logout",
Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this, SplashActivity.class);
startActivity(i);
}
/* Else we use our Fragment switching mechanism */
else {
// Create a new fragment
Fragment fragment = null;
Class fragmentClass;
switch (menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = AnimationFragment.class;
break;
case R.id.nav_second_fragment:
fragmentClass = GameFragment.class;
break;
case R.id.nav_third_fragment:
fragmentClass = TextSendFragment.class;
break;
case R.id.nav_fourth_fragment:
fragmentClass = FriendFragment.class;
break;
default:
fragmentClass = AnimationFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
}
// Highlight the selected item, update the title, and close the drawer
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
dlDrawer.closeDrawers();
}
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, dlDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Make sure this is the method with just `Bundle` as the signature
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
public void onFragmentInteraction(String id){
//you can leave it empty
}
}
这是我的nav_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_header"
android:layout_width="match_parent"
android:layout_height="192dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom"
android:background="@drawable/nav_drawer_pixel_background3"
android:clickable="true">
<ImageView
android:id="@+id/nav_header_image"
android:layout_width="wrap_content"
android:layout_height="112dp"
android:src="@drawable/nav_drawer_cooler"
android:clickable="true"/>
<TextView
android:id="@+id/nav_header_username"
android:layout_below="@id/nav_header_image"
android:layout_marginLeft="16dp"
android:layout_marginTop="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/user_name"
android:fontFamily="sans-serif-medium"
android:textColor="@color/dark_main_text"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/>
<TextView
android:layout_below="@id/nav_header_username"
android:layout_marginLeft="16dp"
android:layout_marginBottom="12dp"
android:layout_width="match_parent"
android:layout_height="18dp"
android:text="@string/user_mail"
android:fontFamily="sans-serif"
android:textColor="@color/dark_main_text"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
崩溃报告:
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: FATAL EXCEPTION: main
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pymdev.pym/com.example.pymdev.pym.MainActivity}: java.lang.NullPointerException
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:143)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4960)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at com.example.pymdev.pym.MainActivity.onCreate(MainActivity.java:56)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5203)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:143)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4960)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-05 11:06:33.101 357-357/com.example.pymdev.pym E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
我发现了导致NullPointerException的错误。
headerImage = (ImageView) findViewById(R.id.nav_header_image);
必须更改为
headerImage = (ImageView) headerView.findViewById(R.id.nav_header_image);
希望它有所帮助。我将它与onClick处理程序结合使用,以便在单击时更改NavigationView
标题中的图像。