綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] app計算機

[複製連結] 查看: 2362|回覆: 17|好評: 0
跳轉到指定樓層
樓主
lieak5922512 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2014-6-6 22:56

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
小弟最近寫了簡易計算機 由於技巧不是很純熟 想拜託大大幫我debug 因為我怎麼找都找不到錯誤(資利太淺)  
strings

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>

  3.     <string name="app_name">HW01</string>
  4.     <string name="hello_world">Hello world!</string>
  5.     <string name="action_settings">Settings</string>
  6.     <string name="tv_1">輸入數字</string>
  7.     <string name="btn_1">1</string>
  8.     <string name="btn_2">2</string>
  9.     <string name="btn_3">3</string>
  10.     <string name="btn_4">4</string>
  11.     <string name="btn_5">5</string>
  12.     <string name="btn_6">6</string>
  13.     <string name="btn_7">7</string>
  14.     <string name="btn_8">8</string>
  15.     <string name="btn_9">9</string>
  16.     <string name="btn_10">0</string>
  17.     <string name="btn_11">+</string>
  18.     <string name="btn_12">-</string>
  19.     <string name="btn_13">*</string>
  20.     <string name="btn_14">/</string>
  21.     <string name="btn_15">.</string>
  22.     <string name="btn_16">=</string>
  23.     <string name="btn_17">C</string>
  24.    
  25. </resources>
複製代碼

activity

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tool="http://schemas.android.com/apk/res/android"
  4.     tool:layout_width="fill_parent"
  5.     tool:layout_height="fill_parent"
  6.     android:orientation="vertical" >
  7.     <TextView
  8.         android:id="@+id/tv_1"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:textSize="30sp"
  12.         android:text=""/>
  13.     <LinearLayout
  14.         android:layout_width="fill_parent"
  15.         android:layout_height="wrap_content"
  16.         android:orientation="horizontal">
  17.         <Button
  18.             android:id="@+id/btn_1"
  19.             android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content"
  21.             android:text="@string/btn_1" />
  22.         <Button
  23.             tool:id="@+id/btn_2"
  24.             tool:layout_width="wrap_content"
  25.             tool:layout_height="wrap_content"
  26.             tool:text="@string/btn_2" />
  27.         <Button
  28.             android:id="@+id/btn_3"
  29.             android:layout_width="wrap_content"
  30.             android:layout_height="wrap_content"
  31.             android:text="@string/btn_3" />
  32.          <Button
  33.             android:id="@+id/btn_11"
  34.             android:layout_width="wrap_content"
  35.             android:layout_height="wrap_content"
  36.             android:text="@string/btn_11" />
  37.         </LinearLayout>
  38.         <LinearLayout
  39.         android:layout_width="fill_parent"
  40.         android:layout_height="wrap_content"
  41.         android:orientation="horizontal">
  42.         <Button
  43.             android:id="@+id/btn_4"
  44.             android:layout_width="wrap_content"
  45.             android:layout_height="wrap_content"
  46.             android:text="@string/btn_4" />
  47.         <Button
  48.             android:id="@+id/btn_5"
  49.             android:layout_width="wrap_content"
  50.             android:layout_height="wrap_content"
  51.             android:text="@string/btn_5" />
  52.         <Button
  53.             android:id="@+id/btn_6"
  54.             android:layout_width="wrap_content"
  55.             android:layout_height="wrap_content"
  56.             android:text="@string/btn_6" />
  57.         <Button
  58.             android:id="@+id/btn_12"
  59.             android:layout_width="wrap_content"
  60.             android:layout_height="wrap_content"
  61.             android:text="@string/btn_12" />
  62.         </LinearLayout>
  63.         <LinearLayout
  64.         android:layout_width="fill_parent"
  65.         android:layout_height="wrap_content"
  66.         android:orientation="horizontal">
  67.         <Button
  68.             android:id="@+id/btn_7"
  69.             android:layout_width="wrap_content"
  70.             android:layout_height="wrap_content"
  71.             android:text="@string/btn_7" />
  72.         <Button
  73.             android:id="@+id/btn_8"
  74.             android:layout_width="wrap_content"
  75.             android:layout_height="wrap_content"
  76.             android:text="@string/btn_8" />
  77.         <Button
  78.             android:id="@+id/btn_9"
  79.             android:layout_width="wrap_content"
  80.             android:layout_height="wrap_content"
  81.             android:text="@string/btn_9" />
  82.         <Button
  83.             android:id="@+id/btn_13"
  84.             android:layout_width="wrap_content"
  85.             android:layout_height="wrap_content"
  86.             android:text="@string/btn_13" />
  87.         </LinearLayout>
  88.          <LinearLayout
  89.         android:layout_width="fill_parent"
  90.         android:layout_height="wrap_content"
  91.         android:orientation="horizontal">
  92.         <Button
  93.             android:id="@+id/btn_10"
  94.             android:layout_width="wrap_content"
  95.             android:layout_height="wrap_content"
  96.             android:text="@string/btn_10" />
  97.         <Button
  98.             android:id="@+id/btn_15"
  99.             android:layout_width="wrap_content"
  100.             android:layout_height="wrap_content"
  101.             android:text="@string/btn_15" />
  102.         <Button
  103.             android:id="@+id/btn_16"
  104.             android:layout_width="wrap_content"
  105.             android:layout_height="wrap_content"
  106.             android:text="@string/btn_16" />
  107.          <Button
  108.             android:id="@+id/btn_14"
  109.             android:layout_width="wrap_content"
  110.             android:layout_height="wrap_content"
  111.             android:text="@string/btn_14" />
  112.         </LinearLayout>
  113.         <Button
  114.             android:id="@+id/btn_17"
  115.             android:layout_width="fill_parent"
  116.             android:layout_height="wrap_content"
  117.             android:text="@string/btn_17" />
  118. </LinearLayout>
