Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

java - app crashes when starting an activity with final intent

I am programming an app that records phone calls and in this moment I am programming a menu and I want that when you click on an object in the menu you go to another activity; I tried to do this on both activities but when I click on a menu item the app crashes. Can you help me solve this crash? I leave you the code and the logcat of the crash

my code:

 //menu
        final Intent go = new Intent(this, records.class);


        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_start:
                        Toast.makeText(MainActivity.this, "Start", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_records:
                        Toast.makeText(MainActivity.this, "Records", Toast.LENGTH_SHORT).show();
                        startRec();
                        break;
                }
                return true;
            }

            private void startRec() {
                startActivity(go);
            }
        });

        //menu code end

crash logcat:

2021-01-12 16:30:09.637 5101-5101/com.example.ngoctri.introsliderexam E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ngoctri.introsliderexam, PID: 5101
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ngoctri.introsliderexam/com.conta.app.spyear.records}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
        at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
        at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
        at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
        at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
        at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
        at com.conta.app.spyear.records.<init>(records.java:13)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)?
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)?
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)?
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)?
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)?
        at android.os.Handler.dispatchMessage(Handler.java:106)?
        at android.os.Looper.loop(Looper.java:223)?
        at android.app.ActivityThread.main(ActivityThread.java:7656)?
        at java.lang.reflect.Method.invoke(Native Method)?
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)?
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.conta.app.spyear.records.<init>(records.java:13)

The problem is in your records activity on row 13. You're calling findViewById() too early at instance init time. Move the findViewById() to onCreate() or later, after setContentView().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...