简体   繁体   English

在 Android Studio 中单击按钮时,onClickListener 使应用程序崩溃

[英]onClickListener crashes the app when Button clicked in Android Studio

I am trying to make an application in which the word Sorry.我正在尝试制作一个应用程序,其中包含“抱歉”一词。 will print as much time as the input given by user button as soon as the button is clicked the crashes, Even if I try set a Toast in onclicklistener, the app doesn't crash.一旦单击按钮崩溃,将打印与用户按钮给出的输入一样多的时间,即使我尝试在 onclicklistener 中设置 Toast,应用程序也不会崩溃。 but toast is not formed.但吐司没有形成。 I think there is some problem in the Java file: Here's the java file:我认为 Java 文件中存在一些问题: 这是 java 文件:

    package com.example.satvara.vipul.instantmessage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Sorry extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sorry);
        this.setTitle(R.string.activity_sorry);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        EditText editText= findViewById(R.id.editText);
        String str = editText.getText().toString();
        final int i= Integer.parseInt(str);
        final TextView textView=findViewById(R.id.textView);
        Button btn=findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int j=0; j>i;j++){
                    textView.setText("Sorry!");
                }
            }
        });

    }

}

Here's the XML file:这是 XML 文件:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".Sorry">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Number:"
                android:padding="10dp"
                android:textSize="20dp"
                android:textColor="@color/text"
                android:layout_column="1"/>
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter Here"
                android:paddingLeft="7dp"
                android:paddingRight="7dp"
                android:backgroundTint="@color/colorPrimaryDark"
                android:textCursorDrawable="@drawable/cursor_color"
                android:inputType="number"
                android:id="@+id/editText"/>
        </TableRow>
        <TableRow>
            <Button
                android:layout_column="2"
                android:hint="Send via Whatsapp"
                android:id="@+id/btn"
                ></Button>
        </TableRow>
        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView">
            </TextView>
        </TableRow>
    </TableLayout>

</android.support.constraint.ConstraintLayout>

You have to put get edit text in onClick().您必须将获取编辑文本放入 onClick() 中。 Your for loop condition is wrong.你的 for 循环条件是错误的。 Even if it was correct you were just setting the same text for i times.即使它是正确的,您也只是设置了 i 次相同的文本。 You should only use the loop for setting the value of the yourString and later when the loop is over setText(yourString)您应该只使用循环来设置 yourString 的值,然后在循环结束时使用 setText(yourString)

public class Sorry extends AppCompatActivity {
    EditText edittext;
    TextView textView;
    Button btn;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sorry);
            this.setTitle(R.string.activity_sorry);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            editText= findViewById(R.id.editText);
            textView=findViewById(R.id.textView);
            btn=findViewById(R.id.btn);

            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                 String str = editText.getText().toString();
                 int i= Integer.parseInt(str);
                  String myText="";
                    for (int j=0; j<i;j++){
                        myText=myText.concat("Sorry \n");
                    }
                   textView.setText(myText);
                }
            });

        }

    };

The problem is in these lines:问题出在以下几行:

String str = editText.getText().toString();
final int i= Integer.parseInt(str);

Your EditText is not yet created on the UI in the activity's onCreate method.您的EditText尚未在活动的onCreate方法中的 UI 上创建。

You need to read the value through some UI event, after the entire UI is formed.在整个 UI 形成之后,您需要通过一些 UI 事件来读取值。

For example, you can read the EditText when the button is clicked.例如,您可以在单击按钮时读取EditText

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String str = editText.getText().toString();
            int i= Integer.parseInt(str);

            for (int j=0; j>i;j++){
                textView.setText("Sorry!");
            }
        }
    });

In the above code, the text inside the TextView will get overwritten after every loop.在上面的代码中, TextView中的文本在每次循环后都会被覆盖。 So for any value of i >= 1, you will only see a single "Sorry!"因此,对于 i >= 1 的任何值,您只会看到一个“对不起!” inside the TextView .TextView内部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM