a android.view.view.setbackgroundresource(int)' null object on referen void_[随时更新][Android]小问题记录...

此文随时更新,旨在记录平时遇到的不值得单独写博客记录的细节问题,当然如果问题有拓展将会另外写博客。

1.xml中layout_gravity,gravity的区别。

layout_gravity表示控件自身位置,gravity表示子组件的位置。

2.加imageView后提示Warning:Missing content Description attribute on image

Resolved this warning by setting attribute android:contentDescription for my ImageView

android:contentDescription="@string/desc"

Android Lint support in ADT 16 throws this warning to ensure that image widgets provide a contentDescription.

This defines text that briefly describes content of the view. This property is used primarily for accessibility. Since some views do not have textual representation this attribute can be used for providing such.

Non-textual widgets like ImageViews and ImageButtons should use the contentDescription attribute to specify a textual description of the widget such that screen readers and other accessibility tools can adequately describe the user interface.

http://stackoverflow.com/questions/8500544/android-lint-contentdescription-warning

3.eclipse编译时长时间refreshing external folders导致等待时间过长

选择project->properties

选择Java build path->libraries->android.jar->source attachment,remove掉Source attachment中的路径

4.如何往 AlertDialog 的 title 上增加按钮?

不能加按钮之类的控件,title 只能设置内容,颜色,背景。如果想加?那就这样:仿照你需要的效果画一个 layout 出来,然后写一个类A inflate 这个 layout,之后再定义一个 AlertDialog ,

用 builder.setView(A.getViewGroup) 就可以了,最后 show() 出来。提醒一句,不要 builder.setTitle() 了,不设置 title 的内容 AlertDialog 就不会显示 title 栏,下面的三个按钮也一样。

下面放个效果图: (找时间再专门写一个小demo吧)

5.使用 GridView 如何获取 item 单击事件?

mGridShowThumbnail.setOnItemClickListener(newOnItemClickListener() {

@Overridepublic void onItemClick(AdapterView> parent, View view, int position, longid) {//TODO Auto-generated method stub

}

});

position 就是单击的 item 的序号

但是如果 item 是组合控件,比如说 imageview 和 checkbox ,要实现单击两个控件实现不同效果,这样就不可以,需要对 item 的两个控件分别写各自的事件。

6.Android 项目 clean 后 R 文件不生成?

一般 clean 后重新 build 会自动生成 R 文件,没生成基本上是 xml 文件有错误。把错误改一下就好。

7.android:layout_toLeftOf=“@id/x”

这个id一定要在这个控件之前声明,不能写在后面,否则会报错。(我是这么觉得。。)

8.将应用程序最小化

mActivity.moveTaskToBack(false);

boolean android.app.Activity.moveTaskToBack(booleannonRoot)public boolean moveTaskToBack (booleannonRoot)

Added in API level1Move the task containingthis activity to the back of the activity stack. The activity's order within the task is unchanged.

Parameters

nonRoot Iffalse then this only works if the activity is the root of a task; if true it will work forany activity in a task.

Returns

If the task was moved (or it was already at the back)true is returned, else false.

9.最小化后在 serivce 里再将其恢复窗口

