question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NPE on addView at first start of app

See original GitHub issue

AppIntro Version: 4.1.0

Device/Android Version: Reproduced on Nexus 6P API 24, Nexus 5 API 19 Android Studio Emulators

Issue details / Repro steps / Use case background: I’m trying to have AppIntro show up on first start of the app. Crash occurs as soon as the app loads up. I’m using the template that’s provided in this repo to determine if it is the first start of the app.

Your Code: IntroActivity:

//Also occurs with AppIntro2
public class IntroActivity extends AppIntro {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);
        addSlide(new IntroTextFragment());
        //This also does not fix the crash
        //addSlide(AppIntroFragment.newInstance("Welcome to Vipassana Quotes", "This app is recommended " +
        //        "for people who have done Vipassana once already.", R.drawable.ic_notifications_black_24dp, getResources().getColor(R.color.colorPrimary)));
    }
}

activity_intro.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_intro"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.amitavkhandelwal.vipassanaquotes.intro.IntroActivity">

</RelativeLayout>

IntroTextFragment:

public class IntroTextFragment extends Fragment {


    public IntroTextFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_intro_text, container, false);
    }

}

MainActivity:

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();
    @BindView(R.id.all_quotes_recyclerview) RecyclerView allQuotesRecyclerView;
    private RealmAsyncTask transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //  Declare a new thread to do a preference check
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                //  Initialize SharedPreferences
                SharedPreferences getPrefs = PreferenceManager
                        .getDefaultSharedPreferences(getBaseContext());

                //  Create a new boolean and preference and set it to true
                boolean isFirstStart = getPrefs.getBoolean("firstStart", true);

                //  If the activity has never started before...
                if (isFirstStart) {

                    //  Launch app intro
                    Intent i = new Intent(MainActivity.this, IntroActivity.class);
                    startActivity(i);

                    //  Make a new preferences editor
                    SharedPreferences.Editor e = getPrefs.edit();

                    //  Edit preference to make it false because we don't want this to run again
                    e.putBoolean("firstStart", false);

                    //  Apply changes
                    e.apply();
                }
            }
        });

        // Start the thread
        t.start();
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        transaction = RealmUtils.createRealmDbIfNeeded(this);

        QuotesAdapter allQuotesAdapter = new QuotesAdapter(RealmUtils.getAllQuotes());
        allQuotesRecyclerView.setAdapter(allQuotesAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        allQuotesRecyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void onStop () {
        if (transaction != null && !transaction.isCancelled()) {
            transaction.cancel();
        }
        super.onStop();
    }
}

Stack trace / LogCat:

10-04 11:56:36.765 3045-3045/com.amitavkhandelwal.vipassanaquotes E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.amitavkhandelwal.vipassanaquotes, PID: 3045
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amitavkhandelwal.vipassanaquotes/com.amitavkhandelwal.vipassanaquotes.intro.IntroActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.FrameLayout.addView(android.view.View)' on a null object reference
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.FrameLayout.addView(android.view.View)' on a null object reference
                                                                                        at com.github.paolorotolo.appintro.AppIntroBase.initController(AppIntroBase.java:236)
                                                                                        at com.github.paolorotolo.appintro.AppIntroBase.onPostCreate(AppIntroBase.java:158)
                                                                                        at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1199)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Repo that reproduces this issue: https://gitlab.com/amitav13/vipassana-quotes/tree/appintro_crash

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
avluiscommented, Oct 6, 2016

@amitav13 Thank you but I’m good – I’m already aware of that but thanks anyways.

0reactions
carstenhagcommented, Jan 26, 2019

Yep, issue is fixed.

Am Sa., 26. Jan. 2019, 15:46 hat Nicola Corti notifications@github.com geschrieben:

Yeah of course, I read that in this issue. Read some earlier comments here, they talk about possibly adding a linter error.

Is your issue fixed? I can think about writing a lint check about that, but is definitely a low priority task

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/AppIntro/AppIntro/issues/324#issuecomment-457836776, or mute the thread https://github.com/notifications/unsubscribe-auth/ADmh5Xe-Qv8B_d74cce7EV6XXFz0WQogks5vHGpIgaJpZM4KNWHW .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Null pointer exception when calling addView(): Android
I am trying to create a notepad app that ...
Read more >
NPE when start activity - Google Groups
We're using 2.3, and I had a test, example code: HomePageActivity activity = Robolectric.buildActivity(HomePageActivity.class).create().visible().get();.
Read more >
Attempt to invoke virtual method 'void android.widget ... - GitHub
I'm getting the following error when trying to start my app which uses the AppIntro in a simple Activity. E/AndroidRuntime: FATAL EXCEPTION: ...
Read more >
ViewGroup - Android Developers
Align to the start of the view, which is ALIGN_LEFT if the view's resolved layoutDirection is LTR, and ALIGN_RIGHT otherwise. int, TEXT_DIRECTION_ANY_RTL.
Read more >
AppCompat 1.3.x causes NPE crash [196230364]
We had to downgrade appcompat to 1.2.0 because last stable version is giving us a NPE crash. java.lang.NullPointerException: Attempt to invoke virtual ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found