2014-03-19 55 views
-2

當我點擊添加按鈕而沒有在edittext上進行任何操作時,不幸的是計算器已經停止。Android中的簡單計算器應用程序

 package com.example.calculator; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Calci extends Activity { 
TextView t1; 
EditText e1,e2; 
Button add,sub,mul,div; 
Context c;; 

String b,a; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_calci); 
     e1=(EditText) findViewById(R.id.EditText01); 
     e2=(EditText) findViewById(R.id.EditText02); 
     add=(Button) findViewById(R.id.add); 
     sub=(Button) findViewById(R.id.sub); 
     mul=(Button) findViewById(R.id.mul); 
     div=(Button) findViewById(R.id.div); 
     t1=(TextView) findViewById(R.id.textView1); 
     a=e1.getText().toString(); 
     b=e2.getText().toString(); 

    } 

    public void doSomething(View v){ 
     if(v.getId()==R.id.add){ 
      if (a==null & b==null){ 
       Toast.makeText(c,"PLEASE ENTER SOMETHING", Toast.LENGTH_LONG).show(); 
      } 
      else{ 



      int result=Integer.parseInt(a) + Integer.parseInt(b); 
      t1.setText(Integer.toString(result)); 
      } 
     } 

    } 

} 

當我點擊添加按鈕,不幸的是計算器已經停止。

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" 
    tools:context=".Calci" > 

    <EditText 
     android:id="@+id/EditText01" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:ems="10" 
     android:inputType="number" /> 

    <EditText 
     android:id="@+id/EditText02" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/EditText01" 
     android:ems="10" 
     android:inputType="number" /> 

    <Button 
     android:id="@+id/add" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/EditText01" 
     android:layout_marginTop="20dp" 
     android:text="ADD" 
     android:onClick="doSomething"/> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button4" 
     android:layout_marginTop="44dp" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/sub" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/add" 
     android:onClick="doSomething" 
     android:text="SUBTRACT" /> 

    <Button 
     android:id="@+id/mul" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/sub" 
     android:onClick="doSomething" 
     android:text="MULTIPLY" /> 

    <Button 
     android:id="@+id/div" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/mul" 
     android:onClick="doSomething" 
     android:text="DIVIDE" /> 

</RelativeLayout> 

當我點擊添加按鈕,不幸的是計算器已停止。\ shuold我該怎麼辦現在

+0

請上傳錯誤日誌中有問題。 – Kedarnath

+1

添加你的xml和logcat .. –

+1

你已經初始化除textview t1以外的所有視圖。這就是我可以看到在你的代碼 –

回答

2

首先初始化 t1=(TextView) findViewById(R.id.textView1);

你可以把驗證的按鈕點擊收聽空管檢查furthe過程之前一樣

if(!(t1.getText.toString().equals("")))顯示快訊

1

我認爲問題是,你沒有申報文本視圖,嘗試像下面那樣聲明它,然後嘗試。

t1=(TextView) findViewById(R.id.textView1); 

編輯

if (e1.getText.toString().trim() == "" && e2.getText.toString().trim() == "") 
{ 
// alert 
} 
else 
{ 
// do your code 
} 

所以你的最終代碼會

public void doSomething(View v){ 
      if(v.getId()==R.id.add){ 
       a=e1.getText().toString(); 
       b=e2.getText().toString(); 

       if (a == "" && b == "") 
       { 
        // alert 
       } 
       else 
       { 
       int result=Integer.parseInt(a) + Integer.parseInt(b); 
       t1.setText(Integer.toString(result)); 
       } 
      } 

     } 
+0

已經提出的意見:) –

+0

我得到它thankzz – Anuj

+0

我想如果TAT用戶添加按鈕單擊而無需在文本框中輸入數量警報應該出現 – Anuj

0

起初,你有錯在你的功能 - 你應該叫e1.setText(),不是t1,t1未申報且未初始化。接下來,您應該使用OnClickListener,如下所示:

public class Calci extends Activity { 
TextView t1; 
EditText e1,e2; 
Button add,sub,mul,div; 

    String b,a; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_calci); 
    e1=(EditText) findViewById(R.id.EditText01); 
    e2=(EditText) findViewById(R.id.EditText02); 
    add=(Button) findViewById(R.id.add); 
    sub=(Button) findViewById(R.id.sub); 
    mul=(Button) findViewById(R.id.mul); 
    div=(Button) findViewById(R.id.div); 
    add.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
         a=e1.getText().toString(); 
         b=e2.getText().toString(); 
         int result=Integer.parseInt(a) + Integer.parseInt(b); 
         e1.setText(Integer.toString(result)); 

     } 
    }); 
} 


} 

是的,請檢查文本字段是否爲空字符串。

if((!a.isEmpty())&(!b.isEmpty())) { your code } 

而最好的方法 - 使用的try-catch追趕FormatNumberException

try { 
     int result=Integer.parseInt(a) + Integer.parseInt(b); 
     e1.setText(Integer.toString(result)); 
    } 
    catch (FormatNumberException e) { 
    Log.d("Exception", e.getMessage()); 
    } 

這樣就可以防止所有的崩潰。

+0

thankz此外,如果用戶犯規進入任何BT的話,我甲肝設置舉杯I M越來越FC – Anuj

+0

您可以顯示這個土司扣()部分 –