Android:调用新意图时出现空指针异常

时间:2015-06-25 19:41:44

标签: java android android-intent

尝试通过意图调用新活动时收到错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference

我的活动有一个按钮,点击后会转到这个方法:

public void onClickChangeActivity() {

        Intent intent = new Intent(context , Details.class);
        startActivity(intent);

    }

公共静态上下文上下文在类的开头创建,然后在我的onCreate中实例化为context = getApplicationContext();

我也尝试过context = getBaseContext()和context = RecyclerViewActivity.this(RecyclerViewActivity是我当前班级的名字)

为什么我收到这个NullP的任何帮助?我已经尝试了所有与StackOverFlow相关的答案。另外,我的清单包括我的活动。

onClickChangeActivity是从我的RecyclerViewerAdapter类调用的:

View.OnClickListener getPersonClickListener(final int position) {
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                RecyclerViewActivity recyclerViewActivity = new RecyclerViewActivity();
                recyclerViewActivity.onClickChangeActivity();
            }
        };

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.akash.android_test" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="18" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- <activity -->
        <!-- android:name=".DataFetcher" -->
        <!-- android:label="@string/title_activity_data_fetcher" > -->
        <!-- </activity> -->
        <activity
            android:name=".Crime"
            android:label="@string/title_activity_crime"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".RecyclerViewActivity"
            android:label="@string/title_activity_recycler_view"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Details"
            android:label="@string/title_activity_crime_details"
            android:parentActivityName=".RecyclerViewActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="RecyclerViewActivity" />
        </activity>
    </application>

</manifest>

logcat的:

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
            at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
            at android.content.ComponentName.<init>(ComponentName.java:77)
            at android.content.Intent.<init>(Intent.java:4160)
            at com.bah.baltimoreopendata.android_baltimoreopendata.RecyclerViewActivity.onClickChangeActivity(RecyclerViewActivity.java:265)
            at com.bah.baltimoreopendata.android_baltimoreopendata.RecycleViewAdapter$1.onClick(RecycleViewAdapter.java:121)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

4 个答案:

答案 0 :(得分:8)

问题是您尝试从适配器调用活动方法:

RecyclerViewActivity recyclerViewActivity = new RecyclerViewActivity();
recyclerViewActivity.onClickChangeActivity();

您应该在适配器类中替换此代码。在适配器构造函数中添加以下行:

public RecycleViewAdapter(......, Context context){
    ...your code.
    this.mContext=context;
}

getPersonClickListener 适配器方法中:

View.OnClickListener getPersonClickListener(final int position) {
    return new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mContext instanceof RecyclerViewActivity){
                ((RecyclerViewActivity)mContext).onClickChangeActivity();
            }
        }
    };

当您在RecyclerViewActivity上调用 onClickChangeActivity 方法时,请使用:

public void onClickChangeActivity() {
    Intent intent = new Intent(RecyclerViewActivity.this, Details.class);
    startActivity(intent);
}

确保 Details.class 扩展了Activity。

答案 1 :(得分:3)

试试这个:

Intent intent = new Intent(RecyclerViewActivity.this , Details.class);
startActivity(intent);

使用RecyclerViewActivity.this代替context

答案 2 :(得分:2)

解决方案:

在适配器类中创建private static Context context;

在inCreateViewHolder()中,实例化viewGroup.getContext()的上下文:

@Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        context = viewGroup.getContext();
        return pvh;
    }

这就是我得到空指针异常的原因。出于某种原因,我创建了一个带有上下文的构造函数,上下文仍为null。在上面的类中实例化上下文就可以了!感谢所有回答的人。

答案 3 :(得分:1)

您正在尝试启动Details.class -

但我在你的清单中找不到任何对此活动的引用。

请添加

<activity android:name=".Details"</activity>

到您的清单并确保详细信息扩展活动。