国产午夜精品理论片,国产亚洲精品久久久999蜜臀,国产精品久久久久久久久免费,国产卡一卡二卡3卡4乱码,国产精品午夜无码av天美传媒

Android 開發(fā)技巧 開發(fā)常見問題歸納 需要注意的坑和解決方案
發(fā)布時間:2016/1/8 來源:搜數(shù)網(wǎng)絡 瀏覽:46

1. 在Activity還沒完全顯示時,彈出PopupWindow或者Dialog  崩Activity not running 錯誤

        解決方案: 重寫onWindowFocusChanged方法 

  1.        @Override  
  2. public void onWindowFocusChanged(boolean hasFocus) {  
  3.   
  4.     if (hasFocus) {  
  5.         if (!mIsInitData) {  
  6.             initData();  
  7.             mIsInitData = true;  
  8.         }  
  9.     }  
  10.   
  11.     super.onWindowFocusChanged(hasFocus);  
  12. }  
  13.   
  14. protected void initData() {  
  15.            // 在此處編寫彈出Popup或者Dialog的方法  
  16. }  
2. 在library中使用switch語句處理id 時報錯

        產(chǎn)生原因:library中生成的R文件中生成的id 沒有用final 修飾(不要問為什么,我也想知道)

        解決方案:使用if else 替換switch

3. 不要在Application中緩存任何數(shù)據(jù),NoPointException

       產(chǎn)生原因:一般情況下不會出現(xiàn)異常,當按下Home 應用隱藏到后臺,長時間未使用,導致應用被回收,當再次啟動時,Application會重新創(chuàng)建,而Activity此時再向Application里取數(shù)據(jù),異常發(fā)生

       解決方案:將數(shù)據(jù)緩存到sp 或者數(shù)據(jù)庫或者sd卡?;蛘咴谑褂肁pplication緩存數(shù)據(jù)時,進行null判斷

4. AsyncTask只能執(zhí)行一次

5. 提取一個BaseActivity,里面進行一些統(tǒng)一處理,能讓你的代碼更簡潔。繼承第三方框架的時候也很方便處理,特別是繼承友盟統(tǒng)計是,如果沒有Base,那你就哭(┬_┬)吧

  1. public abstract class BaseActivity extends Activity implements OnClickListener {  
  2.   
  3.     private boolean mIsInitData;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.           
  9.     }  
  10.     @Override  
  11.     public void setContentView(int layoutResID) {  
  12.         super.setContentView(layoutResID);  
  13.         initView();  
  14.     }  
  15.     @Override  
  16.     public void onWindowFocusChanged(boolean hasFocus) {  
  17.   
  18.         if (hasFocus) {  
  19.             if (!mIsInitData) {  
  20.                 initData();  
  21.                 mIsInitData = true;  
  22.             }  
  23.         }  
  24.   
  25.         super.onWindowFocusChanged(hasFocus);  
  26.     }  
  27.   
  28.     protected void initData() {  
  29.   
  30.     }  
  31.   
  32.     protected abstract void initView();  
  33.   
  34.     /** 
  35.      * 獲取View 
  36.      *  
  37.      * @param id 
  38.      * @return 
  39.      */  
  40.     @SuppressWarnings("unchecked")  
  41.     protected extends View> T mGetView(int id) {  
  42.         return (T) findViewById(id);  
  43.     }  
  44.   
  45.     /** 
  46.      * 獲取Button的實例 并綁定點擊事件 
  47.      *  
  48.      * @param id 
  49.      * @return 
  50.      */  
  51.     protected Button mGetButtonSetOnClick(int id) {  
  52.   
  53.         Button btn = (Button) findViewById(id);  
  54.         btn.setOnClickListener(this);  
  55.         return btn;  
  56.     }  
  57.   
  58.     /** 
  59.      * 獲取ImageView的實例 并綁定點擊事件 
  60.      *  
  61.      * @param id 
  62.      * @return 
  63.      */  
  64.     protected ImageView mGetImageViewSetOnClick(int id) {  
  65.   
  66.         ImageView image = (ImageView) findViewById(id);  
  67.         image.setOnClickListener(this);  
  68.         return image;  
  69.     }  
  70.   
  71.     /** 
  72.      * 獲取View的實例 并綁定點擊事件 
  73.      *  
  74.      * @param id 
  75.      * @return 
  76.      */  
  77.     protected View mGetViewSetOnClick(int id) {  
  78.   
  79.         View view = (View) findViewById(id);  
  80.         view.setOnClickListener(this);  
  81.         return view;  
  82.     }  
  83.   
  84.     /** 
  85.      * 獲取TextView中的文本信息 
  86.      *  
  87.      * @param tv 
  88.      * @return 
  89.      */  
  90.     protected String mGetTextViewContent(TextView tv) {  
  91.         return tv.getText().toString().trim();  
  92.     }  
  93.   
  94.     /** 
  95.      * 獲取EditText中的文本信息 
  96.      *  
  97.      * @param et 
  98.      * @return 
  99.      */  
  100.     protected String mGetEditTextContent(EditText et) {  
  101.         return et.getText().toString().trim();  
  102.     }  
  103.         protected void showHintMsg(int sid) {  
  104.         MToast.showToast(this, getResources().getString(sid));  
  105.     }  
  106.     protected void showHintMsg(String sMsg) {  
  107.         MToast.showToast(this, sMsg);  
  108.     }  
  109.   
  110. }  

