Wrong textview is being updated - java

In the onCreate() method I am updating 2 TextViews to be blank. For some reason it is setting two other TextViews to be blank. I have followed my logic and can not see what is going wrong.
here is my onCreate() method.
import android.widget.TextView;
public class MainActivity extends Activity {
private int purpleDrinks; //number of purple drinks
private int greenDrinks; //number of green drinks
private int redDrinks; //number of red drinks
private int blueDrinks; //number of blue drinks
private TextView purpleCount; //number of purple drinks display
private TextView greenCount; //number of green drinks display
private TextView redCount; //number of red drinks display
private TextView blueCount; //number of blue drinks display
private TextView purchaseConfirm; //displays what the user bought
private TextView outOfStock; //lets the user know when the item is out of stock.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( savedInstanceState == null ) // the app just started running
{
purpleDrinks = 10;
greenDrinks = 10;
redDrinks = 10;
blueDrinks = 10;
} // end if
purpleCount = (TextView) findViewById(R.id.purpleCount);
greenCount = (TextView) findViewById(R.id.greenCount);
redCount = (TextView) findViewById(R.id.redCount);
blueCount = (TextView) findViewById(R.id.blueCount);
purchaseConfirm = (TextView) findViewById(R.id.purchaseConfirm);
outOfStock = (TextView) findViewById(R.id.outOfStock);
purchaseConfirm.setText("");
outOfStock.setText("");
purpleCount.setText(purpleDrinks + "");
greenCount.setText(greenDrinks + "");
redCount.setText(redDrinks + "");
blueCount.setText(blueDrinks + "");
}
here is the xml code: the ids are correct.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background= "#D5E3EA"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/greenCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_marginTop="26dp"
android:src="#drawable/greencan" />
<ImageButton
android:id="#+id/redCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/greenCann"
android:layout_marginRight="25dp"
android:src="#drawable/redcan" />
<ImageButton
android:id="#+id/purpleCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/redCann"
android:layout_marginRight="25dp"
android:src="#drawable/purplecan" />
<ImageButton
android:id="#+id/blueCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/purpleCann"
android:layout_marginRight="25dp"
android:src="#drawable/bluecan" />
<Button
android:id="#+id/restockButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="54dp"
android:text="#string/restock" />
<TextView
android:id="#+id/purpleCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/purpleCann"
android:layout_marginBottom="18dp"
android:layout_toLeftOf="#+id/purpleCann"
android:text="TextView" />
<TextView
android:id="#+id/redCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/redCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/redCann"
android:text="TextView" />
<TextView
android:id="#+id/greenCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/greenCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/greenCann"
android:text="TextView" />
<TextView
android:id="#+id/blueCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/blueCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/blueCann"
android:text="TextView" />
<TextView
android:id="#+id/outOfStock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/restockButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="TextView" />
<TextView
android:id="#+id/purchaseConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/outOfStock"
android:layout_alignBottom="#+id/outOfStock"
android:layout_alignLeft="#+id/outOfStock"
android:text="TextView" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/greenCount"
android:layout_toLeftOf="#+id/restockButton"
android:text="#string/greenSoda2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/redCount"
android:layout_alignBottom="#+id/redCount"
android:layout_alignLeft="#+id/textView5"
android:text="#string/redSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/purpleCount"
android:layout_alignBottom="#+id/purpleCount"
android:layout_alignLeft="#+id/textView6"
android:text="#string/purpleSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/blueCount"
android:layout_alignBottom="#+id/blueCount"
android:layout_alignLeft="#+id/textView7"
android:text="#string/blueSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
here is a screenshot of the app:
does anybody know what is going on?

Related

Why my android application has stopped?

