Android開發中各種問題集錦【1-10】

來源:互聯網
上載者:User

 本文主要收集個人在Android應用開發過程中遇到一些問題,包括開發過程中遇到一些Bug應如何解決,或者如何通過代碼調用某個方法實現一定的功能.....等等各種問題。俗話說:好記性不如爛筆頭。
【1】Android中如何通過單擊Button按鈕來實現 BACK(返回)功能:
          網上有很多人都說通過調用:         
[html]
onKeyDown(KeyEvent.KEYCODE_BACK, null); 

          來實現該功能。但會出現報錯!!
          其實可以通過調用如下系統的方法來實現該功能:
[html]
onBackPressed(); 

【2】Android中如何通過單擊Button按鈕(或者其它方式)實現 MENU(菜單) 功能:
[html]
openOptionsMenu(); 
【3】android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 異常解決方案:
 原由:定義了一個Context的變數,如 private Context mContext; 同時在 onCreate(Bundle savedInstanceState); 方法中採用 mContext = getApplicationContext(); 執行個體化。同時在該Activity中添加一個AlertDialog的提示對話方塊,採用如下方式執行個體化:
[html]
AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
這樣就會出現異常!致報這個錯是在於new AlertDialog.Builder(mcontext),雖然這裡的參數是AlertDialog.Builder(Context context)但我們不能使用getApplicationContext()獲得的Context,而必須使用Activity,因為只有一個Activity才能添加一個表單。
解決方案:將new AlertDialog.Builder(Context context)中的參數用Activity.this(Activity是你的Activity的名稱)來填充就可以正確的建立一個Dialog了。
【4】android中動態實現全屏和動態退出全屏方法:
[html]
/** 
 * 動態設定全屏 
 */ 
private void setFullScreen(){ 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
   } 
 
/** 
 * 動態取消全屏 
 */ 
private void quitFullScreen(){ 
    final WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
    attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().setAttributes(attrs); 
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 

【5】Android中如何擷取SDCard的目錄路徑:
[html]
   /** 
    * 擷取SDCard的目錄路徑功能 
    * @return 
    */ 
private String getSDCardPath(){ 
    File sdcardDir = null; 
    //判斷SDCard是否存在 
    boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
    if(sdcardExist){ 
        sdcardDir = Environment.getExternalStorageDirectory(); 
    } 
    return sdcardDir.toString(); 

【6】未完待續...

摘自 Android-Idea

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.