Intent intent = new Intent(getBaseContext(), MainActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

getApplication().startActivity(intent);

10.设置背景用setbackgroundXXX,那去掉背景用什么,没有相关的remove方法啊?

mLayout.setBackgroundColor(getResources().getColor(R.color.pic_bg)); //设置背景色要注意写法

mLayout.setBackgroundResource(0); //去掉背景

关于去掉背景,参见文档

voidandroid.view.View.setBackgroundResource(int resid)

public voidsetBackgroundResource(int resid)

Added inAPI level 1

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

11.刷新GridView的内容

mAdapter.notifyDataSetChanged();

voidandroid.widget.BaseAdapter.notifyDataSetChanged()

public voidnotifyDataSetChanged()

Added inAPI level 1

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

12.DDMS下使用 Traceview 查看程序运行时间 (Android2.2+)

1,在设备表中选中你想进行method trace的进程。

2,单击Start Method Profiling按钮开始method trace。

3,method trace进行中。

4,单击Stop Method Profiling按钮停止method trace。紧接着系统会自动弹出Traceview窗口来显示刚才的method trace结果。

13.如何获取程序从后台回到前台的状态信息来做响应事件?

这种情况基本上都用到service,从前台转到后台,然后在service中做一些处理,然后程序回到前台后获得到前台的信息,做关闭service等处理。

我是用到了截屏功能:程序转到后台,service开启浮动按钮来自由截屏,如果截屏期间再点到原来的程序,就要获取程序回到前台这个状态,根据这个状态把service的按钮关掉。

处理这个的思路就是service开启后就每隔一段时间(1s-2s)做一次检查,检查系统启动的app队列,从队列中找到自己activity,然后对状态进行判断是在前台还是后台:

privateActivityManager mActivityManager;privateString mPackageName;private voidinit(){

mActivityManager=(ActivityManager) mNetworkService

.getSystemService(Context.ACTIVITY_SERVICE);

mPackageName=mNetworkService.getPackageName();new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

try{while (true) {

Thread.sleep(1000);if(isAppOnForeGround()) {

mScreenHandler.sendEmptyMessage(SCREENSHOT_DONE+ 2);break;

}

}

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}).start();

}private booleanisAppOnForeGround() {

List runningAppProcesses =mActivityManager.getRunningAppProcesses();for(RunningAppProcessInfo processInf : runningAppProcesses) {if(processInf.processName.equals(mPackageName)) {if (processInf.importance ==RunningAppProcessInfo.IMPORTANCE_FOREGROUND)return true;else

return false;

}

}return false

14. java异常:ConcurrentModificationException

这个问题是说,你不能在对一个List进行遍历的时候将其中的元素删除掉

解决办法是,你可以先将要删除的元素用另一个list装起来,等遍历结束再remove掉

可以这样写

List delList= new ArrayList();//用来装需要删除的元素

for(Information ia:list)if(ia.getId()==k){

n++;

delList.add(ia);

}

list.removeAll(delList);//遍历完成后执行删除

15. DashPathEffect

android.graphics.DashPathEffect.DashPathEffect(float[] intervals, floatphase)

The intervals array must contain an even number of entries (>=2), with the even indices specifying the "on"intervals,

and the odd indices specifying the"off"intervals. phase is an offset into the intervals array (mod the sum of all of the intervals).

The intervals array controls the length of the dashes. The paint's strokeWidth controls the thickness of the dashes.

Note: this patheffect only affects drawing with the paint's style is set to STROKE or STROKE_AND_FILL.

It is ignored if the drawing is done with style ==FILL.

Parameters:

intervals array of ON and OFF distances

phase offset into the intervals array

参数间隔数组(条目数>=2)必须包含一个偶数,其中偶数列代表“打开”间隔,奇数列代表“关闭”间隔。

参数相位是间隔数组内的偏移(对所有间隔之和取模)。间隔数组控制连接符的长度。Paint的边宽度控制连接符的宽度。

大概用法://draw dotted line

paint.setStyle(Paint.Style.STROKE);

paint.setColor(Color.GRAY);

Rect rcDot= newRect();

rcDot.left= 20;

rcDot.top= 20;

rcDot.right= bkWidth - 20;

rcDot.bottom= bkHeight - 20;

PathEffect effects= new DashPathEffect(new float[] { 10, 5 }, 1);

paint.setPathEffect(effects);

canvas.drawRect(rcDot, paint);

间隔数组是{10, 5} 效果为 —— —— —— —— —— 即实线和空白为2:1的关系

间隔数组是{10, 5, 3, 2} 效果为 ————— —— ————— —— 大概就是这个效果

16. popupwindow的小问题

mPopupMenu = new PopupWindow(mPopupMenuView, 220,

ViewGroup.LayoutParams.WRAP_CONTENT);

mPopupMenu.setOutsideTouchable(true);

mPopupMenu.setBackgroundDrawable(newBitmapDrawable());

mPopupMenu.setOnDismissListener(newPopupWindow.OnDismissListener() {

@Overridepublic voidonDismiss() {//TODO Auto-generated method stub

mBtnMore.setChecked(false);

}

});

写了个菜单,点击按钮弹出。希望的效果是点击菜单外部时菜单消失,同时改变按钮状态;点击按钮时,如果菜单弹出过了,也改变按钮的状态并且让菜单消失。

但无情的是,这样定义的popupwindow在显示时,我如果再点击按钮,首先调用onDismissListener,然后才是按钮事件,一直达不到我的效果。

然后我考虑,在popupwindow显示的时候点击按钮,不再触发按钮事件,只触发OnDismissListener,在查看popupwindow方法,试了试之后,发现需要定义下面的方法:

mPopupMenu.setFocusable(true);

这样在popupwindow显示时,点击外部,不会触发外部的事件,只是关闭了弹出的对话框。

下面是方法文档:void android.widget.PopupWindow.setFocusable(booleanfocusable)

Changes the focusability of the popup window. When focusable, the window will grab the focus from the current focused widgetif the popup contains

a focusable android.view.View. By defaulta popup window is not focusable.

If the popup is showing, callingthismethod will take effect only the next time the popup is shown or through a manual call to one of the update()

methods.

Parameters:

focusabletrue if the popup should grab focus, false otherwise.

17. The jar of this class file belongs to container 'Android Private Libraries' which does not allow modifications to source attachments on its entries.

查看外部依赖jar包的源代码

这里假设你已经把外部依赖的jar包正确导入并且编译通过了,但是ctrl+鼠标左键想看代码或者想看其注释却不可行的情况,

首先在libs下面找到引入的包,这里以android-support-v4.jar为例,

在libs下新建android-support-v4.jar.properties文件,即与引用的jar包同名最后加上.properties的文件

其内容如下:

src=E:\\eclipse\\sdk\\extras\\android\\support\\v4\\src

这里路径要根据你自己的路径进行调整,配置之后退出eclipse,在进入,就可以正常查看了。

参考:Android开发技巧之查看外部依赖jar的源码_android private libralies does not allow modifications to source

18. 带有默认文字的edittext,让光标默认在文字后方

mEtName.setText(strAsName);

mEtName.setSelection(strAsName.length());

19.如何获取Android设备的顶部状态栏和底部工具栏的高度

20.如何设置AlertDialog的按钮的可用状态

21. Android文件名长度有限制么?

Android下文件名最长只能支持127个中文,不超过255个字符

路径也算的,即使在windows下也是要算路径的

22. 获取资源文件里的bitmap

BitmapDrawable draw=(BitmapDrawable)getResources().getDrawable(R.drawable.bitmap);

Bitmap m=draw.getBitmap();

23. Android设置屏幕方向

//自动转屏

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);//设置横向

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);//设置纵向

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

