安卓經驗收集(不斷更新)

一、如何讓對話框在任意位置顯示

在地圖上的時候通常點擊哪裏就會在哪裏顯示一個提示框,在安卓實現的時候就先要獲得該座標,並且放入dialog的window裏面去

上代碼:

OnClickListener clicklistener = new OnClickListener(){

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
int loc[] = new int[2];
view.getLocationInWindow(loc);

Dialog c = new MapCardDialog(xx,xx);

Window w = c.getWindow();     
WindowManager.LayoutParams lp = w.getAttributes();     
lp.gravity=Gravity.CENTER;
lp.x = loc[0]-890;   //新位置X座標  890爲圖中央位置
lp.y = loc[1]-690; //新位置Y座標  690爲圖中央位置
c.onWindowAttributesChanged(lp);
c.show(); 
}

};
}

先獲得點擊點的位置,然後通過window對象獲得layoutparams,然後再修改x,y屬性,最後改變就可以了

這邊值得注意一點,就是lp.gravity,關係到初始位置,我用的center屬性之後,默認從中間開始,即(890,690爲0座標),所以實際位置需要減去這兩個,當你使用其他的屬性,比如Gravity.TOP或者LEFT之類的話,那就自己去推算嘍


二、dialog背景透明

dialog默認背景是黑色,極其醜陋,之前糾結了很久,其實改起來比較簡單

在style裏面<item name="android:windowBackground">@android:color/transparent</item>即可



三、Android SDK自帶的工具Lint誤報

如果此時我們運行lint檢查錯誤的話,會發現清單文件裏註冊這個類的地方會報錯如下:
Class referenced in the manifest, com.xxx.xxx.receiver.NetworkReceiver, was not found in the
project or the libraries(在清單文件中引用的類com.xxx.xxx.receiver.NetworkReceiver沒有在工程中或類庫中找到)
 
其實這裏是一個誤報,直接無視掉就行,當我們修改完其他Lint錯誤後,只要再次重複之前的操作(右鍵工程-->Android Tools-->Clear Lint Markers)即可,

四、sqlite數據庫自增問題

我設置了sqlite的id字段爲自增字段,結果用cursor從0開始讀總是說數組越界,全部取出發現sqlite自增從1開始,尼瑪。。。


五、關於安卓程序中靜態變量問題

我在某些類中設置了一些靜態變量,從1開始自增,在銷燬Activity後從新啓動應用時候,這些變量都會接着增加而不是從1開始,由此可以看出這些變量並沒有隨着應用消失而被回收,所以得小心這點。


六、soundpool無法播放聲音的問題

soundpool無法播放聲音,原因是你的load函數和play函數貼的過近,沒有加載完就播放了,建議將load函數放入初始化中,不要在播放的時候再加載


七、java.lang.IllegalStateException: TimerTask is scheduled already錯誤的解決方法

同一個定時器任務只能被放置一次,每次放任務都要新建一個對象,否則出現這個錯誤


八、listview緩存機制

ListView先請求一個type1視圖(getView)然後請求其他可見的項目。convertView在getView中是空(null)的,第一次都是爲空的,只要顯示過了convertView都不爲空,會保存在Recycler中.


當item1滾出屏幕,並且一個新的項目從屏幕低端上來時,ListView再請求一個type1視圖。convertView此時不是空值了,它的值是item1。你只需設定新的數據然後返回convertView,不必重新創建一個視圖,省去了inflate和findViewById的時間,性能就得到了優化。



九、AudioFlinger could not  create track, status: -12

soundpool類加載超過5-6秒的音頻文件就會出現這種問題,經常是有時候有聲音,有時候沒有聲音,具體原因還有待進一步探究


十、listview設置android:listSelector="#00000000"後內部textview還是會閃動

意即將listview的點擊效果設置成透明之後,點擊選項textview中的文字會閃爍,將textview的textcolor設置成固定值後這個問題解決,具體原因還是得探究啊。。。


十一、Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?


Context中有一個startActivity方法,Activity繼承自Context,重載了startActivity方法。如果使用 Activity的startActivity方法,不會有任何限制,而如果使用Context的startActivity方法的話,就需要開啓一個新的task,遇到上面那個異常的,都是因爲使用了Context的startActivity方法。解決辦法是,加一個flag。

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 


以上轉自:http://www.cnblogs.com/rioder/archive/2011/11/02/2233584.html


