-1

My project won't open on my device, it say's "Unfortunately,myApp has stopped".

this is my manifest:

enter image description here

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>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

2

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.

Rayhane Mama
  • 2,374
  • 11
  • 20