为什么我在Android Studio中的应用在按下一个按钮时冻结?

问题描述 投票:-4回答:1

我做了一个应用程序(在Android studio 3.0.1中),它可以显示输入的数字是三角形数字,还是方形数字,还是两者都有,或者没有。然而,当我输入一个数字并按下 "回车 "按钮时,应用程序就会冻结,并停止响应。我试着改变循环的类型(forwhile)......但没有任何结果。

我在下面给出了.java代码......

package com.example.home.numbershape;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {




    class testNumber {
        int n;
        int i = 0;
        int tTestnum;
        int sTestNum;
        boolean triangular;
        boolean square;

        boolean triangleTest(){
            while( tTestnum <= n){
                int tTestnum = i*(i+1)/2;

                if( n == tTestnum){
                    triangular = true;
                } else if(n > tTestnum){
                    i++;
                } else {
                    triangular = false;
                }

            }
            return triangular;
        }

        boolean squareTest(){

            while( sTestNum <= n){
                int sTestNum = i*i;

                if( n == sTestNum ){
                    square = true;
                } else if(n > sTestNum){
                    i++;
                } else {
                    square = false;
                }
            }
            return square;



        }


    }





    public void submitFunction(View view){

        testNumber num = new testNumber();

        EditText inNum = (EditText) findViewById(R.id.inputNum);
        String nString = inNum.getText().toString();
        num.n = Integer.parseInt(nString);


        if(num.triangleTest()){
            if(num.squareTest()){
                Toast.makeText(this, "It is both a triangular and a square number!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "It is a triangular number.", Toast.LENGTH_LONG).show();
            }

        } else if(num.squareTest()){
            if(num.triangleTest()){
                Toast.makeText(this, "It is both a triangular and a square number!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "It is a square number.", Toast.LENGTH_LONG).show();
            }
        }
         else {
            Toast.makeText(this, "It is neither.", Toast.LENGTH_LONG).show();
        }

        Log.i("Info", "Submitted and checked.");



    }





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

下面是.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="com.example.home.numbershape.MainActivity">

    <EditText
        android:id="@+id/inputNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="21dp"
        android:layout_marginEnd="84dp"
        android:layout_marginStart="85dp"
        android:layout_marginTop="33dp"
        android:ems="10"
        android:hint="enter the number."
        android:inputType="number"
        android:selectAllOnFocus="true"
        android:singleLine="false"
        app:layout_constraintBottom_toTopOf="@+id/submitButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="292dp"
        android:layout_height="113dp"
        android:layout_marginBottom="33dp"
        android:layout_marginEnd="46dp"
        android:layout_marginStart="46dp"
        android:layout_marginTop="29dp"
        android:text="This app tells you whether the number which you have typed in is a Square number, triangular number, both or none. So give it a go!"
        android:textAlignment="center"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/inputNum"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="221dp"
        android:layout_marginEnd="148dp"
        android:layout_marginStart="148dp"
        android:layout_marginTop="21dp"
        android:onClick="submitFunction"
        android:text="enter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputNum" />
</android.support.constraint.ConstraintLayout>

我在代码结构上有什么错误吗?请告诉有什么问题...谢谢!

P.S: 这里是日志 05-24 10:35:45.471 2322-2322/com.google.android.gms.persistent E/ActivityThread: Service com.google.android.gms.autofill.service.AutofillService has leaked IntentReceiver com.google.android.gms.autofill.smsretriever.TracingSmsBroadcastReceiver@88dcee that was originally registered here. Are you missing a call to unregisterReceiver()? android.app.IntentReceiverLeaked: Service com.google.android.gms.autofill.service.AutofillService has leaked IntentReceiver com.google.android.gms.autofill.smsretriever.TracingSmsBroadcastReceiver@88dcee that was originally registered here. Are you missing a call to unregisterReceiver()? at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1588) at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1368) at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1515) at android.app.ContextImpl.registerReceiver(ContextImpl.java:1488) at android.app.ContextImpl.registerReceiver(ContextImpl.java:1476) at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627) at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627) at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627) at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627) at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627) at lkl.<init>(:com.google.android.gms@[email protected] (040700-294335909):5) at ljc.<init>(:com.google.android.gms@[email protected] (040700-294335909):1) at kxb.a(:com.google.android.gms@[email protected] (040700-294335909):10) at bzlo.a(:com.google.android.gms@[email protected] (040700-294335909):5) at kxj.a(:com.google.android.gms@[email protected] (040700-294335909):2) at bzlo.a(:com.google.android.gms@[email protected] (040700-294335909):5) at kvm.b(:com.google.android.gms@[email protected] (040700-294335909):0) at lfu.onFillRequest(:com.google.android.gms@[email protected] (040700-294335909):70) at android.service.autofill.-$$Lambda$I0gCKFrBTO70VZfSZTq2fj-wyG8.accept(Unknown Source:8) at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:300) at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:195) at com.android.internal.util.function.pooled.OmniFunction.run(OmniFunction.java:86) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 05-24 10:35:59.300 2114-2146/system_process E/memtrack: Couldn't load memtrack module

java android android-studio freeze
1个回答
-1
投票

你没有在你的Java代码中引用你的按钮。试着将这段代码添加到"... "中。onCreate 你的 Main 活动

Button Sbutton = findViewById(R.id.submitButton);
© www.soinside.com 2019 - 2024. All rights reserved.