我不明白如何删除空指针异常

时间:2016-01-09 13:50:06

标签: android nullpointerexception android-context

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="true"
    >

<LinearLayout

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.myapplication.Main2Activity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:id="@+id/AC2toolbar"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:elevation="4dp"

        >
        <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/images"
            android:scaleType="centerCrop"
            android:onClick="ham_click"/>






    </android.support.v7.widget.Toolbar>

</LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main2"
        app:menu="@menu/activity_main2_drawer"
        android:elevation="16dp"
        android:id="@+id/nav"
        />

</android.support.v4.widget.DrawerLayout>

这是活动背后的java代码:

package com.example.android.myapplication;

import android.content.Context;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity
{
   public Context ContextProvider()
    {

        return this.getApplicationContext();
    }
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar AC2toolbar = (Toolbar)findViewById(R.id.AC2toolbar);
        setSupportActionBar(AC2toolbar);
    }

    public void ham_click(View view)
     {
        NavigationView nav = (NavigationView)findViewById(R.id.nav);
        DrawerLayout mDrawer =(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawer.openDrawer(nav);
        mDrawer.setDrawerListener(new DrawerListenerClass());
     }



}

这是用于实现DrawerListener的java类文件:

package com.example.android.myapplication;

import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.Toast;


public class DrawerListenerClass implements DrawerLayout.DrawerListener {

    public void onDrawerOpened(View drawerview)
    {
       Main2Activity obj = new Main2Activity();
        CharSequence text = "Drawer opened";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(obj.ContextProvider(), text, duration);
        toast.show();


    }
    public void onDrawerClosed(View drawerview)
    {
        Main2Activity obj = new Main2Activity();
        CharSequence text = "Drawer closed";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(obj.ContextProvider(), text, duration);
        toast.show();
    }
    public void onDrawerSlide(View drawerView, float slideOffset)
    {

    }
 }

这是错误部分的堆栈跟踪:

01-09 19:18:26.853 24739-24739/com.example.android.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.myapplication, PID: 24739
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.example.android.myapplication.Main2Activity.ContextProvider(Main2Activity.java:17)
at com.example.android.myapplication.DrawerListenerClass.onDrawerOpened(DrawerListenerClass.java:16)
at android.support.v4.widget.DrawerLayout.dispatchOnDrawerOpened(DrawerLayout.java:740)
at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:700)
at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:1833)
at android.support.v4.widget.ViewDragHelper.setDragState(ViewDragHelper.java:874)
at android.support.v4.widget.ViewDragHelper$2.run(ViewDragHelper.java:335)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:1)

你应该以这种方式开始新的活动;

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
CurrentActivity.this.startActivity(myIntent);

通过以下方式检索额外内容:

@Override
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
CurrentActivity.this.startActivity(myIntent);

不要忘记在AndroidManifest.xml中添加新活动:

<activity android:label="@string/app_name" android:name="NextActivity"/>

答案 1 :(得分:0)

问题在于这行代码

Main2Activity obj = new Main2Activity();

你应该尝试在你的类构造函数中传递Context作为参数,但不要实例化它

package com.example.android.myapplication;

import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.Toast;


public class DrawerListenerClass implements DrawerLayout.DrawerListener {

    Context context;

    public DrawerListenerClass(Context context) {
        this.context = context
    }
    public void onDrawerOpened(View drawerview)
    {
        //Main2Activity obj = new Main2Activity();
        CharSequence text = "Drawer opened";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();


    }
    public void onDrawerClosed(View drawerview)
    {
        //Main2Activity obj = new Main2Activity();
        CharSequence text = "Drawer closed";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
    public void onDrawerSlide(View drawerView, float slideOffset)
    {

    }
 }
MainActivty 中的

执行此操作

mDrawer.setDrawerListener(new DrawerListenerClass(getApplicationContext()));
祝你好运