2014-11-08 173 views
-4

這裏是我的代碼,我需要的是使用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

回答

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

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

+1

你能解釋你的代碼如何幫助不初始化t2嗎? – 2014-11-08 10:45:03

+0

@HareshChhelana我已編輯我的答案,請先檢查 – 2014-11-08 10:46:08

1

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

t1 = (EditText) findViewById(R.id.editText1); 
t2 = (EditText) findViewById(R.id.editText2); 
相關問題