Java 为什么我在Android Studio中的应用程序在我按下按钮时会冻结?
我制作了一个应用程序(在Android studio 3.0.1中),可以告诉你输入的数字是三角形还是正方形,或者两者都是,或者没有。然而,当我输入一个数字并按下“回车”按钮时,应用程序就会冻结,停止响应。我尝试改变循环的类型(for/while)…但没有产生任何结果 我已经给出了下面的.java代码Java 为什么我在Android Studio中的应用程序在我按下按钮时会冻结?,java,android,android-studio,freeze,Java,Android,Android Studio,Freeze,我制作了一个应用程序(在Android studio 3.0.1中),可以告诉你输入的数字是三角形还是正方形,或者两者都是,或者没有。然而,当我输入一个数字并按下“回车”按钮时,应用程序就会冻结,停止响应。我尝试改变循环的类型(for/while)…但没有产生任何结果 我已经给出了下面的.java代码 package com.example.home.numbershape; import android.support.v7.app.AppCompatActivity; import and
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);
}
}
package com.example.home.numbershape;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.Toast;
公共类MainActivity扩展了AppCompatActivity{
类测试号{
int n;
int i=0;
内特特努姆;
int-sTestNum;
布尔三角形;
布尔平方;
布尔三角测试(){
while(tTestnum tTestnum){
i++;
}否则{
三角形=假;
}
}
返回三角形;
}
布尔平方检验(){
while(sTestNum sTestNum){
i++;
}否则{
平方=假;
}
}
返回广场;
}
}
公共无效提交函数(视图){
testNumber num=新的testNumber();
EditText inNum=(EditText)findviewbyd(R.id.inputNum);
字符串nString=inNum.getText().toString();
num.n=整数.parseInt(nString);
if(num.triangleTest()){
if(num.squareTest()){
Toast.makeText(这是“它既是一个三角形数字又是一个正方形数字!”,Toast.LENGTH_LONG.show();
}否则{
Toast.makeText(这是一个三角形数字),Toast.LENGTH_LONG.show();
}
}else if(num.squareTest()){
if(num.triangleTest()){
Toast.makeText(这是“它既是一个三角形数字又是一个正方形数字!”,Toast.LENGTH_LONG.show();
}否则{
Toast.makeText(这是一个平方数),Toast.LENGTH_LONG.show();
}
}
否则{
Toast.makeText(这两个都不是),Toast.LENGTH_LONG.show();
}
Log.i(“信息”,“提交并检查”);
}
@凌驾
创建时受保护的void(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>
我有没有在代码结构上犯过错误?请告诉我怎么了。。。
谢谢大家!
注:这是日志05-24 10:35:45.471 2322-2322/com.google.android.gms.persistent E/ActivityThread:Service com.google.android.gms.autofill.Service.AutofillService已经泄露了IntentReceiver.google.android.gms.autofill.smsretriever。TracingSmsBroadcastReceiver@88dcee原来是在这里注册的。是否缺少对unregisterReceiver()的调用?
android.app.IntentReceiver泄漏:Service com.google.android.gms.autofill.Service.AutofillService已泄漏IntentReceiver com.google.android.gms.autofill.smsretriever。TracingSmsBroadcastReceiver@88dcee原来是在这里注册的。是否缺少对unregisterReceiver()的调用?
在android.app.LoadedApk$ReceiverDispatcher(LoadedApk.java:1588)
位于android.app.LoadedApk.getReceiveDispatcher(LoadedApk.java:1368)
位于android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1515)
在android.app.ContextImpl.registerReceiver(ContextImpl.java:1488)
位于android.app.ContextImpl.registerReceiver(ContextImpl.java:1476)
位于android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627)
位于android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627)
位于android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627)
位于android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627)
位于android.content.ContextWrapper.registerReceiver(ContextWrapper.java:627)
在lkl。(:com.google.android)。gms@200414022@20.04.14 (040700-294335909):5)
在ljc。(:com.google.android)。gms@200414022@20.04.14 (040700-294335909):1)
在kxb.a(:com.google.android)。gms@200414022@20.04.14 (040700-294335909):10)
在bzlo.a(:com.google.android)。gms@200414022@20.04.14 (040700-294335909):5)
在kxj.a(:com.google.android)。gms@200414022@20.04.14 (040700-294335909):2)
在bzlo.a(:com.google.android)。gms@200414022@20.04.14 (040700-294335909):5)
Button Sbutton = findViewById(R.id.submitButton);