一些錯誤及淩亂知識

來源:互聯網
上載者:User

1. SharedPreference 無法寫入值

 

先看下代碼:

public void storePreference(boolean value) {if(preference == null) {preference = getPreferences(Context.MODE_PRIVATE);}Log.d("Write SharedPreferences", "FLAG: " + value);preference.edit().putBoolean("FLAG", value);preference.edit().commit();}

咋看好像是正確的..  實際上這樣寫是不對的。 正確的寫法:

public void storePreference(boolean value) {if(preference == null) {preference = getPreferences(Context.MODE_PRIVATE);}Log.d("Write SharedPreferences", "FLAG: " + value);preference.edit().putBoolean("FLAG", value).commit();}

2. AlarmManager

 

 

 

3. getSharedPreferences 與 getPreferences 的區別。

 

getSharedPreferences   是Context類中的方法, 可以指定file name 以及 mode。

getPreferences  是Activity類中的方法,只需指定mode

 

 

4. 返回Home的代碼

Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME); startActivity(intent);


5.ScrollView滾動到頂部

scrollView.post(new Runnable() {@Overridepublic void run() {scrollView.scrollTo(0,0);}});

不用線程還不行,NND!

6. 動畫結束後執行某些動作。


    Animation hang_fall = AnimationUtils.loadAnimation(Curriculum.this, R.anim.hang_fall );    hang_fall.setAnimationListener(new Animation.AnimationListener()        {            public void onAnimationEnd(Animation animation)           {                Intent i = new Intent( ThisActivity.this, NextActivity.class );                ThisActivity.this.startActivity( i );            }            public void onAnimationRepeat(Animation animation)            {                // Do nothing!            }            public void  onAnimationStart(Animation animation)            {                // Do nothing!            }        });    v.startAnimation( hang_fall );

7. 自訂SeekBar常見問題

1. 自訂的圓球高度小於進度條的高度

解決方案:

        android:layout_height="wrap_content"  //高度自適應,以便能容下圓球的高度
        android:minHeight="10dip" android:maxHeight="10dip"   //指定進度條的高度,讓高度比球小就行(關鍵)


2. 自訂的圓球在進度條左邊和右邊被擋住

解決方案:

         android:thumbOffset="0dp" / /調整這個值,預設情況下為8px (或者對圓球圖片進行處理,左右各留一部分透明空間)


8. 很多朋友在用隱式Intent跳轉Activity時, 遇到如下錯誤:


android.content.ActivityNotFoundException: No Activity found to handle Intent

action對應肯定沒問題,那問題出在哪呢? 是AndroidManifest設定檔中缺少了Caterogy的配置。


Android對待所有傳遞給Context.startActivity()的隱式intent 至少包含"android.intent.category.DEFAULT"(對應CATEGORY_DEFAULT常量)。因此,活動想要接收隱式intent必須要在intent過濾器中包含"android.intent.category.DEFAULT"。

注意:"android.intent.action.MAIN" 和 "android.intent.category.LAUNCHER"設定,它們分別標記活動開始新的任務和帶到啟動列表介面。它們可以包含"android.intent.category.DEFAULT"到種類列表,也可以不包含。


9. 自訂ListView、ExpandableListView快速滑動塊  (代碼中elv代表ExpandableListView執行個體)


        try {        // 反射        Field f = AbsListView.class.getDeclaredField("mFastScroller");        f.setAccessible(true);        Object o= f.get(elv);        f = f.getType().getDeclaredField("mThumbDrawable");        f.setAccessible(true);        Drawable drawable = (Drawable) f.get(o);        drawable = getResources().getDrawable(R.drawable.icon);        f.set(o,drawable);    } catch (Exception e) {    Logger.e(TAG, e.getMessage(), e);    }

10. Android 解析json出錯

在使用  JSONObject jsonContent = new JSONObject(content); 時出錯,日誌:

 org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject。

最後發現,因為這個json檔案是在windows上建立的,多了UTF-8的BOM檔案頭。 使用Notepad++等工具去掉這個BOM頭就ok了。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.