Android - How to call a method from another class from onCreate ()?

0

I'm pretty new to Android. So if you can explain the logic behind the problem I would appreciate it. This would help me understand the problem and see how I could solve it if it happens again.

In activity one I have a method that is executed when a certain button is clicked. The same button is in the other activity, so instead of creating the same method twice and saying all that it has to do, I'd just like to call the method that's inside activity1. This from onCreate ();

Any help is valid!

Activity 1

//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)
    
asked by anonymous 11.01.2018 / 11:53

1 answer

1

Unless this method is static or you create a class from which your activity derives, you need to implement this method in both.

Your log error is derived from the fact that you tried to instantiate activity manually with MainActivity mainAct = new MainActivity();

Android will not simply create an activity object, it will try to "put it to run" it.

Changing your method to static should solve your problem.

public static void buttonIntent(final Activity activity){
    Button buttonShop = activity.findViewById(R.id.shopButton);
    if (buttonShop != null){
        buttonShop.setOnClickListener(new View.OnClickListener( ) {
            @Override
            public void onClick( View v ) {
                Intent i = new Intent(activity, ShopActivity.class);
                activity.startActivity(i);
            }
        });
    }
}
    
11.01.2018 / 12:34