複製代碼

Mainactivity

  1. package com.example.hw01;

  2. import java.text.DecimalFormat;

  3. import android.support.v7.app.ActionBarActivity;
  4. import android.support.v7.app.ActionBar;
  5. import android.support.v4.app.Fragment;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.content.Intent;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.Button;
  14. import android.widget.Switch;
  15. import android.widget.TextView;
  16. import android.os.Build;
  17. import android.view.View.OnClickListener;
  18. import android.R.anim;
  19. public class MainActivity extends ActionBarActivity {
  20.     TextView tv_1;
  21.     Button   btn_1;
  22.     Button   btn_2;
  23.     Button   btn_3;
  24.     Button   btn_4;
  25.     Button   btn_5;
  26.     Button   btn_6;
  27.     Button   btn_7;
  28.     Button   btn_8;
  29.     Button   btn_9;
  30.     Button   btn_10;
  31.     Button   btn_11;
  32.     Button   btn_12;
  33.     Button   btn_13;
  34.     Button   btn_14;
  35.     Button   btn_15;
  36.     Button   btn_16;
  37.     Button   btn_17;
  38.     double   s;
  39.     double t1;
  40.   @Override
  41.   protected void onCreate(Bundle savedInstanceState) {
  42.     super.onCreate(savedInstanceState);
  43.     setContentView(R.layout.activity_main);
  44.    
  45.     set();
  46.     initiew();
  47.   }
  48.       public void initiew() {
  49.    
  50.   
  51.     tv_1  =(TextView)findViewById(R.id.tv_1);
  52.     btn_1 =(Button)findViewById(R.id.btn_1);
  53.     btn_2 =(Button)findViewById(R.id.btn_2);
  54.     btn_3 =(Button)findViewById(R.id.btn_3);
  55.     btn_4 =(Button)findViewById(R.id.btn_4);
  56.     btn_5 =(Button)findViewById(R.id.btn_5);
  57.     btn_6 =(Button)findViewById(R.id.btn_6);
  58.     btn_7 =(Button)findViewById(R.id.btn_7);
  59.     btn_8 =(Button)findViewById(R.id.btn_8);
  60.     btn_9 =(Button)findViewById(R.id.btn_9);
  61.     btn_10 =(Button)findViewById(R.id.btn_10);
  62.     btn_11 =(Button)findViewById(R.id.btn_11);
  63.     btn_12 =(Button)findViewById(R.id.btn_12);
  64.     btn_13 =(Button)findViewById(R.id.btn_13);
  65.     btn_14 =(Button)findViewById(R.id.btn_14);
  66.     btn_15 =(Button)findViewById(R.id.btn_15);
  67.     btn_16 =(Button)findViewById(R.id.btn_16);
  68.     btn_17 =(Button)findViewById(R.id.btn_17);
  69.    
  70.   }
  71.   public void  set(){
  72.     btn_15.setOnClickListener(tv_2);
  73.     btn_1.setOnClickListener(tv_2);
  74.     btn_2.setOnClickListener(tv_2);
  75.     btn_3.setOnClickListener(tv_2);
  76.     btn_4.setOnClickListener(tv_2);
  77.     btn_5.setOnClickListener(tv_2);
  78.     btn_6.setOnClickListener(tv_2);
  79.     btn_7.setOnClickListener(tv_2);
  80.     btn_8.setOnClickListener(tv_2);
  81.     btn_9.setOnClickListener(tv_2);
  82.     btn_10.setOnClickListener(tv_2);
  83.     btn_11.setOnClickListener(Compu);
  84.     btn_12.setOnClickListener(Compu);
  85.     btn_13.setOnClickListener(Compu);
  86.     btn_14.setOnClickListener(Compu);
  87.     btn_16.setOnClickListener(equ);
  88.     btn_17.setOnClickListener(clear);
  89.   }
  90.   public Button.OnClickListener tv_2 = new Button.OnClickListener() {
  91.    
  92.     @Override
  93.     public void onClick(View v) {
  94.       String button = tv_1.getText().toString();
  95.       switch(v.getId())
  96.       {
  97.       case R.id.btn_1:
  98.       {
  99.       tv_1.setText(button+"1");
  100.       break;
  101.       }
  102.       case R.id.btn_2:
  103.       {
  104.       tv_1.setText(button+"2");
  105.       break;
  106.       }
  107.       case R.id.btn_3:
  108.       {
  109.       tv_1.setText(button+"3");
  110.       break;
  111.       }
  112.       case R.id.btn_4:
  113.       {
  114.       tv_1.setText(button+"4");
  115.       break;
  116.       }
  117.       case R.id.btn_5:
  118.       {
  119.       tv_1.setText(button+"5");
  120.       break;
  121.       }
  122.       case R.id.btn_6:
  123.       {
  124.       tv_1.setText(button+"6");
  125.       break;
  126.       }
  127.       case R.id.btn_7:
  128.       {
  129.       tv_1.setText(button+"7");
  130.       break;
  131.       }
  132.       case R.id.btn_8:
  133.       {
  134.       tv_1.setText(button+"8");
  135.       break;
  136.       }
  137.       case R.id.btn_9:
  138.       {
  139.       tv_1.setText(button+"9");
  140.       break;
  141.       }
  142.       case R.id.btn_10:
  143.       {
  144.       tv_1.setText(button+"0");
  145.       break;
  146.       }
  147.       case R.id.btn_15:
  148.       {
  149.       tv_1.setText(button+".");
  150.       break;
  151.       }
  152.       }
  153.     }
  154.   };
  155.     public Button.OnClickListener Compu = new Button.OnClickListener() {
  156.     @Override
  157.     public void onClick(View v) {
  158.       t1=Double.parseDouble(tv_1.getText().toString());
  159.       switch(v.getId()){
  160.       case R.id.btn_11:
  161.       {
  162.         s=1;
  163.         tv_1.setText("");   
  164.       break;
  165.       }
  166.       case R.id.btn_12:
  167.       {
  168.         s=2;
  169.         tv_1.setText("");   
  170.       break;
  171.       }  
  172.       case R.id.btn_13:
  173.       {
  174.         s=3;
  175.         tv_1.setText("");   
  176.       break;
  177.       }
  178.       case R.id.btn_14:
  179.       {
  180.         s=4;
  181.         tv_1.setText("");   
  182.       break;
  183.       }
  184.       }
  185.     }
  186.     };
  187.      public Button.OnClickListener equ = new Button.OnClickListener()
  188.      {
  189.         @Override
  190.         public void onClick(View v)
  191.         {
  192.         double t2=Double.parseDouble(tv_1.getText().toString());
  193.         DecimalFormat tf = new DecimalFormat("0.00");
  194.         if(s==1)
  195.         {
  196.           tv_1.setText(tf.format(t1+t2));
  197.         }
  198.         else if(s==2){
  199.            
  200.             tv_1.setText(tf.format(t1-t2));
  201.         }
  202.         else if(s==3){
  203.            
  204.           tv_1.setText(tf.format(t1*t2));
  205.           }
  206.         else if(s==4){
  207.            
  208.           tv_1.setText(tf.format(t1/t2));
  209.           }
  210.         
  211.         }
  212.         };
  213. public Button.OnClickListener clear = new Button.OnClickListener()
  214.          {
  215.             @Override
  216.     public void onClick(View v)
  217.             {
  218.               tv_1.setText("");
  219.               }};
  220.             
  221. }
