7

I am receiving an error when trying to call new activity through an intent:

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

My Activity has a button, and when clicked, it goes to this method:

public void onClickChangeActivity() {

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

    }

public static Context context is created in the beginning of the class and then is instantiated in my onCreate as context = getApplicationContext();

I've also tried context = getBaseContext() and context = RecyclerViewActivity.this (RecyclerViewActivity is the name of my current class)

Any help as to why I'm receiving this NullP? I've tried everything related StackOverFlow answers have offered. Also, my Manifest includes my activity.

onClickChangeActivity is being called from my RecyclerViewerAdapter class:

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


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

Manifest File:

<?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)
10
  • have you tried onClickChangeActivity(context)? Commented Jun 25, 2015 at 19:48
  • Use Intent intent = new Intent(RecyclerViewActivity.this, Details.class); if works the variable context need to be a final variable and use like onClickChangeActivity(context) Commented Jun 25, 2015 at 19:48
  • 1
    Try with context = this; Commented Jun 25, 2015 at 19:50
  • @Santiago Just tried that, same error though. Commented Jun 25, 2015 at 19:52
  • @JonathanGarcía Just tried that as well, same error. Commented Jun 25, 2015 at 19:52

4 Answers 4

8

Problem is that you are trying to call an activity method from the adapter:

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

You should replace this code in adapter class. In the adapter constructor add the following lines:

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

And in In the getPersonClickListener adapter method:

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

And when you invoke onClickChangeActivity method on RecyclerViewActivity use:

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

And be sure that Details.class extends Activity.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried that. I get this error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
There is something that is not initialized correctly. Put your logcat
In order to do this, I would need to make onClickChangeActivty static?
No, check last edit answer above. I hope that this could solve your problem.
Are you passing Context from Activity to Adapter and after that using this Context to invoke method on Adapter to call method on the Activity? Please, could you post your code updated?
|
3

try this:

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

use RecyclerViewActivity.this instead of context

4 Comments

This should work indeed. You don't need the ApplicationContext since each Activity is a context itself. Therefore RecyclerViewActivity.this should work.
I tried that. I get this error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
can you put your logcat?
you not defined Details Activity!!
2

Solution:

In the adapter class create a private static Context context;

In your inCreateViewHolder(), instantiate context to 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;
    }

This is the reason I was getting a Null Pointer Exception. For some reason, what I create a constructor with a context, context was still null. Instantiating context in the above class did the trick! Thanks to all who answered.

Comments

1

You are trying to start Details.class -

But I can not find any references to this activity in your manifest.

Please add

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

To your manifest and make sure that Details extends Activity.

1 Comment

My class name used to be CrimeDetails, but then I changed it to Details without changing the manifest. But just changed it to .Details and it still didn't work. Still getting same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.