不幸的是,myapp已停止工作

不幸的是,myapp已停止工作

问题描述:

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

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

't2'为空,你永远不会初始化它。 – 2014-11-08 10:41:28

+0

请始终包含发生错误时的堆栈跟踪。 – EWit 2014-11-08 10:47:18

试图改变

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); 
+0

希望这可以帮助你! – 2014-11-08 10:44:15

+1

你能解释你的代码如何帮助不初始化t2吗? – 2014-11-08 10:45:03

+0

@HareshChhelana我已编辑我的答案,请先检查 – 2014-11-08 10:46:08

你忘了初始化T2变量。请在下面添加行:

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