java - 当我单击下一个活动的按钮时,我的应用程序没有给我任何输出并崩溃?
问题描述
我正在尝试为羽毛球运动员制作一个随机的球员配对应用程序,但是当我运行我的代码时,它没有给我任何结果。在编写配对算法之前,如果我只是使用意图值来传递 textview.setText(intent_value_xyz) 它可以正常工作,但是当我使用 Stringbuilder 传递 textview(即 textview.setText(builder))时会出现问题。此外,如果意图值为空,应用程序突然崩溃......帮我修复
mainactivity的java代码
package com.example.happybirthday;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static String EXTRA_MESSAGE ="com.example.happybirthday.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goButton(View view) {
Toast.makeText(this, "You are ready to Go ! ",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, second.class);
EditText numberOfPlayer =(EditText) findViewById(R.id.numberOfPlayer);
int message = Integer.parseInt(numberOfPlayer.getText().toString());
intent = intent.putExtra(String.valueOf(EXTRA_MESSAGE),message);
startActivity(intent);
}
}
SecondActivity Java 代码
package com.example.happybirthday;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static String EXTRA_MESSAGE ="com.example.happybirthday.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goButton(View view) {
Toast.makeText(this, "You are ready to Go ! ",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, second.class);
EditText numberOfPlayer =(EditText) findViewById(R.id.numberOfPlayer);
int message = Integer.parseInt(numberOfPlayer.getText().toString());
intent = intent.putExtra(String.valueOf(EXTRA_MESSAGE),message);
startActivity(intent);
}
}
MainActivity Xml
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="ExtraText">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="178dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/person_text"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/numberOfPlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0"
android:ems="10"
android:gravity="center"
android:hint="@string/enter_no_of_players"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goButton"
android:text="@string/go_text_onButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/numberOfPlayer"
app:layout_constraintVertical_bias="0.495" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/first_view"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
SecondActivity Xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".second">
<TextView
android:id="@+id/textView_second"
android:layout_width="387dp"
android:layout_height="704dp"
android:gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
我建议您在以下位置调试代码:
Integer.parseInt(numberOfPlayer.getText().toString());
也许无法将值解析为字符串!让我知道您的崩溃错误,然后我可以帮助您。谢谢!