複製代碼


當初在原本寫出來的電腦裡面 是找不到任何錯誤的
我懷疑是這兩行出了問題

  1. import android.support.v7.app.ActionBarActivity;
  2. import android.support.v7.app.ActionBar;
複製代碼

這兩行 是在我電腦裡面的adt裡面這個原始瑪跟另一個android.support.v7很像這東西伴生的 也不知道為啥麼

我用了別台的電腦除錯後 發現 呼應的程式碼不對 也就是

  1.   public void initiew() {
  2.    
  3.   
  4.     tv_1  =(TextView)findViewById(R.id.tv_1);
  5.     btn_1 =(Button)findViewById(R.id.btn_1);
  6.     btn_2 =(Button)findViewById(R.id.btn_2);
  7.     btn_3 =(Button)findViewById(R.id.btn_3);
  8.     btn_4 =(Button)findViewById(R.id.btn_4);
  9.     btn_5 =(Button)findViewById(R.id.btn_5);
  10.     btn_6 =(Button)findViewById(R.id.btn_6);
  11.     btn_7 =(Button)findViewById(R.id.btn_7);
  12.     btn_8 =(Button)findViewById(R.id.btn_8);
  13.     btn_9 =(Button)findViewById(R.id.btn_9);
  14.     btn_10 =(Button)findViewById(R.id.btn_10);
  15.     btn_11 =(Button)findViewById(R.id.btn_11);
  16.     btn_12 =(Button)findViewById(R.id.btn_12);
  17.     btn_13 =(Button)findViewById(R.id.btn_13);
  18.     btn_14 =(Button)findViewById(R.id.btn_14);
  19.     btn_15 =(Button)findViewById(R.id.btn_15);
  20.     btn_16 =(Button)findViewById(R.id.btn_16);
  21.     btn_17 =(Button)findViewById(R.id.btn_17);
  22.    
  23.   }
