0

editText フィールドと textView フィールドを持つ単純な Android アプリを作成しています。これらのフィールド内の値は数値のみです。

選択時に編集テキストとテキスト ビューの値を加算したいボタンを作成しました。その後、editText の値を '0' に変更し、合計を textView に表示します。 .

たとえば、ボタンを押したときに editText の値が 10 で textView の値が 20 の場合、合計 30 が textView に表示され、editText の値は 0 と表示されます。

これまでのコードは次のとおりです。アプリがクラッシュする瞬間にボタンを押すと、正しくコーディングしたかどうか確信が持てません。私はJavaが初めてです。どんな助けでも大歓迎です。

public class durood extends Activity{

TextView txtCount;
TextView totalCount;
EditText enteramount;
Button btnCount;
Button addtotal1;
Button addtotal2;
Button cleartotal;
static int count=0;
double x=0;
double y=0;
double z=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.durood);

enteramount = (EditText)findViewById(R.id.enteramount);
totalCount = (TextView)findViewById(R.id.totalCount);
addtotal1=(Button)findViewById(R.id.addtototal1);
addtotal2=(Button)findViewById(R.id.addtototal2);


//add to total 1
addtotal1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

x=Double.parseDouble(enteramount.getText().toString());
y=Double.parseDouble(totalCount.getText().toString());
z=x+y;
totalCount.setText(Double.toString(z));
}


});

LogCat

