gpt4 book ai didi

java - 无法更改 Android Activity

转载 作者:行者123 更新时间:2023-12-01 12:21:53 25 4
gpt4 key购买 nike

我尝试从菜单 Activity 更改为任何其他 Activity ,但整个应用程序停止响应,不知道为什么。

这是我使用的代码(我从 Activity 的 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;;
}

整个 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();
}
}

编辑2:

还尝试向 Activity 类添加一个方法并从 SurfaceView 类调用它,但结果是相同的。

编辑3:

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

最佳答案

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

  1. 我宁愿只将上下文传递给 View ,而不是整个 Activity

  2. 或者创建一个监听器并在 View 中启动它。在 Activity 中捕获它并从那里启动其他 Activity

编辑:根据编辑的问题,尽量不要在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.),在我看来它更干净。

关于java - 无法更改 Android Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26608003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com