Android 将OnClickListener()应用于结束场景中的新按钮

Android 将OnClickListener()应用于结束场景中的新按钮,android,android-layout,android-button,android-transitions,Android,Android Layout,Android Button,Android Transitions,我的应用程序以一个按钮开始。按下它会使此按钮向下滑动,新按钮滑入同一活动。我想将OnClickListeners设置为这些新按钮,以便使用它们启动使用intent的新活动。问题是当我尝试这样做时,应用程序甚至在启动之前就崩溃了 我的应用程序从此处过渡: 06-02 15:02:30.355 31007-31007/? I/art: Late-enabling -Xcheck:jni 06-02 15:02:30.477 31007-31007/com.techpappy.whattoeat W/

我的应用程序以一个按钮开始。按下它会使此按钮向下滑动,新按钮滑入同一活动。我想将OnClickListeners设置为这些新按钮,以便使用它们启动使用intent的新活动。问题是当我尝试这样做时,应用程序甚至在启动之前就崩溃了

我的应用程序从此处过渡:

06-02 15:02:30.355 31007-31007/? I/art: Late-enabling -Xcheck:jni
06-02 15:02:30.477 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.753 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.845 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.924 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.986 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.033 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.080 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.124 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.172 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.215 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.258 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.260 31007-31007/com.techpappy.whattoeat W/System: ClassLoader referenced unknown path: /data/app/com.techpappy.whattoeat-2/lib/arm
06-02 15:02:31.263 31007-31007/com.techpappy.whattoeat I/InstantRun: starting instant run server: is main process
06-02 15:02:31.356 31007-31007/com.techpappy.whattoeat W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-02 15:02:31.463 31007-31007/com.techpappy.whattoeat D/AndroidRuntime: Shutting down VM


                                                                         --------- beginning of crash
06-02 15:02:31.464 31007-31007/com.techpappy.whattoeat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.techpappy.whattoeat, PID: 31007
                                                                         Theme: themes:{}
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.techpappy.whattoeat/com.techpappy.whattoeat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5449)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                             at com.techpappy.whattoeat.MainActivity.onCreate(MainActivity.java:62)
                                                                             at android.app.Activity.performCreate(Activity.java:6339)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2399)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5449) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

对此:

06-02 15:02:30.355 31007-31007/? I/art: Late-enabling -Xcheck:jni
06-02 15:02:30.477 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.753 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.845 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.924 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.986 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.033 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.080 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.124 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.172 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.215 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.258 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.260 31007-31007/com.techpappy.whattoeat W/System: ClassLoader referenced unknown path: /data/app/com.techpappy.whattoeat-2/lib/arm
06-02 15:02:31.263 31007-31007/com.techpappy.whattoeat I/InstantRun: starting instant run server: is main process
06-02 15:02:31.356 31007-31007/com.techpappy.whattoeat W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-02 15:02:31.463 31007-31007/com.techpappy.whattoeat D/AndroidRuntime: Shutting down VM


                                                                         --------- beginning of crash
06-02 15:02:31.464 31007-31007/com.techpappy.whattoeat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.techpappy.whattoeat, PID: 31007
                                                                         Theme: themes:{}
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.techpappy.whattoeat/com.techpappy.whattoeat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5449)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                             at com.techpappy.whattoeat.MainActivity.onCreate(MainActivity.java:62)
                                                                             at android.app.Activity.performCreate(Activity.java:6339)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2399)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5449) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

MainActivity.java(注释部分是我试图应用于按钮的代码)

主初始布局(Initial\u Layout.xml


尝试动态创建按钮,然后将onClickListener添加到按钮中,并将其添加到布局中。

IMO,当您尝试获取视图时,
vegbtn
nonegbtn
为空,因此对它们的任何操作都将导致
NullPointerException
。尝试将侦听器添加到转换对象,并在
onTransitionEnd
方法上执行赋值

也许是这样:

mainmenuTrans.addListener(new Transition.TransitionListener() {
    @Override
    public void onTransitionStart(Transition transition) { }

    @Override
    public void onTransitionEnd(Transition transition) {
        vegbtn = (Button)findViewById(R.id.vegButton);
        nonvegbtn = (Button)findViewById(R.id.nonvegbutton);

        vegbtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                final Intent i = new Intent(MainActivity.this,VegOptions.class);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });

        nonvegbtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Intent i = new Intent(MainActivity.this, NonVegOptions.class);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });
    }

    @Override
    public void onTransitionCancel(Transition transition) {}

    @Override
    public void onTransitionPause(Transition transition) { }

    @Override
    public void onTransitionResume(Transition transition) { }
});

让我知道它是否有效。我没有一个完整的项目来测试这一点。

您尝试的东西有什么不起作用?为什么不使用片段?您确定
vegbtn
nonegbtn
不为空吗?我的意思是,创建活动时,带有这些ID的视图还不存在。试着在过渡完成后再做。至于如何做到这一点,请看Android的文档。我以前从未使用过场景转换,所以我不能给你一个明确的答案。@codeMagic我不太清楚。应用程序刚启动就崩溃了,但当我注释掉vegbtn和NONOVEGBTH代码时,它就工作了,这就是为什么我不知道该尝试什么。这就是我问题的答案。。。“它崩溃了”。在这种情况下,你需要发布StackTrace这是有意义的,只有一个问题。我是将此添加到代码中(保持以前的转换代码不变)还是用此替换代码。您能指出我应该在java文件中的何处插入此代码吗?请将此代码放在btn按钮的OnClickListener代码之后。工作得很有魅力。
06-02 15:02:30.355 31007-31007/? I/art: Late-enabling -Xcheck:jni
06-02 15:02:30.477 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.753 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.845 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.924 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:30.986 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.033 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.080 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.124 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.172 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.215 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.258 31007-31007/com.techpappy.whattoeat W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.techpappy.whattoeat-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.techpappy.whattoeat-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
06-02 15:02:31.260 31007-31007/com.techpappy.whattoeat W/System: ClassLoader referenced unknown path: /data/app/com.techpappy.whattoeat-2/lib/arm
06-02 15:02:31.263 31007-31007/com.techpappy.whattoeat I/InstantRun: starting instant run server: is main process
06-02 15:02:31.356 31007-31007/com.techpappy.whattoeat W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-02 15:02:31.463 31007-31007/com.techpappy.whattoeat D/AndroidRuntime: Shutting down VM


                                                                         --------- beginning of crash
06-02 15:02:31.464 31007-31007/com.techpappy.whattoeat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.techpappy.whattoeat, PID: 31007
                                                                         Theme: themes:{}
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.techpappy.whattoeat/com.techpappy.whattoeat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5449)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                             at com.techpappy.whattoeat.MainActivity.onCreate(MainActivity.java:62)
                                                                             at android.app.Activity.performCreate(Activity.java:6339)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2399)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5449) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
mainmenuTrans.addListener(new Transition.TransitionListener() {
    @Override
    public void onTransitionStart(Transition transition) { }

    @Override
    public void onTransitionEnd(Transition transition) {
        vegbtn = (Button)findViewById(R.id.vegButton);
        nonvegbtn = (Button)findViewById(R.id.nonvegbutton);

        vegbtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                final Intent i = new Intent(MainActivity.this,VegOptions.class);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });

        nonvegbtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Intent i = new Intent(MainActivity.this, NonVegOptions.class);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });
    }

    @Override
    public void onTransitionCancel(Transition transition) {}

    @Override
    public void onTransitionPause(Transition transition) { }

    @Override
    public void onTransitionResume(Transition transition) { }
});