如何解决同一活动上有多个按钮
首先,让我感谢您的关注。我对开发仍然很陌生,但是现在我成功地有了一个不错的启动画面,它正在朝着注册和登录活动前进,以在按下按钮后显示出来。下面的代码适用于登录活动,但是按下注册按钮后,我的应用程序崩溃了。我怀疑我在做一些愚蠢的事情,但是我尝试了几种方法来介绍这些按钮并使它们起作用,但是似乎什么也没有。
点击SignUpBtn时Logcat
2020-09-18 11:32:21.732 1217-2251/? I/HwResolverManager: isMultiScreenCollaborationEnabled: Operate settings exception! 2020-09-18 11:32:28.474 30804-30804/com.example.doadventuresv30 I/BlockMonitor: dispatchingThrewException In MainThread 2020-09-18 11:32:28.474 30804-30804/com.example.doadventuresv30 I/QarthLog: [PatchStore] createdisableExceptionQarthFile 2020-09-18 11:32:28.475 30804-30804/com.example.doadventuresv30 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.doadventuresv30,PID: 30804 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.doadventuresv30/com.example.doadventuresv30.SignUpActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setonClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:3895) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4074) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8347) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setonClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.doadventuresv30.SignUpActivity.initactions(SignUpActivity.java:59) at com.example.doadventuresv30.SignUpActivity.onCreate(SignUpActivity.java:30) at android.app.Activity.performCreate(Activity.java:8085) at android.app.Activity.performCreate(Activity.java:8073) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320) at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:3868) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4074) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8347) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055) 2020-09-18 11:32:28.573 2157-2381/? I/HwPacketFilterBypass: isAllowSendSmartCuretochr false,no exceptions occur,dropCnt:0 2020-09-18 11:32:31.597 1217-1822/? W/AwareLog: AwareMem_MemoryUtils: trim Memory illegal arg exception,proc:30209 userId:-2 level:40 fromIAware:true 2020-09-18 11:32:38.212 13449-13912/? I/SmartChgBase/Arb_HidlParam: Apply Skipped: IsApplyExcept return true. [moduleName:NightChargeAi,param:soc_control,detailsWithValue:source=trigger;event=heartbeat;chargestatus=start;value=1 95 100 1 0;]
public class SplashActivity extends AppCompatActivity {
private ImageView iconImageView;
private Button SignUpBtn,LoginBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SignUpBtn = findViewById(R.id.SignUpBtn);
SignUpBtn.setonClickListener((v) ->{
Intent intent = new Intent(SplashActivity.this,SignUpActivity.class);
startActivity(intent);
}
LoginBtn = findViewById(R.id.LoginBtn);
LoginBtn.setonClickListener((v) ->{
Intent in = new Intent(SplashActivity.this,LoginActivity.class);
startActivity(in);
});
}
}
public class SignUpActivity extends AppCompatActivity {
TextView createTextView;
LinearLayout facebookLinearLayout,twitterLinearLayout,googlePlusLinearLayout;
Button registerButton;
ImageView bgImageView;
Button forgotButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
initUI();
initDataBindings();
initactions();
}
//region Init Functions
private void initUI() {
forgotButton = findViewById(R.id.forgotButton);
facebookLinearLayout = findViewById(R.id.facebookLinearLayout);
twitterLinearLayout = findViewById(R.id.twitterLinearLayout);
googlePlusLinearLayout = findViewById(R.id.googlePlusLinearLayout);
registerButton = findViewById(R.id.registerButton);
bgImageView = findViewById(R.id.bgImageView);
}
private void initDataBindings() {
int id = R.drawable.lightbg;
Utils.setimageToImageView(getApplicationContext(),bgImageView,id);
}
private void initactions() {
forgotButton.setonClickListener(view -> {
Toast.makeText(getApplicationContext(),"Clicked Forgot Password",Toast.LENGTH_SHORT).show();
});
createTextView.setonClickListener(view -> {
Toast.makeText(getApplicationContext(),"Clicked Create Account",Toast.LENGTH_SHORT).show();
});
facebookLinearLayout.setonClickListener(view -> {
Toast.makeText(getApplicationContext(),"Clicked Facebook",Toast.LENGTH_SHORT).show();
});
twitterLinearLayout.setonClickListener(view -> {
Toast.makeText(getApplicationContext(),"Clicked Twitter",Toast.LENGTH_SHORT).show();
});
googlePlusLinearLayout.setonClickListener(view -> {
Toast.makeText(getApplicationContext(),"Clicked Google Plus",Toast.LENGTH_SHORT).show();
});
registerButton.setonClickListener(view -> {
Toast.makeText(getApplicationContext(),"Clicked Sign Up",Toast.LENGTH_SHORT).show();
});
}
//endregion
}
解决方法
createTextView
为空。用findViewById()
初始化它。
您必须使用ID错误的注册按钮作为参考。
SignUpBtn = findViewById(R.id.SignUpBtn);
检查R.id.SignupButton是否正确引用了SignUp Button。 看来 activity_splash.xml 不包含此ID。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。