6. Toast定義為全局,避免一直不斷的吐吐吐吐。

     

  1. public class MToast {  
  2.     private static Toast mToast;  
  3.   
  4.     private static TextView tv_content;  
  5.   
  6.     public static void showToast(Context context, String msg) {  
  7.         try {  
  8.   
  9.             if (mToast == null) {  
  10.                 mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);  
  11.                 mToast.setGravity(Gravity.TOP, 0,  
  12.                         DensityUtil.dip2px(context, 3));  
  13.                 View view = View.inflate(context, R.layout.m_toast, null);  
  14.                 tv_content = (TextView) view.findViewById(R.id.tv_content);  
  15.                 mToast.setView(view);  
  16.                 tv_content.setText(msg);  
  17.             } else {  
  18.                 tv_content.setText(msg);  
  19.             }  
  20.             mToast.show();  
  21.         } catch (Exception e) {  
  22.             // TODO: handle exception  
  23.         }  
  24.     }  
  25. }  

7. 標題欄樣式抽取,抽取思路大概有兩種,第一種:用標簽在xml布局時引入,第二種:自定義一個TitleView,千萬不要偷懶節(jié)省這個步驟。指不定那天產(chǎn)品就要讓你改個樣式,到時候你就哭吧。不僅僅是標題欄,字體大小,主題顏色,能抽取的都統(tǒng)一處理,產(chǎn)品的心和女人的新一樣,說變就變。

8. TextView.setText();中要顯示int類型的值,用String.valueOf()轉,不要直接124+“”,不知道為什么這樣的同學,基礎太差,去看看源碼就知道為什么了。

9. 退出應用方式,1.直接殺死進程 2.在BaseActivity中注冊一個廣播,發(fā)送廣播關閉 3.定義一個全局容器存儲Activity應用,退出時遍歷退出(不推薦)

10. 一個功能分幾個頁面處理時,使用Dialog 模擬Activity 避免了數(shù)據(jù)在Activity之間傳遞。

11. 手機重啟,知乎上看到滴,通過不斷的new 空Toast,導致系統(tǒng)奔潰而重啟,想想竟有一種無言以對的感覺,原來Toast還可以尼瑪這么玩

  1. public void onClick(View v){  
  2.        while(true){  
  3.             Toast toast = new Toast(this);  
  4.              toast.setView(new View(this));  
  5.              toast.show();  
  6.      }  
  7.   
  8. }  

12. View類中的setSelected(boolean)方法結合android:state_selected="" 用來實現(xiàn)圖片選中效果 自定義標題欄用起來很方便;

13. EditText 中有個 android:digits="" 屬性,用來自定義輸入的字符類型,比如輸入身份證只能是數(shù)字和x或者X  使用 android:digits="1234567890xX"  就輕松搞定了,不用再在代碼里面進行蛋疼的校驗了;

今天就補充到這

原文:http://blog.csdn.net/soul_code/article/details/50128637

返回