当我尝试重新创建活动时,应用崩溃

时间:2019-10-14 08:18:11

标签: java android

我正在编写一个具有两个Activity的应用程序。第一个将内容视图设置为.xml。在.xml中,单击按钮后,它将调用另一个活动并再次设置视图。

到目前为止,它运行良好。但是,当应用程序达到某个特定点时,它将在第一个活动中调用应recreate()的方法。但是recreate()使我的应用程序崩溃了。

在下面找到我的两个活动:

public class MainActivity extends Activity {


private GameActivity gameAc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);
}

public void startGame(View v) {



    gameAc = new GameActivity();//this line calls the second activity


}



public void setViewActivity(){

    recreate(); // this is the line that crashes the app

}

}

现在这是我的第二项活动

public class GameActivity extends Activity {

//declare class Gamedraw
private GameDraw gameDraw;

//too hold screen dimensions
Point size = new Point();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //get phone dimensions
    Display display = getWindowManager()
            .getDefaultDisplay();

    display.getSize(size);



    //initialse class
    gameDraw = new GameDraw(this, size);



    //set view to gamedraw
    setContentView(gameDraw);

}

@Override
protected void onResume() {
    super.onResume();
    gameDraw.resume();
}

@Override
protected void onPause() {
    super.onPause();
    gameDraw.pause();
}
}

在拳头活动中。 recreate()使应用程序崩溃。

不能完全确定这是否是堆栈跟踪的意思。如果不是,请告诉我。