以下爲Context源碼中的startActivity:

    /**
     * Launch a new activity.  You will not receive any information about when
     * the activity exits.
     *
     * <p>Note that if this method is being called from outside of an
     * {@link android.app.Activity} Context, then the Intent must include
     * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag.  This is because,
     * without being started from an existing Activity, there is no existing
     * task in which to place the new activity and thus it needs to be placed
     * in its own separate task.
     *
     * <p>This method throws {@link ActivityNotFoundException}
     * if there was no Activity found to run the given Intent.
     *
     * @param intent The description of the activity to start.
     *
     * @throws ActivityNotFoundException
     *
     * @see PackageManager#resolveActivity
     */
    public abstract void startActivity(Intent intent);


    以上解釋了爲什麼使用Context的startActivity方法(比如在Service中或者BroadcastReceiver中啓動Activity)爲什麼需要添加flag:FLAG_ACTIVITY_NEW_TASK


十二、關於startActivity(Intent)在single_top模式下啓動

用這種啓動方式,在activity中使用getIntent獲得的intent是原來的intent而不是後傳入的,所以想通過這個方式改變activity的狀態是不可行的


十三、activity的切換時候無動畫

轉自:http://blog.csdn.net/rwecho/article/details/6683789

一般直接用startActivity(Intent).默認都會有一個刷動的動畫。當然如果獎intent的flag設置爲 Intent.FLAG_ACTIVITY_NO_ANIMATION。再使用startActivity(Intent),打開的activity就會直接顯示,不會有那種刷屏的動畫。
        如果 activity1 去啓動activity2 ,並設置 Intent.FLAG_ACTIVITY_NO_ANIMATION,當activity1調用startActivity後,不調用自己的finish()方法,就不會有動畫,activity2就直接顯示出來了,感覺就像是直接替換了當前的UI。
但如果activity1調用finish()方法後,不管設不設Intent.FLAG_ACTIVITY_NO_ANIMATION,都會有刷屏的動畫。

       解決辦法:最後在finish()後加上,overridePendingTransition(0, 0)。


十四、測量代碼片段運行時間

<span style="font-size:18px;">方法一
//僞代碼
long startTime=System.currentTimeMillis();   //獲取開始時間
doSomeThing();  //測試的代碼段
long endTime=System.currentTimeMillis(); //獲取結束時間
System.out.println("程序運行時間: "+(endTime-startTime)+"ms");
方法二
//僞代碼
long startTime=System.nanoTime();   //獲取開始時間
doSomeThing();  //測試的代碼段
long endTime=System.nanoTime(); //獲取結束時間
System.out.println("程序運行時間: "+(endTime-startTime)+"ns");</span>

十五、Cound not find class ‘xxxxxx’referenced from method 'xxxxx '

導入包的順序問題,在javabuildpath裏面更改導入包的順序,引用的類所屬的包必須在被引用之前

調整順序即可



十六:無法使用Junit

# Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode windows-x86)

  # An error report file with more information is saved as:

  # E:\Mydoc\EclipseWorkspace\TestAndroid\hs_err_pid4900.log

  #

  # If you would like to submit a bug report, please visit:

  #http://java.sun.com/webapps/bugreport/crash.jsp

  #

這個問題因爲android沒有集成Junit,所以無法使用Junit的方式進行調試,android自有一套體系,Junit的TestCase只是作爲基類來用,具體請學習AndroidTestCase等相關的知識


十七:Application does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner

運行android junit遇到的問題

必須在AndroidManifest.xml中添加


 <uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
    <instrumentation
         android:name="android.test.InstrumentationTestRunner"
         android:targetPackage="com.sdiciot.alarmclient" />

和在<application>標籤內添加<uses-library android:name="android.test.runner" />


十八、導入未編譯的jni工程

如果直接運行會報錯,如下圖



需要項目右鍵->Android Tools-> Add Native Support,然後再運行就ok了


十九、can not load library


在jni/Application中 設置屬性爲APP_STL :=  gnustl_static 

靜態就ok了


二十、java.lang.UnsatisfiedLinkError: Native method not found

這個原因可以有很多,可以自行google,我貼出我的錯誤


可以看到虛擬機報出沒有實現這個函數,其實我是寫了c文件的,但是沒有在Android.mk中註冊

需要在LOCAL_SRC_FILES := xxxx.cpp \ 纔可以

看全Log的重要性。。。


未完待續。。。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章