应用程序不断崩溃

时间:2016-06-27 02:01:58

标签: java android crash

我的应用程序一直在崩溃,我无法弄清楚原因。我怀疑getReactants()方法有问题,因为按钮工作正常,可以显示我放在beq.setText()中的任何其他文本。

logcat中没有错误,线程被暂停,我的设备说应用程序没有响应,并说我可以等待或杀死应用程序。

这是我的代码。

爪哇

public List<BDO_Enquiry> GetEnquiryList()
{
    List<BDO_Enquiry> retList = new List<BDO_Enquiry>();

    try
    {
        using (var context = new BDODevelopmentEntities())
        {
            try
            {
                var query = context.BDO_Enquiry.Include("Child");
                query = query.Include("AnotherChild");
                retList = query.ToList();
            }
            catch (Exception ex) { string g = ""; }
        }
    } catch(Exception ex) { string h = ""; }

    return retList;
}

XML

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");
        }
    }
}

3 个答案:

答案 0 :(得分:0)

此消息表示您的应用程序在其主要线程(UI Tread)上做了太多工作。

我建议将getReactants()放在AsyncTask中,并在所有过程完成时显示结果,而不会阻止主线程。

答案 1 :(得分:0)

看看你的代码,似乎你可能有一个无限循环。尝试在while循环结束时将getElements标记设置为false,以确保它将被执行。必须是这样的:

    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.

    }

答案 2 :(得分:0)

在某些情况下,你不是在递增i,你应该将i ++移动到你的结束,以避免无限循环

我必须在所有情况下增加,考虑这样的事情:

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