简体   繁体   English

应用程序不断崩溃

[英]Application keeps Crashing

My application keeps on crashing, and I cannot figure out why. 我的应用程序一直在崩溃,我无法弄清楚原因。 I am suspecting that there is something wrong with the getReactants() method because the button is working just fine and can display any other text I put in the beq.setText(). 我怀疑getReactants()方法有问题,因为按钮工作正常,可以显示我放在beq.setText()中的任何其他文本。

There are no errors in the logcat, the threads are simply suspended and my device says that the application is not responding, and says I can either wait or kill the app. logcat中没有错误,线程被暂停,我的设备说应用程序没有响应,并说我可以等待或杀死应用程序。

Here is my code. 这是我的代码。

Java Java的

package me.finalproject.com.apchemchemolyapp;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.io.Serializable;


/**
 * Created by Shishir on 6/9/2016.
 */
public class stoich_fragment extends Fragment implements View.OnClickListener, Serializable
{
    View rootview;
    int i = 0;
    ArrayList<Integer> arr = new ArrayList<>();
    ArrayList<String> elements = new ArrayList<>();
    boolean getElements = true;
    String s1;
    String element = "";
    EditText reactants;
    TextView beq;
    Button go;
    int temp;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        rootview = inflater.inflate(R.layout.stoich_layout, container, false);
        reactants = (EditText) rootview.findViewById(R.id.reactants);
        go = (Button) rootview.findViewById(R.id.button);
        go.setOnClickListener(this);
        return rootview;
    }
    public void onClick(View v)
    {
        getReactants(s1);
        beq = (TextView) rootview.findViewById(R.id.balanced_equation);
        beq.setText(s1);
    }
    public void getReactants(String s)
    {
        String reactant = reactants.getText().toString();
        //saying that reactants is null even after it went through the onCreateView method
        String re = reactant.replaceAll("\\s+","");
        while(getElements)
        {
            String let = re.substring(i, i+1);
            if(let.compareTo(let.toLowerCase()) > 0)
            {
                element += let;
                if(i == re.length()-1 || i == re.length())
                {
                    elements.add(element);
                    if(re.substring(re.length()-1).equals("2")||re.substring(re.length()-1).equals("3")||re.substring(re.length()-1).equals("4")||re.substring(re.length()-1).equals("5")||re.substring(re.length()-1).equals("6")||re.substring(re.length()-1).equals("7")||re.substring(re.length()-1).equals("8")||re.substring(re.length()-1).equals("9"))
                    {
                        arr.add(Integer.parseInt(re.substring(re.length()-1)));
                    }
                    else
                    {
                        arr.add(1);
                        elements.add(re.substring(re.length()-1));
                        arr.add(1);
                    }
                    getElements = false;
                }
                else if(re.substring(i+1, i+2).compareTo(re.substring(i+1, i+2).toLowerCase()) != 0)
                {
                    if(!re.substring(i+1,i+2).equals("2")||!re.substring(i+1,i+2).equals("3")||!re.substring(i+1,i+2).equals("4")||!re.substring(i+1,i+2).equals("5")||!re.substring(i+1,i+2).equals("6")||!re.substring(i+1,i+2).equals("7")||!re.substring(i+1,i+2).equals("8")||!re.substring(i+1,i+2).equals("9"))
                    {
                        temp = 1;
                        arr.add(temp);
                    }
                }
            }
            else if(let.compareTo(let.toLowerCase()) == 0)
            {
                element += let;
                if(i == re.length()-1 || i == re.length())
                {
                    elements.add(element);
                    if(re.substring(re.length()-1).equals("2")||re.substring(re.length()-1).equals("3")||re.substring(re.length()-1).equals("4")||re.substring(re.length()-1).equals("5")||re.substring(re.length()-1).equals("6")||re.substring(re.length()-1).equals("7")||re.substring(re.length()-1).equals("8")||re.substring(re.length()-1).equals("9"))
                    {
                        arr.add(Integer.parseInt(re.substring(re.length()-1)));
                    }
                    else
                    {
                        arr.add(1);
                        elements.add(re.substring(re.length()-1));
                        arr.add(1);
                    }
                    getElements = false;
                }
                else if(!re.substring(i+1,i+2).equals("2")||re.substring(i+1,i+2).equals("3")||re.substring(i+1,i+2).equals("4")||re.substring(i+1,i+2).equals("5")||re.substring(i+1,i+2).equals("6")||re.substring(i+1,i+2).equals("7")||re.substring(i+1,i+2).equals("8")||re.substring(i+1,i+2).equals("9"))
                {
                    temp = 1;
                    arr.add(temp);
                }
            }
            else if (let.equals("2")||let.equals("3")||let.equals("4")||let.equals("5")||let.equals("6")||let.equals("7")||let.equals("8")||let.equals("9"))
            {
                temp = Integer.parseInt(let);
                arr.add(temp);
                elements.add(element);
                element = "";
            }
            i++;
            if(i == re.length()+1)
            {
                getElements = false;
            }
        }
        // displays the elements isolated on the reactant side
        // to test to make sure my logic works
        for(int a = 0; a<elements.size(); a++)
        {
            s += (elements.get(a) + " : " + arr.get(a) + "\n");
        }
    }
}

