Android开发问题怎么解决?

如题所述

    R.java消失或解析异常

    自定义title栏

    SQLite isFirst和isBeforeFirst方法的区别

    eclipse删除空行

    getX()和getRawX()的区别

    imagView居中显示问题

    synchronized引发了 java.util.ConcurrentModificationException

    获取随机颜色

    去掉Activity的标题栏,全屏显示

    如何修改应用名称及应用图标

    关于调试方法

    Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

    android.content.res.Resources.loadXmlResourceParser

    android.content.res.Resources$NotFoundException

    交互性的button定义的方法

    在超级终端中执行程序报错-Permission deny

    从svn导入工程项目有惊叹号

    从svn导入工程项目有惊叹号

    首次进入带有EditText的Activity不自动弹出软键盘,再次点击才弹

    Gallery中OnItemClickListener与OnItemSelectedListener的区别

    Eclipse中签名导出apk崩溃,手动签名

    android.view.InflateException: Binary XML file line #异常的解决

    将assets文件夹中的压缩包拷贝到sdcard中(不限大小)

    判断是否有root权限

    最简单的Root 模拟器的方法

    新版ADT开启eclipse提示 "Running Android Lint" has encountered a problem

    新版ADT开启eclipse提示 cannot open libstdc++.so.6..

    无法升级ADT

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设置如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<ImageView
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" >
</ImageView>
</LinearLayout>

7.synchronized引发了 java.util.ConcurrentModificationException
如果多个线程同时访问一个 ArrayList 实例,而其中至少一个线程从结构上修改了列表,那么它必须 保持外部同步。
解决方法:初始化的时候 将ArrayList改为CopyOnWriteArrayList;
原理:
JAVA中将引用从一个对象移到另外一个对象不需要消耗CPU时间,可以看做是一个原子操作。
JAVA中如果一个对象仍旧存在引用,则不会被CG回收。      
CopyOnWriteArrayList就是根据以上两个特点,在列表有更新时直接将原有的列表复制一份,并再新的列表上进行更新操作,完成后再将引用移到新的列表上。旧列表如果仍在使用中(比如遍历)则继续有效。如此一来就不会出现修改了正在使用的对象的情况(读和写分别发生在两个对象上),同时读操作也不必等待写操作的完成,免去了锁的使用加快了读取速度。

8.获取随机颜色
并不用每次都生成三个随机数,下面两条语句就可以了:
Random myRandom=new Random();
int ranColor = 0xff000000 | mRandom.nextInt(0x00ffffff);

9.去掉Activity的标题栏,全屏显示
在manifest文件中修改对应的Avtivity属性。
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

10.如何修改应用名称及应用图标
修改程序的图标,修改drawable文件夹的i→→c_launcher.png图标,把新的图标改名覆盖就可以了。
如果你要自己的名称,可以修改AndroidManifest.xml的这个节点,application android:icon="@drawable/ic_launcher",不需要加文件扩展名。
即使这么做了,真机调试的时候可能还是会有一些问题,比如图标没办法改变,这个时候就需要在Eclipse中新建一个不同名的项目,然后转移代码(有点小麻烦~_~!)。

11.关于调试方法

调试的时候程序如果出错,一般是查看logcat,看error发生的地方,会提示在程序的第几行,然后去找就可以了。

但有些错误没办法定位,那就把日志输出成txt,然后去google,baidu吧。

12.Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
错误1:请求的字段在数据库的表中不存在,一般是大小写没写对;
错误2:编程的中途改变表的字段,实际字段并没有改变,解决方法是卸载当前版本,再安装调试。

13.android.content.res.Resources.loadXmlResourceParser
在传递string类做参数的地方传了int形变量。

14.android.content.res.Resources$NotFoundException
出现此类异常时,可以根据 Resource ID到资源类R中找相关的资源。比如0x7f030000,对应的是city_item布局文件,就可以将问题缩小到更小的范围。对于这类运行时找不到资源,但资源又确实存在的问题,可能的编译打包时出现问题,没有将该资源加入。可修改一下该资源,让编译器重新编译。 
还有试一下Project ->Clean一下这个项目 也可以的。