24. 弹出有输入框的对话框,让软键盘压缩对话框显示

dialog.getWindow().setSoftInputMode(

WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

dialog.show();

对于对话框,加上scrollview使显示良好

android:id="@+id/scrollView1"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" >

25. ubuntu不识别android设备

lsusb 查看设备信息

id信息添加到/etc/udev/rules.d/51-android.rules

再/etc/init.d/udev restart

最后sdk工具目录下sudo ./adb kill-serversudo ./adb devices

26. 查看指定文件夹下某文件数量

File folder = newFile(Path);

File[] files=folder.listFiles();for (int i = 0; i < files.length; ++i) {

File file=files[i];if(file.isFile()) {

String[] str= file.getPath().split("\\.");if (str[1].equals("pdf")) {

mStrNum++;

}

}else

continue;

}

其余相关获取路径/操作路径的方法

27. 软键盘弹出对话框压缩显示

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

28.对话框监听返回事件

dialog.setOnKeyListener(newOnKeyListener() {

@Overridepublic boolean onKey(DialogInterface dialog, intkeyCode,

KeyEvent event) {//TODO Auto-generated method stub

if (keyCode ==KeyEvent.KEYCODE_BACK) {

dialog.dismiss();return true;

}else

return false;

}

});

29.删除系统内置程序

有的设备不能root,要删除内置一些程序,用下面的方法:

adb shellsu

mount -o remount,rw rootfs /system/cd/system/appls

rm XXXX.apk

30. adb: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

sudo apt-get install lib32stdc++6

31. linux 几个 path

.bashrc

.profile/etc/profile

32. ubuntu 下不识别 android 设备:

33. Android资源字符串内有变量

我叫%1$s,我来自%2$s

我%1$d岁了

String sFormat =getResources().getString(R.string.alert);

String sFormat1=getResources().getString(R.string.old);

String sFinal= String.format(sFormat, "张三","上海");

String sFinal1= String.format(sFormat1, 23);

34. ubuntu 关闭/打开笔记本触摸板

#closesudormmod psmouse

#opensudo modprobe psmouse

35. 获取手指滑动阈值

这个值可以自己定,不过看到一个方法,获得的值为8pix,应该是个标准

//Distance in pixels a touch can wander before we think the user is scrolling//int 相当于一个手指滑动的阈值

ViewConfiguration.get(getContext()).getScaledTouchSlop();

36. 获取LayoutInflater

LayoutInflater inflater = LayoutInflater.context(this);

LayoutInflater inflater=getLayoutInflater();

LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

37. wifi 密码路径 data/misc/wifi/wpa_supplicant.conf

38. TextView加下划线

mTvAddmore.setText(Html.fromHtml("" + mStrAddMore + ""));

39. RadioButton / CheckBox 按钮显示在文字后面

android:button="@null"

android:drawableRight="@android:drawable/btn_radio"

40. RadioButton / CheckBox 等设置系统风格

mCbAuto =(CheckBox) mView.findViewById(R.id.sort_auto);int id_cb = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");

mCbAuto.setButtonDrawable(id_cb);

or

android:drawableRight="@drawable/btn_check_holo_light"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值