12-30 23:52:11.098: W/Trace(615): Unexpected value from nativeGetEnabledTags: 0
12-30 23:52:11.129: E/ActivityThread(615): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d332e8 that was originally bound here
12-30 23:52:11.129: E/ActivityThread(615): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d332e8 that was originally bound here
12-30 23:52:11.129: E/ActivityThread(615):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-30 23:52:11.129: E/ActivityThread(615):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-30 23:52:11.129: E/ActivityThread(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-30 23:52:11.129: E/ActivityThread(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-30 23:52:11.129: E/ActivityThread(615):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-30 23:52:11.129: E/ActivityThread(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-30 23:52:11.129: E/ActivityThread(615):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 23:52:11.129: E/ActivityThread(615):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 23:52:11.129: E/ActivityThread(615):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 23:52:11.129: E/ActivityThread(615):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 23:52:11.129: E/ActivityThread(615):  at java.lang.Thread.run(Thread.java:856)
12-30 23:52:11.238: E/StrictMode(615): null
12-30 23:52:11.238: E/StrictMode(615): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d332e8 that was originally bound here
12-30 23:52:11.238: E/StrictMode(615):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-30 23:52:11.238: E/StrictMode(615):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-30 23:52:11.238: E/StrictMode(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-30 23:52:11.238: E/StrictMode(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-30 23:52:11.238: E/StrictMode(615):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-30 23:52:11.238: E/StrictMode(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-30 23:52:11.238: E/StrictMode(615):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 23:52:11.238: E/StrictMode(615):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 23:52:11.238: E/StrictMode(615):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 23:52:11.238: E/StrictMode(615):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 23:52:11.238: E/StrictMode(615):  at java.lang.Thread.run(Thread.java:856)
12-30 23:52:11.272: W/ActivityManager(290): Unbind failed: could not find connection for android.os.BinderProxy@40fb7e78
12-30 23:52:11.328: E/ActivityThread(615): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d32ef0 that was originally bound here
12-30 23:52:11.328: E/ActivityThread(615): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d32ef0 that was originally bound here
12-30 23:52:11.328: E/ActivityThread(615):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-30 23:52:11.328: E/ActivityThread(615):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-30 23:52:11.328: E/ActivityThread(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-30 23:52:11.328: E/ActivityThread(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-30 23:52:11.328: E/ActivityThread(615):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-30 23:52:11.328: E/ActivityThread(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-30 23:52:11.328: E/ActivityThread(615):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 23:52:11.328: E/ActivityThread(615):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 23:52:11.328: E/ActivityThread(615):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 23:52:11.328: E/ActivityThread(615):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 23:52:11.328: E/ActivityThread(615):  at java.lang.Thread.run(Thread.java:856)
12-30 23:52:11.398: E/StrictMode(615): null
12-30 23:52:11.398: E/StrictMode(615): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d32ef0 that was originally bound here
12-30 23:52:11.398: E/StrictMode(615):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-30 23:52:11.398: E/StrictMode(615):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-30 23:52:11.398: E/StrictMode(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-30 23:52:11.398: E/StrictMode(615):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-30 23:52:11.398: E/StrictMode(615):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-30 23:52:11.398: E/StrictMode(615):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-30 23:52:11.398: E/StrictMode(615):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 23:52:11.398: E/StrictMode(615):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 23:52:11.398: E/StrictMode(615):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 23:52:11.398: E/StrictMode(615):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 23:52:11.398: E/StrictMode(615):  at java.lang.Thread.run(Thread.java:856)
12-30 23:52:11.448: W/ActivityManager(290): Unbind failed: could not find connection for android.os.BinderProxy@40f92598

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

 <Button
 android:id="@+id/dmute"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="3dp"
 android:minHeight="30dp"
 android:minWidth="48dp"
 android:text="Mute" />

 <Button
 android:id="@+id/dreset"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="198dp"
 android:minHeight="30dp"
 android:minWidth="48dp"
 android:text="Reset" />
</LinearLayout>

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:paddingTop="20dp" >

 <TextView
 android:id="@+id/dcount"
 android:layout_width="0dip"
 android:layout_height="wrap_content"
 android:layout_weight="90"
 android:gravity="center"
 android:singleLine="true"
 android:text="Numbers"
 android:textSize="25sp" />

 <Button
 android:id="@+id/addtototal1"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="5"
 android:text="+ to Total" />

  </LinearLayout>

  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:paddingTop="20dp" >

  <EditText
  android:id="@+id/enteramount"
  android:layout_width="0dip"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_weight="90"
  android:gravity="center"
  android:hint="Enter Amount"
  android:inputType="number"
  android:singleLine="true"
  android:textSize="25sp" />

  <Button
  android:id="@+id/addtototal2"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="5"
  android:text="+ to Total" />

 </LinearLayout>

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:paddingTop="20dp" >

 <TextView
 android:id="@+id/totalCount"
 android:layout_width="0dip"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_weight="80"
 android:gravity="center"
 android:singleLine="true"
 android:textSize="25sp" />

  <Button
  android:id="@+id/cleartotal"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Clear" />

 </LinearLayout>

  <Button
  android:id="@+id/dclick"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginTop="30dp"
  android:height="120dp"
  android:width="250dp" />

</LinearLayout>
4

2 に答える 2

2

Doubleに変換する前に、このように条件を確認してください...

if(enteramount.getText().toString().length()>0)
{
   x=Double.parseDouble(enteramount.getText().toString());
}
if(totalCount.getText().toString().length()>0)
{
   y=Double.parseDouble(totalCount.getText().toString());
}
于 2012-12-31T13:45:25.557 に答える
1

ボタン addtotal1 を初期化する必要があります。

    addtotal1 = (Button)findViewById(R.id.yourButtonId);

onClickListenerを設定する前に。

編集

xml EditText には inputType "number" しかないため、ユーザーは 10 進数値を入力できません。このようにしたい場合は、二重変数は必要ありません。double の代わりに int を使用すると、結果に小数点が表示されなくなります。

       public class durood extends Activity{

         private TextView txtCount;
         private TextView totalCount;
         private EditText enteramount;
         private Button btnCount;
         private Button addtotal1;
         private Button addtotal2;
         private Button cleartotal;
         private static int count=0;
         private int x=0;
         private int y=0;
         private int z=0;

          @Override
           public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
            // The activity is being created.
            setContentView(R.layout.durood);

            enteramount = (EditText)findViewById(R.id.enteramount);
            totalCount = (TextView)findViewById(R.id.totalCount);
            addtotal1=(Button)findViewById(R.id.addtototal1);
            addtotal2=(Button)findViewById(R.id.addtototal2);


            //add to total 1
            addtotal1.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {

              x=Integer.parseInt(enteramount.getText().toString());
              y=Integer.parseInt(totalCount.getText().toString());
              z=x+y;
              totalCount.setText(Integer.toString(z));
              //to set value of enteramount to 0
              enteramount.setText("0"); //or enteramaount.setText("") if You want show nothing
            }


           });
于 2012-12-31T00:02:06.643 に答える