My project won't open on my device, it say's "Unfortunately,myApp has stopped".
this is my manifest:
this is my activity java :
package bismillah.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Project2Activity extends Activity {
public static int a,b,c;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//coding start here!!
//input data
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
//button proses
Button button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Spinner bibit = (Spinner)findViewById(R.id.spinner1);
TextView biaya = (TextView)findViewById(R.id.textView6);
if(bibit.getSelectedItem().toString().equals("Cabai Rp.100")){
a=pekerja*(lama*upah)+(jumlah*100);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Tomat Rp.150")){
a=pekerja*(lama*upah)+(jumlah*150);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Timun Rp.200")){
a=pekerja*(lama*upah)+(jumlah*200);
biaya.setText("Biaya Rp." + a);
}
}
}
}
});
//button reset
Button button2=(Button)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView biaya = (TextView)findViewById(R.id.textView6);
EditText pek=(EditText)findViewById(R.id.editText1);
EditText lam=(EditText)findViewById(R.id.editText2);
EditText up=(EditText)findViewById(R.id.editText3);
EditText jum=(EditText)findViewById(R.id.editText4);
pek.setText("");
lam.setText("");
up.setText("");
jum.setText("");
biaya.setText("");
}
});
//button pindah activity
Button button3=(Button)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i =new Intent(getApplicationContext(),activity2.class);
startActivity(i);
}
});
}
}
and this is my main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="#string/hello"
android:textColor="#color/warna"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:entries="#array/list" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textColor="#color/warna2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
I tried your code and I had to delete some stuff from your XML file because it was creating some bugs. It might be related to me but here's your XML I was able to use:
But please do not change your XML file before trying the solution below. If it still doesn't work, you should consider the elements I removed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="hello"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
Next I created your Project2Activity java class and activity2 (pretty much empty since you gave no info about it).
So when trying to run your application you get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application.so/com.example.application.so.Project2Activity}: java.lang.NumberFormatException: For input string: ""
and it is at :
at com.example.application.so.Project2Activity.onCreate(Project2Activity.java:24)
now if we consider this lines 23 and 24:
String value=pek.getText().toString(); // line 23
final int pekerja=Integer.parseInt(value); // line 24
The error is that you are trying to read (line 23) your editText Value at the creation of the activity, at that time, your editText is still blank which means value will be equal to "" (empty string).
What you want to do instead is to put your lines from 22 to 33 (the ones below) in a button click event, I tried putting them in your button 1 click event and your app doesn't crash any more:
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
So to summarize, the problem is that you are extracting empty strings from your editTexts and trying to make integers out of them. Instead try making that all reading and integer extracting process on the click of a button where your really need these inputs. just like get the inputs and the final moment.
You should pay attention to your logcat (error messages), it is quite helpful.
What you can also do is to verify that your editTexts are not empty while trying to extract strings from them and raise exception or Toast if they are empty. Nothing too complicated just something like:
if (value.equals("")):
// editText is empty so do something
You can refer to this link for further understanding of how to know when an editText is empty.

How can I put frames in textviews to have the form of a table?

