无法改变android的活动

时间:2014-10-28 12:09:46

标签: java android android-activity crash surfaceview

我正在尝试从菜单活动更改为任何其他活动,整个应用停止响应,不明白为什么。

这是我使用的代码(我从活动的SurfaceView类实例中调用它):

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);

日志:

  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)

编辑:

更多代码 - SurfaceView方法的构造函数:

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

整个活动代码:

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:

还尝试将一个方法添加到activity类并从surfaceview类中调用它,但结果是相同的。

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);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

不确定这是否有帮助,但请尝试使用其中之一:

  1. 我宁愿只将Context传递给视图,而不是整个活动

  2. 或创建一个侦听器并在视图中将其激活。抓住活动并从那里开始其他活动

  3. 编辑: 根据编辑过的问题,尽量不要在MenuPanel类中使用MenuActivity。只需存储Context并使用它而不是menuActivity。

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

    然后:

    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);
    

    但我仍然更喜欢创建一个监听器(2.),在我看来更清洁。

答案 1 :(得分:0)

修改 根据您编辑的代码,在您的MenuPanel中:

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

同时删除自定义构造函数和你的"递归"致电ExitActivity' onCreate

答案 2 :(得分:0)

首先,在意图中使用活动。当你从另一个类开始一个新活动时,还要向Intent添加标记FLAG_ACTIVITY_NEW_TASK

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);

希望这有帮助。