查看: 4682|回复: 0

[经验] Android常见错误汇总

[复制链接]
  • TA的每日心情
    郁闷
    2021-10-6 14:33
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2021-9-27 09:49:59 | 显示全部楼层 |阅读模式
    分享到:

    1.R.java消失或解析异常
    查看res中资源文件,图片,xml等。比如图片文件名不能有大写不能有空格。
    搞定错误之后Project->clean就可以了。
    2.自定义title栏。
    首先要z在values->styles中定义一个style,然后在mainfest文件中设置android:theme.
    最后在Activity中按照这个顺序写:
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_layout);
    3.SQLite isFirst和isBeforeFirst方法的区别:
    看下面一段代码
    Cursor c = queryTheCursor(type);
    if(c.moveToLast())
    while (!c.isBeforeFirst()) {
    String tmpContent = new String();
    tmpContent = c.getString(c.getColumnIndex("content"));
    contents.add(tmpContent);
    c.moveToPrevious();
    }
    c.close();
    代码的作用是逆序输出表中的内容,第三行如果用的是isFirst()的话就无法输出第一行,正确做发是用isBeforeFirst()。
    4.eclipse删除空行
    在eclipse中删除某一行就用ctrl+D快捷键。如果你想删除一个文件中的所有空行呢。
    可以用下面方法。
    1)打开源码编辑器
    2)使用快捷键Ctrl+f
    3)在Find输入框中输入:^\s*\n
    4)Replace With输入框的值为空
    5)在【Options】选中的"Regular expressions"
    6)点击【Replace All】按钮。
    7)OK!
    5.getX()和getRawX()的区别
    getX()是表示Widget相对于自身左上角的x坐标
    而getRawX()是表示相对于屏幕左上角的x坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕),getY(),getRawY()一样的道理
    6.imagView居中显示问题
    xml设置如下:
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    androidrientation="vertical" >
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myImageView"
    android:layout_gravity="center"
    android:scaleType="matrix"
    android:src="@drawable/pic" >

    7.synchronized引发了java.util.ConcurrentModificationException
    如果多个线程同时访问一个 ArrayList 实例,而其中至少一个线程从结构上修改了列表,那么它必须 保持外部同步。
    解决方法:初始化的时候 将ArrayList改为CopyOnWriteArrayList;
    原理:
    JAVA中将引用从一个对象移到另外一个对象不需要消耗CPU时间,可以看做是一个原子操作。
    JAVA中如果一个对象仍旧存在引用,则不会被CG回收。
    CopyOnWriteArrayList就是根据以上两个特点,在列表有更新时直接将原有的列表复制一份,并再新的列表上进行更新操作,完成后再将引用 移到新的列表上。旧列表如果仍在使用中(比如遍历)则继续有效。如此一来就不会出现修改了正在使用的对象的情况(读和写分别发生在两个对象上),同时读操 作也不必等待写操作的完成,免去了锁的使用加快了读取速度。
    8.获取随机颜色
    并不用每次都生成三个随机数,下面两条语句就可以了:
    Random myRandom=new Random();
    int ranColor = 0xff000000 | mRandom.nextInt(0x00ffffff);
    10.去掉Activity的标题栏,全屏显示
    在manifest文件中修改对应的Avtivity属性。
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    11.如何修改应用名称及应用图标
    修改程序的图标,修改drawable文件夹的i→→c_launcher.png图标,把新的图标改名覆盖就可以了。
    如果你要自己的名称,可以修改AndroidManifest.xml的这个节点,application android:icon="@drawable/ic_launcher",不需要加文件扩展名。
    即使这么做了,真机调试的时候可能还是会有一些问题,比如图标没办法改变,这个时候就需要在Eclipse中新建一个不同名的项目,然后转移代码(有点小麻烦~_~!)。
    12.关于调试方法
    调试的时候程序如果出错,一般是查看logcat,看error发生的地方,会提示在程序的第几行,然后去找就可以了。
    但有些错误没办法定位,那就把日志输出成txt,然后去google,baidu吧。
    13.Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
    错误1:请求的字段在数据库的表中不存在,一般是大小写没写对;
    错误2:编程的中途改变表的字段,实际字段并没有改变,解决方法是卸载当前版本,再安装调试。
    14.android.content.res.Resources.loadXmlResourceParser
    在传递string类做参数的地方传了int形变量。
    15.android.content.res.Resources$NotFoundException
    出现此类异常时,可以根据 Resource ID到资源类R中找相关的资源。比如0x7f030000,对应的是city_item布局文件,就可以将问题缩小到更小的范围。对于这类运行时找不到资 源,但资源又确实存在的问题,可能的编译打包时出现问题,没有将该资源加入。可修改一下该资源,让编译器重新编译。
    还有试一下Project ->Clean一下这个项目 也可以的。
    16.交互性的button定义的方法:
    首先是准备好按钮不同状态的图片
    然后 在res/drawable中定义selector的xml文件
    最后Button的background属性中设置
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/addbtn_selector"/>
    17在超级终端中执行程序报错-Permission deny
    参照http://android.stackexchange.com ... fo-on-error-message
    主要原因是不能在sdcard中执行,直接进入data/目录下面创建文件,然后执行就可以了。
    18.从svn导入工程项目有惊叹号
    错误提示Archive for required library: 'libs/armeabi/libvudroid.so' in project 'DocumentViewer' cannot be read or is not a valid ZIP file
    主要是路径出了问题
    解决方法:在project的build-path将外部包(库)的引用删掉就可以了。
    19.首次进入带有EditText的Activity不自动弹出软键盘,再次点击才弹出。
    只有设置manifest的方法有用,在activity的设置中添加:
    [html] view plaincopyprint?
    android:windowSoftInputMode="adjustPan|stateHidden"
    20.Gallery中OnItemClickListener与OnItemSelectedListener的区别
    OnItemClickListener:只有单击Gallery中的View才会触发事件,准确的说是当点击之后抬起手的时候触发,滑动不会触发。
    OnItemSelectedListener:当Gallery中的View被选中的时候就会触发,Galler初次显示就会触发一次,选中第一个iew,滑动和单击都会触发。
    20.从16进制中提取颜色的rgb分量。
    主要就是通过位运算来实现。
    [java] view plaincopyprint?
    public class Main {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int INK_COLOR = 0xFF11ef23;
    float r = getColorR(INK_COLOR );
    float g = getColorG(INK_COLOR );
    float b = getColorB(INK_COLOR );
    System.out.print(r+" "+g+" "+b);
    }
    public static float getColorR(int c)
    {
    int R = (c & 0x00FF0000 )>>16;
    return (float) (R/255.0);
    }
    public static float getColorG(int c)
    {
    int G =(c & 0x0000FF00 )>>8;
    return (float) (G/255.0);
    }
    public static float getColorB(int c)
    {
    int B = c & 0x000000FF;
    return (float) (B/255.0);
    }
    }
    21. Eclipse中签名导出apk崩溃,手动签名。
    工程没问题,调试也没问题,但打包的时候eclipse会崩溃,解决方法是手动打包。
    首先去工程目录下的bin文件夹下找到apk文件,解压后删除META-INF文件夹,重新打包成压缩包,改后缀名为.apk
    首先是签名(假设你已经在根目录下生产了密钥keystore):
    进入java安装目录/bin文件夹下:
    [plain] view plaincopyprint?
    ./jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore android.keystore ~/Output.apk android
    然后是优化,进入sdk的tools文件夹下,运行。
    [plain] view plaincopyprint?
    ./zipalign -v 4 ~/Output.apk Output_realase.apk
    当前目录下Output_realase.apk就是打包签名好的apk了。
    22.android.view.InflateException: Binary XML file line #异常的解决
    创建自定义view的时候,碰到 android.view.InflateException: Binary XML file line #异常,反复研究
    后发现是缺少一个构造器造成。
    [java] view plaincopyprint?
    public MyView(Context context,AttributeSet paramAttributeSet)
    {
    super(context,paramAttributeSet);
    }
    补齐这个构造器,异常就消失了.
    23.将assets文件夹中的压缩包拷贝到sdcard中(不限大小)
    [java] view plaincopyprint?
    public static void copyAssetToSdcard(Context c, String assetFile, String destination) throws IOException {
    InputStream in = c.getAssets().open(assetFile);
    File outFile = new File(destination);
    OutputStream out;
    Log.v("Try", "Try coping.");
    try {
    if (!(new File(destination)).exists()) {
    Log.v("Try", "Not exists..");
    out = new FileOutputStream(outFile);
    copyFile(in, out);
    in.close();
    in = null;
    out.flush();
    out.close();
    out = null;
    }
    } catch (Exception e) {
    Log.v("Error", "Error in if。");
    }
    }
    public static void copyFile(InputStream in, OutputStream out) throws IOException {
    Log.v("Coping", "copyFiling.");
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
    Log.v("read:", "" + read);
    out.write(buffer, 0, read);
    }
    }
    24.判断是否有root权限
    [java] view plaincopyprint?
    public static synchronized boolean getRootAhth()
    {
    Process process = null;
    DataOutputStream os = null;
    try
    {
    process = Runtime.getRuntime().exec("su");
    os = new DataOutputStream(process.getOutputStream());
    os.writeBytes("exit\n");
    os.flush();
    int exitValue = process.waitFor();
    if (exitValue == 0)
    {
    return true;
    } else
    {
    return false;
    }
    } catch (Exception e)
    {
    Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "
    + e.getMessage());
    return false;
    } finally
    {
    try
    {
    if (os != null)
    {
    os.close();
    }
    process.destroy();
    } catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    25.最简单的Root 模拟器的方法
    启动一个模拟器,开始-运行-输入cmd,打开dos,依次输入
    adb shell
    mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
    cd /system/bin
    cat sh > su
    chmod 4755 su
    su
    即可获得root权限

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-3-29 14:30 , Processed in 0.103613 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.