Intent stopping the App in Android Studio

Asked

Viewed 1,100 times

1

I have a problem with Intent, something that was supposed to be simple is causing me problems...

I’m trying to create a second button for call another screen, but as soon as I add the following code the application to:

mAnaliseButton = (Button) findViewById(R.id.custom_view_analiseDetalhada);
        mAnaliseButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Books.this, AnaliseScreen.class);
                startActivity(i);
                finish();
            }
        });

There are (4) screens created in my application which are respectively:

  • Activitysplashscreen
  • Aboutscreen
  • Books
  • Analisescreen

  • The Books.class screen is where the user’s camera opens.

To Activitysplashscreen starts, automatically calling the Aboutscreen. In the Aboutscreen there is a button to call the Books and in the Books once it detects the Imagetarget opens a Bookie with the dice and a button to call the Analisescreen.

But in case I add the code above, as soon as I click the Button on the screen Aboutscreen to start the Books the Application stops working, the interesting thing is that there is already a button inside Books and it works perfectly and does not give this type of error.

Follow the codes below(If necessary, I will send you the ones you ask for): NOTE: I WILL POST THE CODES THROUGH A LINK, AS THEY ARE PASSING THE MAXIMUM LIMIT OF CHARACTERS OF THE FORUM.

05-29 16:23:38.002 21258-21258/com.vuforia.samples.Books E/Androidruntime: FATAL EXCEPTION: main Process: com.vuforia.samples.Books, PID: 21258 java.lang.Runtimeexception: Unable to start Activity Componentinfo{com.vuforia.samples.Books/com.vuforia.samples.Books.app.Books.Books}: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$Onclicklistener)' on a null Object Reference at android.app.Activitythread.performLaunchActivity(Activitythread.java:2584) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2650) at android.app.Activitythread. -wrap11(Activitythread.java) at android.app.Activitythread$H.handleMessage(Activitythread.java:1505) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.Activitythread.main(Activitythread.java:5776) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:888) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:749) Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$Onclicklistener)' on a null Object Reference at com.vuforia.samples.Books.app.Books.Books.startLoadingAnimation(Books.java:523) at com.vuforia.samples.Books.app.Books.Books.onCreate(Books.java:343) at android.app.Activity.performCreate(Activity.java:6270) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2537) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2650)  at android.app.Activitythread. -wrap11(Activitythread.java)  at android.app.Activitythread$H.handleMessage(Activitythread.java:1505)  at android.os.Handler.dispatchMessage(Handler.java:111)  at android.os.Looper.loop(Looper.java:207)  at android.app.Activitythread.main(Activitythread.java:5776)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:888)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:749)

  • 2

    What error does it make? There is Activity Books declared on Androidmanifest.xml?

  • @ramaral , I forgot to post the Androidmanifest. I just posted there on the question, give a look there for kindness.

  • 1

    You can see why this log error is occurring.

  • I have the impression that the Finish() after the startActivity(i) closes Activity mother. Tries to comment.

  • @Reginaldorigo , I put the error in my publication.

  • @Edsonfsantos , I tried to remove Finish(); from the two button and after a button only the same thing happened.

  • And when you click on the first red line of the error. Where it takes you in the sources?

Show 2 more comments

2 answers

1


Your Activity Books is not inflating the layout book_data.xml (which contains custom_view_parseDedited), so when you try to initialize the mAnaliseButton variable with findViewById(), it returns null for this variable (because it did not find the id passed as parameter) and with it gives the error you posted, of trying to associate the Listener (setOnClickListener) in a null object.

The other button works (I’m imagining it to be mBackButton) because you inflate the appropriate layout before trying to set the System to the same.

  • got it.. How do I find out what layout . java right ? Because what I’m using is a vuforia sdk and I didn’t create some things like the layout.

  • The layout is book_data.xml that I posted in my reply, at least I think it is, because it’s what I saw that contains the id you want to call in your code. Tries to inflate the layout the same way it was made for mBackButton in the startLoadingAnimation method().

1

Friend , try this way , I removed the Finish ()

        mAnaliseButton = (Button) 
        findViewById(R.id.custom_view_analiseDetalhada);
        mAnaliseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Books.this, AnaliseScreen.class);
            startActivity(i);

        }
    });
  • i already tried to insert without Finish. But still the error happens.

  • I recommend that you take a look at this link

  • http://www.androidpro.com.br/intents/

  • See if it goes now , I put in setOnclick the New View.Onclicklistener

  • Thiago worked out?

  • I used the knowledge of Márcio Oliveira. But thank you.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.