IMAGE-Something like that
I have begun to create an application that will show us the weather conditions of some cities.My problem is that I do not know how can I put frames in textviews to have the form of a table.If there are any solutions please help me.
My xml code
<RelativeLayout 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.junior_marg.current_weather.Main12Activity">
<TextView
android:id="#+id/textview12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/c"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview12"
android:layout_marginLeft="20dp"
android:layout_marginStart="92dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/d"
android:layout_below="#+id/textview12"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview14"
android:layout_below="#+id/textview13"
android:layout_marginLeft="50dp"
android:layout_marginStart="133dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/h2"
android:layout_below="#+id/textview14"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview16"
android:layout_marginLeft="20dp"
android:layout_marginStart="77dp"
android:layout_marginTop="63dp" />
<TextView
android:id="#+id/textview18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/i2"
android:layout_below="#+id/textview16"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview18"
android:layout_marginLeft="50dp"
android:layout_marginStart="130dp"
android:layout_marginTop="90dp" />
<TextView
android:id="#+id/textview20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/j2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview18"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview20"
android:layout_below="#+id/textview19"
android:layout_marginLeft="10dp"
android:layout_marginStart="104dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/k2"
android:layout_below="#+id/textview20"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview22"
android:layout_below="#+id/textview21"
android:layout_marginLeft="50dp"
android:layout_marginStart="63dp"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/textview24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/l2"
android:layout_below="#+id/textview22"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview24"
android:layout_below="#+id/textview23"
android:layout_marginLeft="20dp"
android:layout_marginStart="94dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/m2"
android:layout_below="#+id/textview24"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview26"
android:layout_below="#+id/textview25"
android:layout_marginLeft="50dp"
android:layout_marginStart="36dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/n2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview26"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview28"
android:layout_below="#+id/textview27"
android:layout_marginLeft="20dp"
android:layout_marginStart="75dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/o2"
android:layout_below="#+id/textview28"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview30"
android:layout_below="#+id/textview29"
android:layout_marginLeft="50dp"
android:layout_marginStart="85dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/p2"
android:layout_below="#+id/textview30"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview32"
android:layout_below="#+id/textview31"
android:layout_marginLeft="20dp"
android:layout_marginStart="73dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/q2"
android:layout_below="#+id/textview32"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview34"
android:layout_below="#+id/textview33"
android:layout_marginLeft="50dp"
android:layout_marginStart="24dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/r2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview34"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview36"
android:layout_below="#+id/textview35"
android:layout_marginLeft="10dp"
android:layout_marginStart="52dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/s2"
android:layout_below="#+id/textview36"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview39"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview38"
android:layout_below="#+id/textview37"
android:layout_marginLeft="50dp"
android:layout_marginStart="45dp"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/textview40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/t2"
android:layout_below="#+id/textview38"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview40"
android:layout_below="#+id/textview39"
android:layout_marginLeft="20dp"
android:layout_marginStart="122dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/u2"
android:layout_below="#+id/textview40"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textview43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview42"
android:layout_below="#+id/textview41"
android:layout_marginLeft="50dp"
android:layout_marginStart="151dp"
android:layout_marginTop="10dp" />
My java code
TextView mTextView;
TextView mTextView2;
TextView mTextView3;
TextView mTextView4;
TextView mTextView5;
TextView mTextView6;
TextView mTextView7;
TextView mTextView8;
TextView mTextView9;
TextView mTextView10;
TextView mTextView11;
TextView mTextView12;
TextView mTextView13;
TextView mTextView14;
TextView mTextView15;
TextView mTextView16;
String therm;
String ygrasia;
String simdrosou;
String anemos;
String varom;
String simerinosy;
String ragd;
String trexkak;
String miniaiosy;
String ethsiosy;
String aisthpsix;
String yperithriakt;
String deiktisdis;
String hliakiakt;
String anatoli;
String disi;
String url;
Document doc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main12);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mTextView = (TextView) findViewById(R.id.textview13);
mTextView2 = (TextView) findViewById(R.id.textview15);
mTextView3 = (TextView) findViewById(R.id.textview17);
mTextView4 = (TextView) findViewById(R.id.textview19);
mTextView5 = (TextView) findViewById(R.id.textview21);
mTextView6 = (TextView) findViewById(R.id.textview23);
mTextView7 = (TextView) findViewById(R.id.textview25);
mTextView8 = (TextView) findViewById(R.id.textview27);
mTextView9 = (TextView) findViewById(R.id.textview29);
mTextView10 = (TextView) findViewById(R.id.textview31);
mTextView11 = (TextView) findViewById(R.id.textview33);
mTextView12 = (TextView) findViewById(R.id.textview35);
mTextView13 = (TextView) findViewById(R.id.textview37);
mTextView14 = (TextView) findViewById(R.id.textview39);
mTextView15 = (TextView) findViewById(R.id.textview41);
mTextView16 = (TextView) findViewById(R.id.textview43);
url = "http://penteli.meteo.gr/stations/trikala/";
doc = null;
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
Element table = doc.select("table").get(0);
Elements rows = table.select("tr");
Element row = rows.get(5);
Elements cols = row.select("td");
therm = cols.get(1).text() ;
mTextView.setText(therm);
Element row_2 = rows.get(6);
Elements cols_2 = row_2.select("td");
ygrasia = cols_2.get(1).text() ;
mTextView2.setText(ygrasia);
Element row_3 = rows.get(7);
Elements cols_3 = row_3.select("td");
simdrosou = cols_3.get(1).text() ;
mTextView3.setText(simdrosou);
Element row_4 = rows.get(8);
Elements cols_4 = row_4.select("td");
anemos = cols_4.get(1).text() ;
mTextView4.setText(anemos);
Element row_5 = rows.get(9);
Elements cols_5 = row_5.select("td");
varom = cols_5.get(1).text() ;
mTextView5.setText(varom);
Element row_6 = rows.get(10);
Elements cols_6 = row_6.select("td");
simerinosy = cols_6.get(1).text() ;
mTextView6.setText(simerinosy);
Element row_7 = rows.get(11);
Elements cols_7 = row_7.select("td");
ragd = cols_7.get(1).text() ;
mTextView7.setText(ragd);
Element row_8 = rows.get(12);
Elements cols_8 = row_8.select("td");
trexkak = cols_8.get(1).text() ;
mTextView8.setText(trexkak);
Element row_9 = rows.get(13);
Elements cols_9 = row_9.select("td");
miniaiosy = cols_9.get(1).text() ;
mTextView9.setText(miniaiosy);
Element row_10 = rows.get(14);
Elements cols_10 = row_10.select("td");
ethsiosy = cols_10.get(1).text() ;
mTextView10.setText(ethsiosy);
Element row_11 = rows.get(15);
Elements cols_11 = row_11.select("td");
aisthpsix = cols_11.get(1).text() ;
mTextView11.setText(aisthpsix);
Element row_12 = rows.get(16);
Elements cols_12 = row_12.select("td");
yperithriakt = cols_12.get(1).text() ;
mTextView12.setText(yperithriakt);
Element row_13 = rows.get(17);
Elements cols_13 = row_13.select("td");
deiktisdis = cols_13.get(1).text() ;
mTextView13.setText(deiktisdis);
Element row_14 = rows.get(18);
Elements cols_14 = row_14.select("td");
hliakiakt = cols_14.get(1).text() ;
mTextView14.setText(hliakiakt);
Element row_15 = rows.get(19);
Elements cols_15 = row_15.select("td");
anatoli = cols_15.get(1).text() ;
mTextView15.setText(anatoli);
Element row_16 = rows.get(20);
Elements cols_16 = row_16.select("td");
disi = cols_16.get(1).text() ;
mTextView16.setText(disi);
}
}
To get something that resembles a table, I suggest using a TableLayout.
Alternatively, you might try the new FlexboxLayout.

