0

I'm trying to change from menu activity to any other activity and the whole app stops responding, no idea why.

This is the code I use(I call it from the SurfaceView class instance of the activity):

Intent intent = new Intent(menuActivity, ExitActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("EXIT", true);
menuActivity.startActivity(intent);

Log:

  10-28 13:03:38.278: E/AndroidRuntime(6185): FATAL EXCEPTION: main
  10-28 13:03:38.278: E/AndroidRuntime(6185): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.app.LoadedApk.makeApplication(LoadedApk.java:501)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4224)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.app.ActivityThread.access$1400(ActivityThread.java:140)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1297)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.os.Handler.dispatchMessage(Handler.java:99)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.os.Looper.loop(Looper.java:137)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.app.ActivityThread.main(ActivityThread.java:4921)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at java.lang.reflect.Method.invokeNative(Native Method)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at java.lang.reflect.Method.invoke(Method.java:511)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at dalvik.system.NativeStart.main(Native Method)
  10-28 13:03:38.278: E/AndroidRuntime(6185): Caused by: java.lang.NullPointerException
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:377)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.app.LoadedApk.getClassLoader(LoadedApk.java:320)
  10-28 13:03:38.278: E/AndroidRuntime(6185):   at android.app.LoadedApk.makeApplication(LoadedApk.java:493)

EDIT:

More code - constructor of the SurfaceView method:

private MenuActivity menuActivity;
public MenuPanel(Context context,MenuActivity activity) {
    super(context);
    menuActivity = activity;;
}

The whole activity code:

public class MenuActivity extends Activity {

private MenuPanel panel;
@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    panel = new MenuPanel(this,this);
    panel.getHolder().setFormat(PixelFormat.RGBA_8888);
    setContentView(panel);
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    panel.onBackPressed(this);
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

@Override
protected void onPause() {
    panel.pauseGame();
    super.onPause();
}

@Override
protected void onStop() {
    //panel.recycleBitmaps();
    // TODO Auto-generated method stub
    super.onStop();
}

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
}

@Override
protected void onResume() {
    if (panel.getbPaused()) panel.resumeGame();
    super.onResume();
}
}

EDIT2:

Also tried adding a method to the activity class and calling it from the surfaceview class but the result is the same.

EDIT3:

public class ExitActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent().getBooleanExtra("EXIT", false)) {
            finish();
        }else {
            Intent myIntent = new Intent(ExitActivity.this, SplashActivity.class);
            ExitActivity.this.startActivity(myIntent);
        }
    }
}
MysticMagicϡ
  • 27,509
  • 15
  • 68
  • 114
Greyshack
  • 1,643
  • 4
  • 23
  • 43

3 Answers3

1

Not sure if this helps, but try to use one of these:

  1. I would rather pass only Context to the view, not the whole activity

  2. Or create a listener and fire it up in the view. Catch it in the activity and start the other activity from there

EDIT: Based on the edited question, try not to use MenuActivity in the MenuPanel class. just store the Context and use it instead of the menuActivity.

private Context context = null;
public MenuPanel(Context context) {
    super(context);
    this.context = context;
}

And then:

Intent intent = new Intent(context, ExitActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("EXIT", true);
context.startActivity(intent);

But I would still prefer creating a listener (2.), it seems to me cleaner.

Hrusilov
  • 586
  • 4
  • 8
  • double click on the lift line of the code editor window (in eclipse) – Hrusilov Oct 28 '14 at 12:47
  • I think I found what is going on here. Instead of launching into a new activity, I just tried to stop my thread and it crashed right there. So now we move on to why it crashes Also adding this debug point helps me how? Because it doesnt seem to do anything – Greyshack Oct 28 '14 at 12:50
  • If you launch the application in debug mode (small bug button) it will pause the code execution when it comes to that point. Then you can check all variables is they are null or what value they hold, also try to execute any function etc. – Hrusilov Oct 28 '14 at 12:54
  • Well, the app crashes when I want to launch debug mode. – Greyshack Oct 28 '14 at 12:56
0

EDIT: Based on your edited code, in your MenuPanel:

Intent intent = new Intent(getContext(), ExitActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 intent.putExtra("EXIT", true);
 getContext().startActivity(intent);

Also remove the custom constructor and your "recursive" call in ExitActivity's onCreate.

Guillermo Merino
  • 3,111
  • 2
  • 15
  • 33
  • Are you sure that your object activity is not null at that point? Please remember that if it is a fragment you can do getContext() – Guillermo Merino Oct 28 '14 at 12:27
  • Why would it be a null at any point? Anyways, I tried not using the object and making a method inside the activity class to change activities, and calling it from the surfaceview but the result is the same – Greyshack Oct 28 '14 at 12:31
  • Your MenuPanel extends SurfaceView which is already a View, why you don't use directly getContext() instead of creating a custom constructor and storing your activity reference? (it is already stored in the view). Check this answer about custom view constructors: http://stackoverflow.com/a/2888594/1668144 – Guillermo Merino Oct 28 '14 at 12:44
  • Intent intent = new Intent(getContext(), ExitActivity.class); getContext().startActivity(intent); This doesn't work as well – Greyshack Oct 28 '14 at 12:48
  • It just stops responding and that shows after a while. But as I said in another comment I deleted the code for activity change and instead put tried to stop my thread and it stops responding as well. I have no idea why, it worked before. – Greyshack Oct 28 '14 at 12:55
0

Firstly, use activity in intent. And also add flag FLAG_ACTIVITY_NEW_TASK to Intent, as you start a new activity from another class.

Intent intent = new Intent(menuActivity , ExitActivity.class);  //use menuactivity as that's your global variable in SurfaceView
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("EXIT", true);
menuActivity.startActivity(intent);

Hope this helps.

MysticMagicϡ
  • 27,509
  • 15
  • 68
  • 114
  • it just stops responding, black screen and it doesnt crash – Greyshack Oct 28 '14 at 12:25
  • ExitActivity works just fine because at first the app in launched into it which then loads next activity. But sure, I'll post it. Actually it is supposed to be GameActivity, I just changed to ExitActivity to see if it is GameActivity's problem – Greyshack Oct 28 '14 at 12:28
  • Put a debug point at `if (getIntent().getBooleanExtra("EXIT", false)) { finish(); }` – MysticMagicϡ Oct 28 '14 at 12:31
  • Don't focus on ExitActivity because it isn't even started... Also I told you it happens with any activity I want to start from this one – Greyshack Oct 28 '14 at 12:34
  • ok @Greyshack try edited answer. and put a debug point at `Intent intent = new Intent` and see where does that stop working. – MysticMagicϡ Oct 28 '14 at 12:37