Android Tips1

分类:androidpaper 178人阅读 评论(0) 收藏 举报

目录(?)[+]

有些是转的。

1.new Handler()不能在run()中执行
Can't create handler inside thread that has not called Looper.prepare() 原因:

在asyncTask的doInBackground()中,或Runable的run()中,或TimerTask的run()中

new Handler()就会失败。


2.TimerTask取消后就不能再用了

java.lang.IllegalStateException: TimerTask is scheduled already

当cancle掉一个TimerTask,这个Task就不能再用了。必须重新new一个。

nextStepTask = new NextStepTimerTask();
nextStepTimer.schedule(nextStepTask, 0, 1000);

3.加载字符串到String

WebpageActivity.this.getResources().getText(R.string.search_page_test).toString()

4.startActivity

很多人使用startActivity时候,会碰到如下的异常:

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。

  1. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

5.android sdcard read-only file system 的解决办法

【解决方法】

在cmd窗口一条命令就可以了:
adb shell
mount -o remount rw /

//然后mkdir aaa试一下ok


LayoutInflater的构造

[java] view plain copy
  1. /**
  2. *ObtainstheLayoutInflaterfromthegivencontext.
  3. */
  4. publicstaticLayoutInflaterfrom(Contextcontext){
  5. LayoutInflaterLayoutInflater=
  6. (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  7. if(LayoutInflater==null){
  8. thrownewAssertionError("LayoutInflaternotfound.");
  9. }
  10. returnLayoutInflater;
  11. }


s

TextView

style = "@android:style/TextAppearance.StatusBar.EventContent"//from api 9
android:textAppearance="?android:attr/textAppearanceSmall"

判断版本
[java] view plain copy
  1. if(android.os.Build.VERSION.SDK_INT<=android.os.Build.VERSION_CODES.FROYO)
  2. <spanstyle="white-space:pre"></span>//这是小于等于2.2(api<=8)

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. 箭头函数的基础使用
  3. NPM 和webpack 的基础使用
  4. Python list sort方法的具体使用
  5. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  6. python list.sort()根据多个关键字排序的方法实现
  7. Android(安卓)中Observer模式的使用
  8. Android百度地图一种简单实现多标注及响应时间的方法
  9. android include使用

随机推荐

  1. Android Studio 第五十三期 - 自定义Edit
  2. [android]编译时出现/usr/bin/ld: skippi
  3. 2013.01.03 (2)——— android开发实例之仿
  4. 关于标准android中视频播放器中的快进和
  5. 仿照Android的池化技术
  6. Android Bluetooth Code
  7. 运行时改变Button图片的android:drawable
  8. Android(安卓)AM命令行启动程序的方法
  9. android 状态栏移动到底部
  10. 网络时间同步