我的應用程序不斷崩潰了,我想不通爲什麼。我懷疑getReactants()方法有問題,因爲按鈕工作正常,並且可以顯示我放入beq.setText()中的任何其他文本。應用不斷崩潰
有在logcat中沒有錯誤,線程只是暫停,我的設備說,應用程序沒有響應,並說我可以等待或終止該應用。
這是我的代碼。
的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 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>
呃這是一種不可能的logcat不顯示任何錯誤。你可以通過在你的方法的任何地方添加一個日誌行來檢查你的logcat是否正在工作? – Razgriz
在getReactants()的while塊中記錄一些跟蹤消息或設置斷點。可能有一個無限循環。 – PWC
@Razgriz logcat的工作就好了 – vdopp