AppIntro: NPE on addView at first start of app
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
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 22 (8 by maintainers)
@amitav13 Thank you but I’m good – I’m already aware of that but thanks anyways.