-4

Here is my code and something i am in need is to use EditText as my output,i have no errors but after the main activity is loaded it say unfortunately myapp has stopped working. Here is my MainActivity.java code::

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener{
    Button b1,b2;
    EditText t1,t2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(this);
        t1 = (EditText) findViewById(R.id.editText2);   
    }

    public void onClick(View v) {
        float b;
        if(v.getId()==R.id.button1){
            float a = Float.valueOf(t1.getText().toString()); 
            b=a*1024;
            t2.setText(" "+b,TextView.BufferType.EDITABLE);
        }
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

xml code:

<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"
tools:context="com.techbolt.byteconverter.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:text="@string/hello_world" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:hint="@string/bit" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:text="@string/button" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="@string/bytes" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:hint="@string/bytes" />

</RelativeLayout>
kgandroid
  • 5,507
  • 5
  • 39
  • 69
Amir parkar
  • 60
  • 1
  • 6

2 Answers2

1

You forgot to initialize your t2 variable. Please add below line:

t1 = (EditText) findViewById(R.id.editText1);  
t2 = (EditText) findViewById(R.id.editText2);  
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
0

Try to change

public void onClick(View v) {
    float b;
    if(v.getId()==R.id.button1){
        float a = Float.valueOf(t1.getText().toString()); 
        b=a*1024;
        t2.setText(" "+b,TextView.BufferType.EDITABLE);
    }
}

Via

public void onClick(View v) {
    double b;
    if(v.getId()==R.id.button1){
        double e1 = Double.parseDouble(t1.getText().toString());

        b=a*1024;
        t2.setText(" "+String.valueOf(b),TextView.BufferType.EDITABLE);
    }
}

and add

t1 = (EditText) findViewById(R.id.editText1);  
t2 = (EditText) findViewById(R.id.editText2);  
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28