2019-10-14 12:16:32.987 18795-18795/? I/roid.runningma: Late-enabling -Xcheck:jni
2019-10-14 12:16:33.303 18795-18795/? W/re-initialized>: type=1400 audit(0.0:2662): avc: denied { read } for name="u:object_r:mtk_amslog_prop:s0" dev="tmpfs" ino=106 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:mtk_amslog_prop:s0 tclass=file permissive=0
2019-10-14 12:16:33.309 18795-18795/? E/libc: Access denied finding property "persist.vendor.sys.activitylog"
2019-10-14 12:16:33.467 18795-18795/com.example.android.runningman W/ActivityThread: Application com.example.android.runningman is waiting for the debugger on port 8100...
2019-10-14 12:16:33.469 18795-18795/com.example.android.runningman I/System.out: Sending WAIT chunk
2019-10-14 12:16:34.472 18795-18795/com.example.android.runningman I/System.out: Debugger has connected
2019-10-14 12:16:34.473 18795-18795/com.example.android.runningman I/System.out: waiting for debugger to settle...
2019-10-14 12:16:34.674 18795-18795/com.example.android.runningman I/System.out: waiting for debugger to settle...
2019-10-14 12:16:34.876 18795-18795/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) identical 1 line
2019-10-14 12:16:35.077 18795-18795/com.example.android.runningman I/System.out: waiting for debugger to settle...
2019-10-14 12:16:35.278 18795-18795/com.example.android.runningman I/System.out: waiting for debugger to settle...
2019-10-14 12:16:35.479 18795-18795/com.example.android.runningman I/System.out: waiting for debugger to settle...
2019-10-14 12:16:35.680 18795-18795/com.example.android.runningman I/System.out: waiting for debugger to settle...
2019-10-14 12:16:35.881 18795-18795/com.example.android.runningman I/System.out: waiting for debugger to settle...
2019-10-14 12:16:36.081 18795-18795/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) identical 1 line
2019-10-14 12:16:36.282 18795-18795/com.example.android.runningman I/System.out: waiting for debugger to settle...
2019-10-14 12:16:36.483 18795-18795/com.example.android.runningman I/System.out: debugger has settled (1301)
2019-10-14 12:16:37.219 18795-18795/com.example.android.runningman D/PackageManager: return text:  RunningManfor package: com.example.android.runningman
2019-10-14 12:16:37.272 18795-18795/com.example.android.runningman D/OpenGLRenderer: Skia GL Pipeline
2019-10-14 12:16:37.275 18795-18795/com.example.android.runningman I/SurfaceFactory: [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@fc013c8
2019-10-14 12:16:37.307 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2663): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.310 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.310 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2664): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.315 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.317 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2665): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.319 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.320 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2666): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.323 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.323 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2667): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.327 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.327 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2668): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.331 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.333 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2669): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.336 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.337 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2670): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.340 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.340 18795-18795/com.example.android.runningman W/roid.runningman: type=1400 audit(0.0:2671): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:37.344 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.404 18795-18795/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) identical 14 lines
2019-10-14 12:16:37.409 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.416 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.578 18795-18795/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) identical 34 lines
2019-10-14 12:16:37.581 18795-18795/com.example.android.runningman D/skia: SkJpegCodec::onGetPixels fEnTdshp 0 fISOSpeedRatings -1!
2019-10-14 12:16:37.652 18795-18795/com.example.android.runningman I/PhoneWindow: isNeedChangeNaviBarColor taskInfo: [android.app.ActivityManager$RunningTaskInfo@f490221] size: 1
2019-10-14 12:16:37.655 18795-18795/com.example.android.runningman I/PhoneWindow: isAPPNeedChange pkgName: com.example.android.runningman needKeep: false
2019-10-14 12:16:37.655 18795-18795/com.example.android.runningman I/PhoneWindow: isNeedChangeNaviBarColor false
2019-10-14 12:16:37.656 18795-18795/com.example.android.runningman I/PhoneWindow: generateLayout mNavigationBarColor: ff000000
2019-10-14 12:16:37.657 18795-18795/com.example.android.runningman I/PhoneWindow: generateLayout isLightNavi false, Visibility: 0
2019-10-14 12:16:38.124 18795-18795/com.example.android.runningman D/ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
2019-10-14 12:16:38.338 18795-18828/com.example.android.runningman I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-10-14 12:16:38.338 18795-18828/com.example.android.runningman I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2019-10-14 12:16:38.339 18795-18828/com.example.android.runningman I/OpenGLRenderer: Initialized EGL, version 1.4
2019-10-14 12:16:38.339 18795-18828/com.example.android.runningman D/OpenGLRenderer: Swap behavior 2
2019-10-14 12:16:38.374 18795-18828/com.example.android.runningman D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2019-10-14 12:16:38.467 18795-18828/com.example.android.runningman E/ion: ioctl c0044901 failed with code -1: Invalid argument
2019-10-14 12:16:38.463 18795-18795/com.example.android.runningman W/RenderThread: type=1400 audit(0.0:2723): avc: denied { search } for name="clients" dev="debugfs" ino=7489 scontext=u:r:untrusted_app:s0:c200,c256,c512,c768 tcontext=u:object_r:debugfs_ion:s0 tclass=dir permissive=0
2019-10-14 12:16:39.449 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:39.512 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:39.569 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:39.626 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:39.682 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:39.905 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 6 lines
2019-10-14 12:16:39.931 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:39.959 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:40.107 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 7 lines
2019-10-14 12:16:40.120 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:40.134 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:40.984 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 51 lines
2019-10-14 12:16:41.000 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.017 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.036 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.053 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.068 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.087 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.103 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.121 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.136 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.388 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 15 lines
2019-10-14 12:16:41.405 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.422 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.791 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 22 lines
2019-10-14 12:16:41.808 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:41.825 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:42.010 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 11 lines
2019-10-14 12:16:42.027 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:42.045 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:42.346 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 18 lines
2019-10-14 12:16:42.363 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:42.383 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:43.221 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 50 lines
2019-10-14 12:16:43.238 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:43.254 18795-18795/com.example.android.runningman E/InputEventReceiver: Exception dispatching input event.
2019-10-14 12:16:43.254 18795-18795/com.example.android.runningman E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-10-14 12:16:43.255 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:43.271 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:43.292 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:43.308 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:43.327 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:43.327 18795-18795/com.example.android.runningman E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.app.ActivityThread.getLooper()' on a null object reference
        at android.app.Activity.recreate(Activity.java:5583)
        at com.example.android.runningman.MainActivity.setViewActivity(MainActivity.java:60)
        at com.example.android.runningman.GameDraw.onTouchEvent(GameDraw.java:389)
        at android.view.View.dispatchTouchEvent(View.java:12545)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3085)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2684)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3085)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2684)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3085)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2684)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:441)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1839)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3400)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:399)
        at android.view.View.dispatchPointerEvent(View.java:12789)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5279)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5073)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4586)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4639)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4605)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4745)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4613)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4802)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4586)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4639)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4605)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4613)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4586)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7336)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7305)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7262)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7453)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:326)
        at android.os.Looper.loop(Looper.java:160)
        at android.app.ActivityThread.main(ActivityThread.java:6702)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
