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().
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.
Here is my code.
Java
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(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++;
if(i == re.length())
{
getElements = false;
}
}
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++;
if(i == re.length())
{
getElements = false;
}
}
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;
}
}
}
// 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>
Aucun commentaire:
Enregistrer un commentaire