0
I am very new on Android. So if you can explain the logic behind the problem I would appreciate it. It 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 runs when a certain button is clicked. The same button is on the other Activity, so instead of creating the same method twice and saying all that it has to do, I would just like to call the method that is within activity1. This from the onCreate();
Any help is valid!
Activity 1 (excerpt)
//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)
MainActivity asd = new MainActivity();
asd.buttonIntent();
buttonIntent (Activity) in Mainactivity cannot be Applied. I get stuck in this result– Nakamoto
You’ll use it like this:
MainActivity.buttonIntent(this);
wherethis
is the Activity reference where the method was called. You do not need to instantiate a new object.– Christian Beregula