XML XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/reactants"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="95dp"
        android:textSize="20sp"
        android:inputType="text" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/products"
        android:layout_alignBottom="@+id/reactants"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="20sp"
        android:inputType="text" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/balanced_equation"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="30sp" />

<!--should make text bold and black-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/beq"
        android:id="@+id/title"
        android:textSize="35sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textStyle = "bold"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="45dp" />

</RelativeLayout>

This messages means your application is doing too much work on it's main thread (UI Tread). 此消息表示您的应用程序在其主线程(UI Tread)上执行了太多工作。

I suggest to put getReactants() in an AsyncTask and show the result when all proccesses finished without blocking the main thread. 我建议将getReactants()放在AsyncTask并在所有proccesses完成时显示结果而不阻塞主线程。

Looking at your code, it seems that you probably have an infinite loop. 看看你的代码,似乎你可能有一个无限循环。 Try to set your getElements flag to false at the end of your while loop to make sure it will be executed. 尝试在while循环结束时将getElements标志设置为false ,以确保它将被执行。 Must be something like: 必须是这样的:

    while(getElements)
    {
        String let = re.substring(i, i+1);
        if(let.compareTo(let.toLowerCase()) > 0)
        {
            element += let;
            if(re.substring(i+1, i+2).compareTo(re.substring(i+1, i+2).toLowerCase()) != 0)
            {
                if(!re.substring(i+1,i+2).equals("2")||re.substring(i+1,i+2).equals("3")||re.substring(i+1,i+2).equals("4")||re.substring(i+1,i+2).equals("5")||re.substring(i+1,i+2).equals("6")||re.substring(i+1,i+2).equals("7")||re.substring(i+1,i+2).equals("8")||re.substring(i+1,i+2).equals("9"))
                {
                    temp = 1;
                    arr.add(temp);
                }
            }
            i++;
        }
        else if(let.compareTo(let.toLowerCase()) == 0)
        {
            element += let;
            if(!re.substring(i+1,i+2).equals("2")||re.substring(i+1,i+2).equals("3")||re.substring(i+1,i+2).equals("4")||re.substring(i+1,i+2).equals("5")||re.substring(i+1,i+2).equals("6")||re.substring(i+1,i+2).equals("7")||re.substring(i+1,i+2).equals("8")||re.substring(i+1,i+2).equals("9"))
            {
                temp = 1;
                arr.add(temp);
            }
            i++;
        }
        else if (let.equals("2")||let.equals("3")||let.equals("4")||let.equals("5")||let.equals("6")||let.equals("7")||let.equals("8")||let.equals("9"))
        {
            temp = Integer.parseInt(let);
            arr.add(temp);
            elements.add(element);
            element = "";
        }

        if(i == re.length())
        {
            getElements = false;
        }
        // must have an else statement here or else you will have an infinite loop if your condition is always false.

    }

You are not incrementing i in some cases, you should have to move i++ to the end of your while to avoid an infinite loop 在某些情况下,你不是在增加i,你应该将i ++移动到你的结束,以避免无限循环

i has to be incremented in all cases, consider something like this: 我必须在所有情况下增加,考虑这样的事情:

if () {
`enter code here`
}
else if () {
`enter code here`
}
else if () {
`enter code here`
}
i++;
if(i == re.length()) {
    getElements = false;
}

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

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