我正在创建一个简单的 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>