我的应用程序没有给我任何输出,当我点击下一个活动的按钮时崩溃了?

t98cgbkg  于 2021-06-26  发布在  Java
关注(0)|答案(3)|浏览(454)

我正在尝试为羽毛球运动员制作一个随机的球员配对应用程序,但是当我运行我的代码时,它不会给我任何结果。在编写配对算法之前,如果我只使用intent value传递textview.settext(intent\u value\u xyz),它工作得很好,但当我用stringbuilder传递textview时就会出现问题(即;textview.settext(生成器)。如果intent值为null,应用程序会突然崩溃…请帮助我修复
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);
}

}
主活动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>

第二个活动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>
sulc1iza

sulc1iza1#

您是否向androidmanifest.xml添加了第二个活动?
android使用它作为intent系统的一部分。如果它不在那里

oug3syen

oug3syen2#

我建议您在以下位置调试代码:

Integer.parseInt(numberOfPlayer.getText().toString());

可能无法将值解析为字符串!告诉我你的崩溃错误,然后我可以帮你。谢谢!

ogsagwnx

ogsagwnx3#

嘿,伙计,如果你调试你的程序,你会发现你的问题,但我认为问题出在你的转换:int message=integer.parseint(numberofplayer.gettext().tostring());告诉我结果!

相关问题