不幸的是,myapp已停止工作了

时间:2014-11-08 10:40:40

标签: java android xml

这是我的代码和我需要的东西是使用EditText作为我的输出,我没有错误但是在加载主要活动之后它说不幸的是myapp已经停止工作。 这是我的 MainActivity.java 代码::

    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代码:

<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>

2 个答案:

答案 0 :(得分:1)

您忘记初始化 t2 变量。请添加以下行:

t1 = (EditText) findViewById(R.id.editText1);  
t2 = (EditText) findViewById(R.id.editText2);  

答案 1 :(得分:0)

尝试更改

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);
    }
}

<强>经

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);
    }
}

并添加

t1 = (EditText) findViewById(R.id.editText1);  
t2 = (EditText) findViewById(R.id.editText2);  
相关问题