15.交互性的button定义的方法:
首先是准备好按钮不同状态的图片
然后 在res/drawable中定义selector的xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定义按钮点击时的图片 -->
<item android:drawable="@drawable/addbtn_focus" android:state_pressed="true"/>
<!-- 定义按钮默认的图片 -->
<item android:drawable="@drawable/addbtn_lost"/>
</selector>
最后Button的background属性中设置
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:background="@drawable/addbtn_selector"/>

16.在超级终端中执行程序报错-Permission deny

参照http://android.stackexchange.com/questions/16814/terminal-permission-denied-need-more-info-on-error-message

主要原因是不能在sdcard中执行,直接进入data/目录下面创建文件,然后执行就可以了。

17.从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将外部包(库)的引用删掉就可以了。

18.首次进入带有EditText的Activity不自动弹出软键盘,再次点击才弹出。

只有设置manifest的方法有用,在activity的设置中添加:

[html] view plain copy

    android:windowSoftInputMode="adjustPan|stateHidden"

    19.Gallery中OnItemClickListener与OnItemSelectedListener的区别

    OnItemClickListener:只有单击Gallery中的View才会触发事件,准确的说是当点击之后抬起手的时候触发,滑动不会触发。

    OnItemSelectedListener:当Gallery中的View被选中的时候就会触发,Galler初次显示就会触发一次,选中第一个iew,滑动和单击都会触发。

    20.从16进制中提取颜色的rgb分量。

    主要就是通过位运算来实现。

    [java] view plain copy

    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 plain copy

    ./jarsigner  -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore android.keystore ~/Output.apk android  

    然后是优化,进入sdk的tools文件夹下,运行。

    [plain] view plain copy

    ./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 plain copy

    public MyView(Context context,AttributeSet paramAttributeSet)  

    {  

    super(context,paramAttributeSet);  

    }  

    补齐这个构造器,异常就消失了.

    23.将assets文件夹中的压缩包拷贝到sdcard中(不限大小)

    [java] view plain copy

    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 plain copy

    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权限

    26.新版ADT开启eclipse提示 "Running Android Lint" has encountered a problem

    进Eclipse,Window > Preferences > Android > Lint Error Checking, u去掉 "When saving files check for errors"的勾。

    27. 新版ADT开启eclipse提示 cannot open libstdc++.so.6..

    要安装32位的库。

    sudo apt-get install lib32stdc++6
    sudo apt-get install lib32z1

    28.无法升级ADT

    I want to install ADT plugin in Eclipse with Zip file but when i click on next button in first page of install, the progress late long time. I wait around 3hours that i wait for install but now half ADT install :(
    In install window, above of progress bar, writes: cannot perform operation.Computing alternate solutions, may take a while: 7/15 .
    Why to install ADT needs a long time? Is this no problem or install has a problem?
    sorry for my poor english and Thanks for help

    由于官方对android开发的ide主推Android Studio,对eclipse也就少了很多,ADT-bundle也停止了更新,所以出现了上面的问题。

    解决方法是从Eclipse官网下载最新版本的Eclipse,最新版的ADT插件。


温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-01-09

随笔 - 534, 文章 - 0, 评论 - 147, 引用 - 0

Android开发环境搭建及常见问题解决方法

转自: http://www.cnblogs.com/rwxwsblog/p/4769785.html

在移动互联网的时代,Android的份额早已超过了苹果。Android的出现无疑加速了移动互联网的普及,手机操作系统已经是IOS和Android的天下了,什么winphone之类的可以说没它们什么事了。记得三年前做过一个简单的基于Android2.2的一个小应用。当时只是为了好玩,因而也没有相应的文档记录;后来由于工作原因也就没有继续讲Android开发进行下去。趁有时间再来折腾折腾Android的开发吧。想要开发Android的应用,首先需要安装Android的开发环境,下面记录了Android开发环境搭建的步骤。

一、环境及所需软件:

操作系统:win7 32位

下载Android Studio:http://developer.android.com/sdk/installing/index.html?pkg=studio(需要翻越GFW,你懂的)

下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html(1.7及以上版本)

二、安装步骤

1、安装java

在Android的官网看到最新版本的Android Studio需要jdk1.7及以上版本的支持。因此第一步是下载jkd。在http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html上找到相应版本的jdk下载即可

由于我的是32位的操作系统,因而下载的是jdk-8u60-windows-i586.exe。如果操作系统是64位的话,需要下载jdk-8u60-windows-x64.exe。下载完以后一步步默认安装即可。由于我之前配置过jre,所以需要改下javahome。

计算机--》右键,属性--》高级系统属性--》高级--》环境变量--》系统变量中添加java_home

到这一步java就算安装好了。

2、安装Android Studio和创建项目

双击android-studio-bundle-141.2178183-windows.exe一步步安装即可。没什么特别的,由于我采用的是在线安装的方式。因而首次启动的时候需要下载一些Android需要的组件,因而这里面需要翻越GFW,根据自己的情况设置即可(业内流行一句话叫做不会FQ的程序猿不是好程序猿)。安装完成后首次启动时界面如下。

选择第一项,Start a new Android Studio project。后面也是一步步按照要求填写即可。

此处选择手机和平板选项

最后选择Blank Activity即可

此时,一个Android项目也就创建完成了。

3、运行Android

由于我的机器配置比较低,而且运行Android的模拟器非常耗资源。而且第一次加载的时候需要初始化很多配置,因而第一次运行的时候速度和蜗牛没什么区别。有条件的哥哥姐姐能换mac还是早日换个mac吧。

在弹出的选择框中选择相应的设备即可。

至此,一切都还算顺利,然后运行模拟器的时候却出现了问题。

三、常见问题

1、未安装Intel HAXM

D:\Android\sdk\tools\emulator.exe -avd Nexus_5_API_23_x86 -netspeed full -netdelay none
emulator: ERROR: x86 emulation currently requires hardware acceleration!
Please ensure Intel HAXM is properly installed and usable.
CPU acceleration status: HAX kernel module is not installed!

异常的原因是Please ensure Intel HAXM is properly installed and usable.说明要安装Intel HAXM,于是搜罗了一番,发现Android sdk已经自带了Intel HAXM。于是找到sdk\extras\intel\Hardware_Accelerated_Execution_Manager目录下的intelhaxm-android.exe安装。

但是安装的时候却又出现了以下的问题。

于是网上搜罗了一番,再看看错误。说是Virtualization 没有开启,网上也有出现类似的情况,例如:http://www.cnblogs.com/csulennon/p/4178404.html这篇博客说的就是这个问题。于是重启电脑,设置Virtualization 为enable。

重启后可正常安装。安装后继续运行Android模拟器,却出现了另外一个异常。

2、模拟器RAM过大

D:\Android\sdk\tools\emulator.exe -avd Nexus_5_API_23_x86 -netspeed full -netdelay none
emulator: WARNING: Requested RAM size of 1536MB is too large for your environment, and is reduced to 1152MB.
emulator: device fd:596
HAXM is not working and emulator runs in emulation mode
emulator: The memory needed by this AVD exceeds the max specified in your HAXM configuration.
emulator: AVD      RAM size = 1152 MB
emulator: HAXM max RAM size = 1024 MB
emulator: You might want to adjust your AVD RAM size and/or HAXM configuration to run in fast virt mode.
Cannot set up guest memory 'pc.ram': Invalid argument
Error accepting connection, aborting

或者

D:\Android\sdk\tools\emulator.exe -avd Nexus_5_API_23_x86 -netspeed full -netdelay none
emulator: device fd:584
HAXM is working and emulator runs in fast virt mode
Cannot set up guest memory 'pc.ram': Invalid argument
Error accepting connection, aborting

最后将模拟器的RAM改为512

再次运行AVD,此时AVD能够正常运行起来了。至此,Android开发环境算是搭建起来了,可是速度和蜗牛一样。i3、4G的机器内存几乎被吃光,有条件还是换个mac吧。

相似回答