这篇博客是用来记录自己在写代码的过程中遇到的一些问题,以及解决方法,做一个总结,算是笔记吧。


1.问题描述:

以某一触发唤醒屏幕

解决方案:

public static void wakeUpAndUnlock(Context context){        KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");                // 解锁        kl.disableKeyguard();                // 获取电源管理器对象        PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);                // 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");                // 点亮屏幕        wl.acquire();                // 释放        wl.release();    }
注:其中,解锁步骤可以省略。这种情况下,就只是点亮你的屏幕而不会解锁了。


2.问题描述:

除掉Actionbar最左侧的箭头

注意,这个最左侧的的箭头可不是actionbar的icon。如果你去设置它的icon,这样就会导致既有icon也有箭头。

解决方案:

getActionBar().setHomeAsUpIndicator(drawable);
上面的这种方法是适用于API18,而对于API小于18的代码则是不适用的。需要做更多的逻辑。如下:

public static void changeActionBarHomeUpDrawable(Activity activity, int rid) {        Drawable homeUp = activity.getResources().getDrawable(rid);        final View home = activity.findViewById(android.R.id.home);        if (home == null) {            // Action bar doesn't have a known configuration, an OEM messed with things.            return;        }        final ViewGroup parent = (ViewGroup) home.getParent();        final int childCount = parent.getChildCount();        if (childCount != 2) {            // No idea which one will be the right one, an OEM messed with things.            return;        }        final View first = parent.getChildAt(0);        final View second = parent.getChildAt(1);        final View up = first.getId() == android.R.id.home ? second : first;        if (up instanceof ImageView) {            ImageView upIndicatorView = (ImageView) up;            upIndicatorView.setImageDrawable(homeUp);        }    }


3.问题描述:

Android Private Libraries缺失

如果你的Android Private Libraries是你误删除的话,你可以通过下面的方法1恢复。如果你的项目无原无故的缺失了Android Private Libraries,那么你可以使用下面的方法2修复。

1. 在项目上点击右键,点击Android Tools -> Fix Project Properties 即可
2. android-support-v4重复,删除一个即可


4.问题描述:

Arrays.copyOfRange在低版本的Android中出现NoSuchMethodError异常

对于这个问题的解释,可能要归结于API版本之间的兼容性了。

解决方案:

我的解决方法就是直接重写了这个方法。非常简单粗暴的一个方法,而且很有效。下面是对于Array的一些常用方法的示例源码:

http://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/ArrayUtils.html


5.问题描述:

ListView与动态显示的底部操作栏

你是否遇到过这样的一个问题:你有一个始终位于屏幕底部的Layout,还有一个ListView,它是始终位于屏幕的顶部。当ListView的item过多时,下面的部分item就会被拦住,或是把底部的Layout拦住。

解决方案(一):

对于上面的问题,可能你已经尝试过,使用让Listview位于Layout上方,或是ListView同时位于屏幕顶部和Layout上方。可是尝试过后就会是沮丧的。下面我就分享一个我的技巧:在Layout的外部再套一层Layout,高度为wrap_content。如下:

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_above="@+id/container_relativeLayout"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true" >    </ListView>    <RelativeLayout        android:id="@+id/container_relativeLayout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center_horizontal" >            <TextView                android:id="@+id/textView1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Bottom Layout" />        </LinearLayout>    </RelativeLayout></RelativeLayout>

  

注:可能你会觉得这样是多此一举。因为我可以把ListView的高度设置成match_parent或是fill_parent,再位于Layout之上。这种方式只是适合于,Layout不会被Gone的情况。这里是一个值得注意的地方。

解决方案(二):

可能你会觉得方案一太过繁琐了,那么你可以使用Relayout的另一个属性:layout_alignWithParentIfMissing

android:layout_alignWithParentIfMissing="true"


6.问题描述:

has leaked window com.android.internal.policy.impl.PhoneWindow$ that was originally added here

大致的意思是存在窗口句柄泄露,即未能及时销毁某个PhoneWindow。其实存在这么一种情况,即因我们在非主线程中的某些操作不当而产生了一个严重的异常,从而强制当前Activity被关闭。而在关闭的同时,却没能及时的调用dismiss来解除对ProgressDialog等的引用,从而系统抛出了标题中的错误,而掩盖了真正导致这个错误的异常信息。

解决方案:

重写Activity/Fragment的onDestroy方法,在方法中调用dismiss来解除对ProgressDialog等的引用。


7.问题描述:

Android判断是否能真正上网

普通方法不能判断外网的网络是否连接,比如连接上局域网

解决方案:

public static final boolean ping() {String result = null;try {String ip = "www.baidu.com"; // ping 的地址,可以换成任何一种可靠的外网Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip); // ping网址3次// 读取ping的内容,可以不加InputStream input = p.getInputStream();BufferedReader in = new BufferedReader(new InputStreamReader(input));StringBuffer stringBuffer = new StringBuffer();String content = "";while ((content = in.readLine()) != null) {stringBuffer.append(content);}// ping的状态int status = p.waitFor();if (status == 0) {result = "success";return true;} else {result = "failed";}} catch (IOException e) {result = "IOException";} catch (InterruptedException e) {result = "InterruptedException";} finally {Log.d("----result---", "result = " + result);}return false;}


8.问题描述:

获取手机号码

解决方案:

private String getPhoneNumber() {        TelephonyManager telephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);         return telephonyMgr.getLine1Number();     }
添加权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />


9.问题描述:

lostserialVersionUID

在开发过程中,有时我们会遇到“The serializable class CommonException does not declare a static final serialVersionUID field of type long”这样的一个报警异常。类似下面这样的:


我们需要这个值的原因是我们要把这个值永久地保存在一个磁盘上,通常是一个文件中。还有一点是我们要把这个对象在网络中传输。

解决方案:

add上面的3 quick fixes available中的一个。通常是去add第二个。


10.问题描述:

如何隐藏底部状态栏(System Bar)

底部导航栏如下:


隐藏方式:

在onCreate中添加如下代码:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

11.问题描述:

获得屏幕在旋转过程中的旋转角度

这里我们获得的不只是横屏还是竖屏的问题。而是更细一点的区分,比如横屏中的是和左旋转了90度,还是向右旋转了90.解决方案如下:

WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);mRotation = manager.getDefaultDisplay().getRotation();

12.问题描述:

Installation error: INSTALL_FAILED_VERSION_DOWNGRADE


安装新的Apk时,出现以上报错信息。这是是因为Manifest中的android:versionCode属性,手机里APP的versionCode高于将要安装的APP。

解决方案:

1.卸载原来的Apk

2.将当前的Apk的versionCode属性值修改到比手机中Apk的versionCode属性值大即可。



大家还可以参见一些Android其他的细节总结

Android细节问题总结(一)

更多相关文章

  1. 高级Redis应用进阶课 一站式Redis解决方案
  2. Android(安卓)Java笔试题(带大部分答案)
  3. Android基于ViewPager+Fragment实现左右滑屏效果的方法
  4. android调用matlab中的函数方法
  5. Android媒体扫描代码分析
  6. Android中banner的使用步骤
  7. Android(安卓)中Edittext设置不弹出软键盘
  8. Android(安卓)SDK1.5 模拟器 快捷键 (模拟 屏幕旋转 轨迹球等。
  9. UI布局之selector layer-list

随机推荐

  1. 【OCP最新题库解析(052)--题3】Which two
  2. 云管平台监控实践分享 | 周末送资料
  3. 什么是爬虫?Python爬虫工作需要掌握哪些技
  4. WebAssembly入门课
  5. Vue3.0高阶实战:开发高质量音乐Web app
  6. CSS进阶知识flex弹性布局容器与项目
  7. Vue3.0高阶实战:开发高质量音乐Web app
  8. MySQL临时表
  9. kubernetes-ingress 安装
  10. 如何快速删除卸载残余-注册表