Crash's trying to find view

I have a slight problem that I have been stuck on. I have narrowed it down and it seems that my application is crashing on create when i am trying to find a text view. I do not understand why, maybe i have been looking at this to long and dont see the obvious.
Any help would be appreciated.
Thanks.
public class FinalActivity extends Activity {
EditText fullName;
EditText serialNumber;
EditText numberOfTicket;
EditText fullCost;
EditText discount;
EditText totalCostOfTickets;
EditText dinnerYesNo;
EditText numberForDinner;
EditText dinnerCost;
EditText vatTv;
EditText totalCost;
SharedPreferences thePrefs;// Shared preferences variable
SharedPreferences.Editor editor;// New editor to add to shared preferences
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
thePrefs = getSharedPreferences(LoginActivity.FIRSTTIMEPREFS, 0);
editor = thePrefs.edit();
fullName = (EditText)findViewById(R.id.personsNameTv);
/*serialNumber = (EditText)findViewById(R.id.serialNumberTv);
numberOfTicket = (EditText)findViewById(R.id.numberOfTicketTv);
fullCost = (EditText)findViewById(R.id.fullCostBeforeTv);
discount = (EditText)findViewById(R.id.discountAmountTV);
totalCostOfTickets = (EditText)findViewById(R.id.totalTicketCostEt);
dinnerYesNo = (EditText)findViewById(R.id.dinnerAvailedTv);
numberForDinner = (EditText)findViewById(R.id.numberForDinnerTv);
dinnerCost = (EditText)findViewById(R.id.dinnerCostTv);
vatTv = (EditText)findViewById(R.id.vatTv);
totalCost = (EditText)findViewById(R.id.totalCostTv);*/
//fullName.setText (thePrefs.getString(PersonalInfoActivity.FULLNAME, ""));
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#404040"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/mainTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="#string/ticketSummary"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/promptTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:text="#string/confirmCorrectDetails"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<ScrollView
android:id="#+id/containerLayout"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#drawable/redborder" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingBottom="30dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingTop="15dp" >
<TextView
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/serialNumberTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/personsNameTv"
android:layout_marginTop="10dp"
android:text="#string/serialNumber"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/numberOfTicketTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/serialNumberTv"
android:layout_marginTop="10dp"
android:text="#string/numberOfTickets"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/fullCostBeforeTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberOfTicketTv"
android:layout_marginTop="10dp"
android:text="#string/fullCostBeforeDiscount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/discountAmountTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fullCostBeforeTv"
android:layout_marginTop="10dp"
android:text="#string/amountOfDisount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/totalTicketCostEt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/discountAmountTV"
android:layout_marginTop="10dp"
android:text="#string/totalTicketCost"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/dinnerAvailedTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/totalTicketCostEt"
android:layout_marginTop="10dp"
android:text="#string/dinnerAvailed"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/numberForDinnerTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dinnerAvailedTv"
android:layout_marginTop="10dp"
android:text="#string/numberForDinner"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/dinnerCostTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberForDinnerTv"
android:layout_marginTop="10dp"
android:text="#string/dinnerCost"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/vatTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dinnerCostTv"
android:layout_marginTop="10dp"
android:text="#string/vat"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/totalCostTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vatTv"
android:layout_marginTop="10dp"
android:text="#string/totalCost"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</RelativeLayout>
</ScrollView>
<Button
android:id="#+id/confirmBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/containerLayout"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#drawable/buttonshape"
android:onClick="confirmTicketOptions"
android:text="#string/confirm"
android:textStyle="bold" />
</LinearLayout>
There are two possible things possible. Either you want all Textviews or all EditText.
If you want all Textview then replace all your EditTextin java file with Textview.
Textview fullName;
fullName = (Textview )findViewById(R.id.personsNameTv);
If you want all EditText then replace all Textview in xml with EditText.
<EditText
android:id="#+id/personsNameTv"
.../>
In XML you are using Text-view and in java file you define it Edit-text
<TextView
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
Replace with
<EditText
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />

Inflated items are gone when device orientation changes

Hi i'm using eclipse for deveoping this android app. I'm inflating a layout using inflator and everything works fine.. I'm adding those inflated items one by one to another layout, so when the device's orientation is changed, all those inflated items are gone. The application seems like it has been restarted. But some values are still remaining.. Please help..
public void addNewItem() {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = li.inflate(R.layout.activity_order_items, null);
orderItemFrameView.add(item);
itemsLayout.addView(item);
setAdditionalFonts(item);
}
private void setAdditionalFonts(View view) {
TextView lblNameTak = (TextView)view.findViewById(R.id.tak_itemNo);
AutoCompleteTextView txtNameTak = (AutoCompleteTextView)view.findViewById(R.id.tak_itemName);
ImageView barScan = (ImageView)view.findViewById(R.id.barcode_scan);
TextView lblRate = (TextView)view.findViewById(R.id.tak_lblRate);
TextView lblQty = (TextView)view.findViewById(R.id.tak_lblQty);
TextView lblTotal = (TextView)view.findViewById(R.id.tak_lblTotal);
TextView rate = (TextView)view.findViewById(R.id.tak_Rate);
EditText2 qty = (EditText2)view.findViewById(R.id.tak_Qty);
TextView total = (TextView)view.findViewById(R.id.tak_Total);
qty.totTextView = total;
qty.rateTextView = rate;
qty.orderTake = this;
qty.index = orderItemFrameView.size() - 1;
qty.sqlDb = SqlDb;
qty.itemName = txtNameTak;
setupBarcodeScan(barScan);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/ubuntu-l.ttf");
Typeface tf2 = Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf");
Typeface tf3 = Typeface.createFromAsset(getAssets(), "fonts/ubu-r.ttf");
Typeface tf4 = Typeface.createFromAsset(getAssets(), "fonts/tahoma.ttf");
lblNameTak.setTypeface(tf3);
txtNameTak.setTypeface(tf2);
lblRate.setTypeface(tf2);
lblQty.setTypeface(tf2);
lblTotal.setTypeface(tf2);
lblNameTak.setText("Item " + (qty.index + 1));
DbAdapterItem dbItem;
TextAdapterItem txtItem;
dbItem = new DbAdapterItem(this, SqlDb);
txtItem = new TextAdapterItem(dbItem, this, qty);
txtNameTak.setAdapter(txtItem);
txtNameTak.setOnItemClickListener(txtItem);
txtNameTak.requestFocus();
}
all these items created by addNewItem() are vanished...
here is the layout from which i'm inflating...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom ="22dp"
tools:context=".OrderItems" >
<FrameLayout
android:layout_width="match_parent"
android:background= "#888888"
android:layout_height="98dp" >
<TextView
android:id="#+id/tak_itemNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="2.7dp"
android:text="Item 1"
android:layout_gravity="right"
android:textColor="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="22dp"
android:orientation="vertical"
android:background= "#FFFFFF"
android:layout_height="75dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/tak_itemName"
android:imeOptions="actionUnspecified"
android:imeActionLabel="search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:layout_weight="1.06"
android:completionThreshold="1"
android:ems="10"
android:hint="Item Name / Barcode"
android:inputType="textCapWords"
android:singleLine="true"
android:textColorHint="#DDDDDD" />
<ImageView
android:id="#+id/barcode_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="6dp"
android:src="#android:drawable/ic_menu_camera" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
>
<TextView
android:id="#+id/tak_lblRate"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Rate" />
<TextView
android:id="#+id/tak_Rate"
android:layout_width="match_parent"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TextView
android:id="#+id/tak_lblQty"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Qty" />
<com.sayka.ordergadget.EditText2
android:id="#+id/tak_Qty"
android:layout_width="match_parent"
android:layout_marginBottom="-4dp"
android:layout_marginTop="4dp"
android:inputType="numberDecimal"
android:imeOptions="actionNext"
android:layout_marginLeft="12dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:text="0.0" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
>
<TextView
android:id="#+id/tak_lblTotal"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Total" />
<TextView
android:id="#+id/tak_Total"
android:layout_width="match_parent"
android:layout_gravity="right"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
It's normal behaviour and you are correct. the activity is recreated from scratch on rotation.
If you want something to persist. store it during onSaveInstanceState in the provided bundle.
then during onCreate check to see if the bundle is null, if it isn't, pull your data out and add the view elements again.
Alternatively you can fudge it and state in the manifest that your activity will handle orientation/configuration changes.

Force close error in Android app debugging

I get no compilation errors or even significant warnings, yet when I run the program, it force closes and the log tells me there's some kind of error on a line. It succeeded once, but I don't know how or when.
onClick method:
public TextView equation;
public TextView answer11;
public TextView answer12;
public TextView or1;
public TextView or2;
public void SolveOnClick(View view){
EditText a = (EditText)findViewById(R.id.aValue);
EditText b = (EditText)findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)findViewById(R.id.cValue);
equation = (TextView)findViewById(R.id.equationText);
answer11 = (TextView)findViewById(R.id.answer1);
answer12 = (TextView)findViewById(R.id.answer2);
or1 = (TextView)findViewById(R.id.or1);
or2 = (TextView)findViewById(R.id.or2);
double A = Double.parseDouble(a.getText().toString());
double B = Double.parseDouble(b.getText().toString());
double C = Double.parseDouble(c.getText().toString());
Formula(A,B,C);
}
My main graphical layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="SolveOnClick"
android:text="#string/solveButtonText" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/quadFormText"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/textView4"
android:text="#string/A"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/aValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignTop="#+id/textView1"
android:layout_marginLeft="15dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/bValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aValue"
android:layout_below="#+id/aValue"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="#+id/cValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/bValue"
android:layout_below="#+id/bValue"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/answer1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/equals1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer1Text"
android:layout_below="#+id/answer1Text"
android:layout_marginTop="88dp"
android:text="#string/equals2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/answer1Text"
android:layout_toRightOf="#+id/answer1Text"
android:text="#string/answer11"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/or1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer1Text"
android:layout_below="#+id/answer1Text"
android:layout_marginTop="25dp"
android:text="#string/or1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/answer2Text"
android:layout_toRightOf="#+id/answer1Text"
android:text="#string/answer12"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/or2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer2Text"
android:layout_below="#+id/answer2Text"
android:layout_marginTop="24dp"
android:text="#string/or2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/equationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/cValue"
android:layout_marginTop="14dp"
android:text="#string/equation"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cValue"
android:layout_toLeftOf="#+id/textView4"
android:text="#string/C"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/bValue"
android:layout_alignLeft="#+id/textView3"
android:text="#string/B"
android:textAppearance="?android:attr/textAppearanceLarge" />
onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
One of the logs when the program fails:
04-26 20:39:33.174: E/AndroidRuntime(16092): Caused by: java.lang.ClassCastException: android.widget.TextView
04-26 20:39:33.174: E/AndroidRuntime(16092): at calc.ulater.QuadCalcActivity.SolveOnClick(QuadCalcActivity.java:31)
I believe this has something to do with the decimal text boxes. The program worked well before I made them only take decimal numbers, but it also worked a time or two after. I'd rather not show the rest of my code, unless you guys are sure the problem lies there.
If this is the line that's screwing up:
EditText b = (EditText)findViewById(R.id.bValue);
Then its saying you're trying to cast a TextView to an EditText.
What's line 31? That one?
EditText a = (EditText)findViewById(R.id.aValue);
EditText b = (EditText)findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)findViewById(R.id.cValue);
equation = (TextView)findViewById(R.id.equationText);
answer11 = (TextView)findViewById(R.id.answer1);
answer12 = (TextView)findViewById(R.id.answer2);
or1 = (TextView)findViewById(R.id.or1);
or2 = (TextView)findViewById(R.id.or2);
all the initialization must be done in onCreate() and not in the constructor
Updated::
EditText a = (EditText)view.findViewById(R.id.aValue);
EditText b = (EditText)view.findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)view.findViewById(R.id.cValue);
equation = (TextView)view.findViewById(R.id.equationText);
answer11 = (TextView)view.findViewById(R.id.answer1);
answer12 = (TextView)view.findViewById(R.id.answer2);
or1 = (TextView)view.findViewById(R.id.or1);
or2 = (TextView)view.findViewById(R.id.or2);
if view is your parent view

Categories