安卓经验收集(不断更新)

一、如何让对话框在任意位置显示

在地图上的时候通常点击哪里就会在哪里显示一个提示框,在安卓实现的时候就先要获得该座标,并且放入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的重要性。。。


未完待续。。。


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