[英]Why does my app keep crashing when I have 2 setOnclickListener?
My app keeps crashing when I set 2 setOnclickListener
.当我设置 2
setOnclickListener
时,我的应用程序不断崩溃。 The interesting thing is, when I only have 1 button intent, it doesn't crash.有趣的是,当我只有 1 个按钮意图时,它不会崩溃。 When I have 2 buttons, the new button does not bring me to new screen, and the old one just crashes the whole app.
当我有 2 个按钮时,新按钮不会将我带到新屏幕,而旧按钮只会使整个应用程序崩溃。
Here's my code for the "old" button:这是我的“旧”按钮代码:
SignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
If that's the only thing, then everything works.如果这是唯一的事情,那么一切正常。 But when I add following, the new one doesn't work and old one crashes:
但是当我添加以下内容时,新的不起作用并且旧的崩溃:
Support.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});
Here's the Logcat:这是Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
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.hoversfw.notes.LoginActivity.onCreate(LoginActivity.java:96)
在 com.hoversfw.notes.LoginActivity.onCreate(LoginActivity.java:96)
And LoginActivity.java Line 96 is the beginning of Support.setOnclickListener.
而LoginActivity.java第96行是Support.setOnclickListener的开始。 Also, I have everything declared like:
另外,我声明的所有内容如下:
Support=findViewById(R.id.Support);
SignUp=findViewById(R.id.textView);
Here's my XML file code:这是我的 XML 文件代码:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Already have an account? Sign in!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/Support"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginEnd="29dp"
android:text="Support"
app:layout_constraintEnd_toStartOf="@+id/about"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
I also have all ID matched, Android Studio detects no error.我也匹配了所有 ID,Android Studio 没有检测到错误。 I wonder what's causing the problem.
我想知道是什么导致了这个问题。
UPDATE更新
I have everything matched, like java file and xml, no ID is same, everything declared.我有所有匹配的东西,比如 java 文件和 xml,没有 ID 相同,所有声明。 And I don't think there is any problem with XML.
而且我认为XML没有任何问题。
SignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
/*Support.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});*/
If it's like above, then SignUp it works.如果像上面那样,那么 SignUp 就可以了。 If it's like
如果它像
SignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
Support.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});
Both of them doesn't work.他们两个都不行。 (The difference is last one I commented Support.setOnClickListener..... but this one I made it as code.)
(不同的是最后一个我评论了 Support.setOnClickListener .....但是这个我把它作为代码。)
I can't confirm directly your error because you don't show your xml, but maybe you can do it.我无法直接确认您的错误,因为您没有显示 xml,但也许您可以做到。
If you are using 2 buttons, then you need to link them as button for both of them.如果您使用 2 个按钮,则需要将它们链接为两个按钮。
UPDATED更新
Since, you are using button and textview.因为,您正在使用按钮和 textview。 I updated the answer.
我更新了答案。
This code at xml此代码位于 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".Main2Activity">
<TextView
android:id="@+id/txtViewSupport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Support" />
<Button
android:id="@+id/btnSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SignUp" />
</LinearLayout>
This code at Java此代码在 Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button buttonSignUp = findViewById(R.id.btnSignUp);
TextView textViewSupport = findViewById(R.id.txtViewSupport);
buttonSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
textViewSupport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.