2019-10-14 12:16:43.327 18795-18795/com.example.android.runningman D/AndroidRuntime: Shutting down VM
2019-10-14 12:16:43.339 18795-18827/com.example.android.runningman D/Surface: lockCanvas

    --------- beginning of crash
2019-10-14 12:16:43.368 18795-18795/com.example.android.runningman E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.runningman, PID: 18795
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.app.ActivityThread.getLooper()' on a null object reference
        at android.app.Activity.recreate(Activity.java:5583)
        at com.example.android.runningman.MainActivity.setViewActivity(MainActivity.java:60)
        at com.example.android.runningman.GameDraw.onTouchEvent(GameDraw.java:389)
        at android.view.View.dispatchTouchEvent(View.java:12545)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3085)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2684)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3085)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2684)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3085)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2684)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:441)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1839)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3400)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:399)
        at android.view.View.dispatchPointerEvent(View.java:12789)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5279)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5073)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4586)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4639)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4605)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4745)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4613)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4802)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4586)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4639)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4605)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4613)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4586)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7336)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7305)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7262)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7453)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:326)
        at android.os.Looper.loop(Looper.java:160)
        at android.app.ActivityThread.main(ActivityThread.java:6702)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
2019-10-14 12:16:43.390 18795-18827/com.example.android.runningman I/chatty: uid=10200(com.example.android.runningman) Thread-2 identical 3 lines
2019-10-14 12:16:43.409 18795-18827/com.example.android.runningman D/Surface: lockCanvas
2019-10-14 12:16:43.426 18795-18795/com.example.android.runningman I/Process: Sending signal. PID: 18795 SIG: 9

~~

这是调用setViewActivity()的代码; 该代码在我的GameDraw类中(在第二个活动中被调用。该类处理屏幕用户输入并绘制游戏。

 public class GameDraw extends SurfaceView implements Runnable {

 private MainActivity MAobject;

  public GameDraw(Context context, Point size) {
 //this handles all drawing to screen
 }


  @Override
   public boolean onTouchEvent(MotionEvent event) {
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN :

            if (death == false) {

                if (firstRun) {
                    firstRun = false;
                    gravity = true;
                    Log.e("viewtrace", "test");

                } else {
                    jumping = true;

                }
            }
            else{
                Log.e("viewtrace", "1");
                MAobject.setViewActivity();

            }

            break;
    }

    return true;
}
}

添加了调用第一个活动的清单(ActivityMain)。

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.runningman">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"

        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="landscape"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

 </manifest>

2 个答案:

答案 0 :(得分:0)

活动类不应直接实例化(即,否new YourActivity())。它们是由系统创建和销毁的。要开始新活动,请创建一个Intent,然后调用startActivity()类的Context方法。

以您的情况为例,要启动GameActivity,您应调用类似的

startActivity(new Intent(this, GameActivity.class));

使用您的startGame()方法。

对于更新后的问题,您还需要将对setViewActivity()的呼叫替换为

getContext().startActivity(new Intent(getContext(), MainActivity.class));

您可以在Android开发者指南中找到更多信息,例如here

自您更新问题以来,另一个问题就显而易见了: 在Android中,您需要在清单中声明所有Activity。您的清单仅包含MainActivity,因此无法启动GameActivity。要解决此问题,您需要在清单文件中(application标签之间)添加以下声明:

<activity
    android:name=".GameActivity"
    android:label="Some name for your Activity" />

答案 1 :(得分:0)

您在哪里打电话? setViewActivity();

尝试此呼叫其他活动


     public void startGame(View v) 
     {
         startActivity(new Intent(this, GameActivity.class));
     }