0

我正在创建一个简单的 android 应用程序,它具有 editText 字段和 textView 字段,这些字段中的值只是数字。

我创建了一个按钮,在选择时,我希望将编辑文本和文本视图的值添加在一起,然后我希望将 editText 更改为“0”的值,并在 textView 中显示总计.

例如,如果在按下按钮时 editText 的值为 10,而 textView 的值为 20,则 textView 中将显示总计 30,而 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));
}


});

日志猫

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“数字”,因此用户不能输入一些十进制值。如果你想要这样,你不需要双变量。使用 int 而不是 double,因此您的结果不显示小数点:

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