複製代碼

這邊的按鈕因該要跟 變數對著的開關、strings裡的東西、activity牌版做呼應 照裡來說因該都有呼應到阿? 怎麼會沒辦法
求解惑 感謝
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
davidshyu | 收聽TA | 只看該作者
發表於 2014-6-8 00:23
本帖最後由 davidshyu 於 2014-6-8 00:28 編輯

<Button
            tool:id="@+id/btn_2"
            tool:layout_width="wrap_content"
            tool:layout_height="wrap_content"
            tool:text="@string/btn_2" />

是你貼錯還是???? tool: -->android
把DDOM錯誤訊息也PO出來會比較好判斷
還有你應該是使用刪除法吧?? 新建專案的內容不應該是這樣
建議你不要使用刪除法,,最好使用自動生成的內容去寫,,要慢慢適應他們新的寫法
因他們現在在注重studio這套,到後面可能ADT會.....到最後你反過來要適應新的寫法,你會很痛苦
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
 樓主| lieak5922512 | 收聽TA | 只看該作者
發表於 2014-6-8 01:00
davidshyu 發表於 2014-6-8 00:23
是你貼錯還是???? tool: -->android
把DDOM錯誤訊息也PO出來會比較好判斷
還有你應該是使用刪除法吧??  ...

刪除法是啥麼? 還有那個是我忘記改到的 因為之前只能用Fragment寫 後來有人教 所以就改回activity了

如果試開關的問題的話 是我移上去的沒錯 畢竟 打完的開關他不會幫我自動排列 就很容易弄再一塊

如果是專案問題的話= = 我有砍專案的習慣 為了要天天練習 砍專案我是常常做的

用別的電腦看錯誤地方在

  1. public void initiew() {
  2.                
  3.        
  4.                 [color=Red]tv_1  =(TextView)findViewById(R.id.tv_1)[/color];
  5.                 [color=Red]btn_1 =(Button)findViewById(R.id.btn_1)[/color];
  6.                 [color=Red]btn_2 =(Button)findViewById(R.id.btn_2)[/color];
  7.                 [color=Red]btn_3 =(Button)findViewById(R.id.btn_3)[/color];
  8.                 [color=Red]btn_4 =(Button)findViewById(R.id.btn_4)[/color];
  9.                 [color=Red]btn_5 =(Button)findViewById(R.id.btn_5)[/color];
  10.                 [color=Red]btn_6 =(Button)findViewById(R.id.btn_6)[/color];
  11.                 [color=Red]btn_7 =(Button)findViewById(R.id.btn_7)[/color];
  12.                 [color=Red]btn_8 =(Button)findViewById(R.id.btn_8)[/color];
  13.                 [color=Red]btn_9 =(Button)findViewById(R.id.btn_9)[/color];
  14.                 [color=Red]btn_10 =(Button)findViewById(R.id.btn_10)[/color];
  15.                 [color=Red]btn_11 =(Button)findViewById(R.id.btn_11)[/color];
  16.                 [color=Red]btn_12 =(Button)findViewById(R.id.btn_12)[/color];
  17.                 [color=Red]btn_13 =(Button)findViewById(R.id.btn_13)[/color];
  18.                 [color=Red]btn_14 =(Button)findViewById(R.id.btn_14)[/color];
  19.                 [color=Red]btn_15 =(Button)findViewById(R.id.btn_15)[/color];
  20.                 [color=Red]btn_16 =(Button)findViewById(R.id.btn_16)[/color];
  21.                 [color=Red]btn_17 =(Button)findViewById(R.id.btn_17)[/color];
  22.                
  23.         }
