0

I'm trying to use a method from activity1 to activity2. But, when I try to open this activity app crashes.

I'm learning android, it's a exercise. Instead of use onClick on XML I should deploy onClickListener on Java code. But as the same button will exist 3x on diferents activity doing Intents each other, I tryied just to call a method from another activity instead of write the same again 3x.

I would like to understand better how could I fix this issue. Thanks for any help

I also have searched here, but have found nothing. Maybe I can't understand the logic from anothers codes completely.

Activity 1 (snippet)

//Do intent on Button shopButton
public void buttonIntent(){
    Button buttonShop = findViewById(R.id.shopButton);
    if (buttonShop != null){
        buttonShop.setOnClickListener(new OnClickListener( ) {
            @Override
            public void onClick( View v ) {
                Intent i = new Intent(MainActivity.this,ShopActivity.class);
                startActivity(i);
            }
        });
    }
}

Activity 2

package com.example.android.musique;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class ListActivity extends AppCompatActivity {

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        //Create an MainActivity Object
        MainActivity mainAct = new MainActivity();

        //Call methods what catches the button and do intent from MainActivity
        mainAct.buttonIntent();
    }

}

LOGCAT

01-10 22:24:16.514 8887-8956/com.example.android.musique D/OpenGLRenderer: ProgramCache.generateProgram: 34359738371
01-10 22:24:16.516 8887-8956/com.example.android.musique D/OpenGLRenderer: ProgramCache.generateProgram: 240518168576
01-10 22:24:16.518 8887-8956/com.example.android.musique D/OpenGLRenderer: ProgramCache.generateProgram: 68724719680
01-10 22:24:16.529 8887-8956/com.example.android.musique D/GraphicBuffer: register, handle(0x7a2b9b5780) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
01-10 22:24:16.539 8887-8887/com.example.android.musique V/InputMethodManager: onWindowFocus: null softInputMode=272 first=true flags=#81810100
01-10 22:24:16.564 8887-8956/com.example.android.musique D/GraphicBuffer: register, handle(0x7a2b9b6200) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
01-10 22:24:17.401 8887-8894/? I/art: Ignoring second debugger -- accepting and dropping
01-10 22:24:17.829 8887-8956/? D/OpenGLRenderer: ProgramCache.generateProgram: 34359738369
01-10 22:24:18.383 8887-8956/? D/OpenGLRenderer: CacheTexture 7 upload: x, y, width height = 72, 18, 28, 103
01-10 22:24:18.392 8887-8956/? D/OpenGLRenderer: CacheTexture 7 upload: x, y, width height = 84, 120, 17, 90
01-10 22:24:18.408 8887-8956/? D/OpenGLRenderer: CacheTexture 7 upload: x, y, width height = 84, 209, 15, 24
01-10 22:24:18.507 8887-8956/? D/OpenGLRenderer: ProgramCache.generateProgram: 103079215104
01-10 22:24:19.352 8887-8956/? D/OpenGLRenderer: ProgramCache.generateProgram: 103084458052
01-10 22:24:24.085 8887-8887/? D/AndroidRuntime: Shutting down VM
01-10 22:24:24.095 8887-8887/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.android.musique, PID: 8887
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.musique/com.example.android.musique.ListActivity}: 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:2745)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2810)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529)
                                                     at android.os.Handler.dispatchMessage(Handler.java:110)
                                                     at android.os.Looper.loop(Looper.java:203)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6275)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
                                                  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.example.android.musique.MainActivity.buttonIntent(MainActivity.java:45)
                                                     at com.example.android.musique.ListActivity.onCreate(ListActivity.java:19)
                                                     at android.app.Activity.performCreate(Activity.java:6717)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2698)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2810) 
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                     at android.os.Looper.loop(Looper.java:203) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6275) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
Nakamoto
  • 1,293
  • 14
  • 17
  • 1
    You are trying to launch an intent/activity from a listener and the object being referenced is null, thus the error. Please see [this](https://stackoverflow.com/questions/6103974/how-do-i-start-an-intent-from-an-onclicklistener) SO post which is probably what you are looking for. – ADimaano Jan 11 '18 at 00:44
  • MainActivity mainAct = new MainActivity(), this is not a good practice, you should implement an interface if you wan to run MainActivity method from another activity. Instead to start ShopActivity from MainActivity, why don't you do it at ListActivity? – YinKiet Jan 11 '18 at 01:38

1 Answers1

5
MainActivity mainAct = new MainActivity();

Never create an instance of an activity yourself.

If res/layout/activity_list.xml has a <Button> named shopButton, then the simplest thing to do (at least in the short term) is to copy or move buttonIntent() into ListActivity, so you can use it directly there.

If res/layout/activity_list.xml does not have a <Button> named shopButton, then the code in buttonIntent() is not relevant to your ListActivity.

BTW, the Android SDK already has a class named ListActivity, which may cause you some confusion.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your help. it's a exercise from a course. Instead use onClick on XML I should deploy onClickListener on Java code. But as the same button will exist 3x on diferents activity doing Intents each other, I tryied just to call a method from another activity instead of write the same again 3x. Did you understand? I would like to know how to perform this... – Nakamoto Jan 11 '18 at 00:58
  • @Nakamoto: You could make `buttonIntent()` into a `static` method, passing in your `Activity` to use for the `findViewById()` call and the `Intent` constructor. Or, you could create a base class (e.g., `ShopButtonActivity`) that has `buttonIntent()`, and have `MainActivity` and `ListActivity` extend `ShopButtonActivity`. – CommonsWare Jan 11 '18 at 01:02
  • could you give a simple code example? it will really help me to understand better. – Nakamoto Jan 11 '18 at 01:04
  • Could you please give me a simple code example? it will really help me to understand better. – Nakamoto Jan 11 '18 at 01:15