将 OnClickListener() 应用于结束场景中的新按钮
Apply OnClickListener() to new Buttons in end scene
我的应用程序从一个按钮开始。按下它会导致此按钮向下滑动,并且新按钮会滑入相同的 activity。我想将 OnClickListeners 设置为这些新按钮,以便我可以使用它们来使用意图启动新活动。问题是当我尝试这样做时,应用程序甚至在启动之前就崩溃了。
我的应用从这个过渡:
对此:
MainActivity.java(注释部分是我试图应用到按钮的代码)
package com.techpappy.whattoeat;
import android.content.Intent;
import android.os.Build;
//import android.support.transition.Fade;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.*;
import android.transition.Slide;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
//import com.transitionseverywhere.*;
//import com.transitionseverywhere.Scene;
//import com.transitionseverywhere.TransitionManager;
public class MainActivity extends AppCompatActivity {
private Button vegbtn, nonvegbtn;
// private TextView vegtext, nonvegtext, prefertext;
ViewGroup container;
Scene current;
Button btn;
Transition mainmenuTrans;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
setContentView(R.layout.initial_layout);
mainmenuTrans = TransitionInflater.from(this).inflateTransition(R.transition.slideinleft);
container = (ViewGroup)findViewById(R.id.container);
current = Scene.getSceneForLayout(container, R.layout.scene1, this);
btn = (Button)findViewById(R.id.btn);
mainmenuTrans = TransitionInflater.from(this).inflateTransition(R.transition.slideinleft);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.go(current, mainmenuTrans);
}
});
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);
}
}); */
}
}
主要初始布局(initial_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:clipToPadding="false"
android:clipChildren="false"
tools:context="com.techpappy.whattoeat.MainActivity"
android:id="@+id/container"
android:background="@color/cherryRed">
<!-- <RelativeLayout
android:id="@+id/prefertext_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:gravity="center">
</RelativeLayout> -->
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Started !"
android:textSize="30sp"
android:backgroundTint="@color/cherryRed"
android:background="?attr/selectableItemBackground"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
MainActivity的结束场景(scene1.xml)
<merge
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What would you prefer ?"
android:textSize="20sp"
android:background="@color/cherryRed"
android:elevation="4dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:textColor="@color/wineBrown"
android:id="@+id/prefertext" />
<LinearLayout
android:id="@+id/BtnHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_gravity="center">
<FrameLayout
android:id="@+id/VegBtnHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/vegButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:clickable="true"
android:layout_gravity="center"
android:backgroundTint="@color/cherryRed"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/vegtext"
android:layout_width="200dp"
android:layout_height="50dp"
android:text="Vegetarian"
android:textSize="30sp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#6B2737"
android:fontFamily="monospace"
android:textStyle="bold"/>
</FrameLayout>
<FrameLayout
android:id="@+id/NonVegBtnHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<Button
android:id="@+id/nonvegbutton"
android:layout_width="350dp"
android:layout_height="50dp"
android:backgroundTint="@color/cherryRed"
android:clickable="true"
android:layout_gravity="center"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/nonvegtext"
android:layout_width="275dp"
android:layout_height="50dp"
android:text="Non Vegetarian"
android:textSize="30sp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#6B2737"
android:fontFamily="monospace"
android:textStyle="bold"/>
</FrameLayout>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Started !"
android:textSize="30sp"
android:backgroundTint="@color/cherryRed"
android:visibility="invisible"
android:background="?attr/selectableItemBackground"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</merge>
如何将功能应用于这些新的 Veg 和 Non Veg 按钮?
Logcat :
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)
尝试动态创建按钮,然后向它们添加 onClickListener 并将它们添加到布局中。
IMO,当您尝试获取视图时,vegbtn
和 nonvegbtn
为空,因此对它们的任何操作都会导致 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) { }
});
让我知道它是否有效。我没有完整的项目来测试这个。
我的应用程序从一个按钮开始。按下它会导致此按钮向下滑动,并且新按钮会滑入相同的 activity。我想将 OnClickListeners 设置为这些新按钮,以便我可以使用它们来使用意图启动新活动。问题是当我尝试这样做时,应用程序甚至在启动之前就崩溃了。
我的应用从这个过渡:
对此:
MainActivity.java(注释部分是我试图应用到按钮的代码)
package com.techpappy.whattoeat;
import android.content.Intent;
import android.os.Build;
//import android.support.transition.Fade;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.*;
import android.transition.Slide;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
//import com.transitionseverywhere.*;
//import com.transitionseverywhere.Scene;
//import com.transitionseverywhere.TransitionManager;
public class MainActivity extends AppCompatActivity {
private Button vegbtn, nonvegbtn;
// private TextView vegtext, nonvegtext, prefertext;
ViewGroup container;
Scene current;
Button btn;
Transition mainmenuTrans;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
super.onCreate(savedInstanceState);
setContentView(R.layout.initial_layout);
mainmenuTrans = TransitionInflater.from(this).inflateTransition(R.transition.slideinleft);
container = (ViewGroup)findViewById(R.id.container);
current = Scene.getSceneForLayout(container, R.layout.scene1, this);
btn = (Button)findViewById(R.id.btn);
mainmenuTrans = TransitionInflater.from(this).inflateTransition(R.transition.slideinleft);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.go(current, mainmenuTrans);
}
});
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);
}
}); */
}
}
主要初始布局(initial_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:clipToPadding="false"
android:clipChildren="false"
tools:context="com.techpappy.whattoeat.MainActivity"
android:id="@+id/container"
android:background="@color/cherryRed">
<!-- <RelativeLayout
android:id="@+id/prefertext_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:gravity="center">
</RelativeLayout> -->
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Started !"
android:textSize="30sp"
android:backgroundTint="@color/cherryRed"
android:background="?attr/selectableItemBackground"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
MainActivity的结束场景(scene1.xml)
<merge
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What would you prefer ?"
android:textSize="20sp"
android:background="@color/cherryRed"
android:elevation="4dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:textColor="@color/wineBrown"
android:id="@+id/prefertext" />
<LinearLayout
android:id="@+id/BtnHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_gravity="center">
<FrameLayout
android:id="@+id/VegBtnHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/vegButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:clickable="true"
android:layout_gravity="center"
android:backgroundTint="@color/cherryRed"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/vegtext"
android:layout_width="200dp"
android:layout_height="50dp"
android:text="Vegetarian"
android:textSize="30sp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#6B2737"
android:fontFamily="monospace"
android:textStyle="bold"/>
</FrameLayout>
<FrameLayout
android:id="@+id/NonVegBtnHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<Button
android:id="@+id/nonvegbutton"
android:layout_width="350dp"
android:layout_height="50dp"
android:backgroundTint="@color/cherryRed"
android:clickable="true"
android:layout_gravity="center"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/nonvegtext"
android:layout_width="275dp"
android:layout_height="50dp"
android:text="Non Vegetarian"
android:textSize="30sp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#6B2737"
android:fontFamily="monospace"
android:textStyle="bold"/>
</FrameLayout>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Started !"
android:textSize="30sp"
android:backgroundTint="@color/cherryRed"
android:visibility="invisible"
android:background="?attr/selectableItemBackground"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</merge>
如何将功能应用于这些新的 Veg 和 Non Veg 按钮?
Logcat :
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)
尝试动态创建按钮,然后向它们添加 onClickListener 并将它们添加到布局中。
IMO,当您尝试获取视图时,vegbtn
和 nonvegbtn
为空,因此对它们的任何操作都会导致 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) { }
});
让我知道它是否有效。我没有完整的项目来测试这个。