複製代碼


  1. public Button.OnClickListener tv_2 = new Button.OnClickListener() {
  2.                
  3.                 @Override
  4.                 public void onClick(View v) {
  5.                         String button = tv_1.getText().toString();
  6.                         switch(v.getId())
  7.                         {
  8.                         case R.id.[color=Red]btn_1[/color]:
  9.                         {
  10.                         tv_1.setText(button+"1");
  11.                         break;
  12.                         }
  13.                         case R.id.[color=Red]btn_2[/color]:
  14.                         {
  15.                         tv_1.setText(button+"2");
  16.                         break;
  17.                         }
  18.                         case R.id.[color=Red]btn_3[/color]:
  19.                         {
  20.                         tv_1.setText(button+"3");
  21.                         break;
  22.                         }
  23.                         case R.id.[color=Red]btn_4[/color]:
  24.                         {
  25.                         tv_1.setText(button+"4");
  26.                         break;
  27.                         }
  28.                         case R.id.[color=Red]btn_5[/color]:
  29.                         {
  30.                         tv_1.setText(button+"5");
  31.                         break;
  32.                         }
  33.                         case R.id.[color=Red]btn_6[/color]:
  34.                         {
  35.                         tv_1.setText(button+"6");
  36.                         break;
  37.                         }
  38.                         case R.id.[color=Red]btn_7[/color]:
  39.                         {
  40.                         tv_1.setText(button+"7"[/color]);
  41.                         break;
  42.                         }
  43.                         case R.id.btn_8[/color]:
  44.                         {
  45.                         tv_1.setText(button+"8"[/color]);
  46.                         break;
  47.                         }
  48.                         case R.id.[color=Red]btn_9[/color]:
  49.                         {
  50.                         tv_1.setText(button+"9"[/color]);
  51.                         break;
  52.                         }
  53.                         case R.id.[color=Red]btn_10[/color]:
  54.                         {
  55.                         tv_1.setText(button+"0");
  56.                         break;
  57.                         }
  58.                         case R.id.[color=Red]btn_15[/color]:
  59.                         {
  60.                         tv_1.setText(button+".");
  61.                         break;
  62.                         }
  63.                         }
  64.                 }
  65.         };
複製代碼


別台電腦看的錯誤就這些 感謝
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
whl | 收聽TA | 只看該作者
發表於 2014-6-8 10:14
有沒有修正過 d大 的問題?修正後還是一樣的問題嗎?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
 樓主| lieak5922512 | 收聽TA | 只看該作者
發表於 2014-6-8 13:27
whl 發表於 2014-6-8 10:14
有沒有修正過 d大 的問題?修正後還是一樣的問題嗎?

不行 還是一樣動不了 雖然修正了可是一樣還是動不了
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
whl | 收聽TA | 只看該作者
發表於 2014-6-8 19:51
貼 log來看看
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

7
 樓主| lieak5922512 | 收聽TA | 只看該作者
發表於 2014-6-9 00:32
我貼從eclipse上的東西下來好了 順便把那奇怪的app裝置弄下來

HW01.apk

787.92 KB, 下載次數: 0, 下載積分: 碎鑽 -1 個

程式apk

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

8
 樓主| lieak5922512 | 收聽TA | 只看該作者
發表於 2014-6-9 00:35

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

9
whl | 收聽TA | 只看該作者
發表於 2014-6-9 08:59
直接給 apk 我不會去測試,log 內會有錯誤的訊息,所以貼 log 上來,重點是紅字的部分。
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

10
CarolHsieh | 收聽TA | 只看該作者
發表於 2014-6-9 09:54
  1. @Override
  2.   protected void onCreate(Bundle savedInstanceState) {
  3.     super.onCreate(savedInstanceState);
  4.     setContentView(R.layout.activity_main);
  5.    
  6.     set();//設監聽
  7.     initiew();//找元件

  8. //順序上就有錯了,應該是

  9. initiew();//找元件
  10. set();//設監聽
  11.    
  12.   }
複製代碼
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則