Android开发常见错误及技巧

1、无法使用网络:Permission denied(maybe missing internet permission)

在AndroidMainifest.xml中增加允许使用网络选项(在</application>结束标签之后>):

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

2、找不到activity类: android.content.ActivityNotFoundException: Unable to find explicit activity class {xxxx}

在AndroidMainifest.xml中增加activity的申明,如:

<activity android:name=".xxxActivity" >

</activity>

3、为什么我找不到 startSubActivity 方法?

现在,请使用 startActivityForResult 方法来代替旧的startSubActivity方法。

4、无法加载xml中的view,报 java.lang.NullPointerException 异常

忘记加载activity的layout文件:

setContentView(R.layout.main);

5、Unparsed aapt error(s)! Check the console for output

但是你的控制台上找不到错误或者 看不懂错误的时候,点 Project--------->clean..就会没问题

http://hovertree.com/menu/android/

6、requestCode和resultCode的区别

在使用startActivityForResult()和onActivityResult()时,会分别用到requestCode和resultCode,有时候极容易将2个参数混淆起来。

requestCode 和 resultCode 混淆说明错的。

startActivityForResult(Intent intent, Int requestCode)

intent 传给B的,不解释,看不懂你还是玩玩手机算了,别想开发的事情了

requestCode >=0就好,随便用于在onActivityResult()区别哪个子模块回传的数据,如果还有C.java ,D甚至E子模块的话,每个区分开不同的requestCode就好。

setResut(int resultCode, Intent intent)

resultCode 如果B子模块可能有几种不同的结果返回,可以用这个参数予以识别区分。这里还有个特殊的 RESULT_OK 值,没有特殊情况用它就好了,sdk有说明的,呵。

intent 继续不解释,传回给A的onActivityResult()

onActivityResult(int requestCode, int resultCode, Intent intent)

这里三个都不用解释了,与上文对应的东西。如果不对requestCode和resultCode 加以识别区分的话,只要有其他activity setResult到了A onActivityResult()会无差别处理。

7、无法下载文件到SD卡中

在manifest文件中加上:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

8、让控件在父容器中居中:

android:layout_gravity="center_vertical"

9、控件两端对齐:

如下代码让位于同一行的两个控件分别左对齐和右对齐:

<RelativeLayout

xmlns:Android="http://schemas.android.com/apk/res/android"

Android:background="@drawable/top"

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

><ImageView

Android:id="@+file_browser/imgRefresh"

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:layout_marginLeft="10px"

Android:src="@drawable/refresh"

Android:layout_centerVertical="true"

>

</ImageView>

<ImageView

Android:id="@+file_browser/imgCheck"

Android:layout_alignParentRight="true"

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:layout_marginRight="10px"

Android:src="@drawable/close"

Android:layout_centerVertical="true"

>

</ImageView>

</RelativeLayout>

10、android软键盘控件往上挤的解决办法:

  键盘区域外才是屏幕的边缘,定义布局文件时使用:android:gravity="bottom"的话就会被挤到上部!

  解决办法:

  在此工程的androidMainfest.xml文件中对应的Activity中写入 android:windowSoftInputMode="adjustPan"

  或者在配置文件中把布局文件的大小写死!

11、在布局中使用scrollview:

把原来的布局用<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none"></ScrollView>括起来即可实现视图的滚动。

12、全局变量Application Context

创建一个属于你自己的android.app.Application的子类,然后在manifest中申明一下这个类,这是android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。 下面看一下Demo:

class MyApp extends Application {

private String myState;

public String getState(){

return myState;

}

public void setState(String s){

myState = s;

}

}

class Blah extends Activity {

@Override

public void onCreate(Bundle b){

...

MyApp appState = ((MyApp)getApplicationContext());

String state = appState.getState();

...

}

}

这个效果就是使用静态变量是一样的,但是其更符合android的架构体系。 使用这种方法的话需要在AndroidManifest.xml中配置一下:

<application android:name=".MyApp"

android:icon="@drawable/icon"

android:label="@string/app_name">

13、Android模拟器打电话发短信

GPhone的模拟器有个特有的号码:15555218135,这个就类似我们实体手机的SIM卡号码啦。要实现拨号,用手机?当然不行!

更简单,三步:

1.打开终端

2.连接: telnet localhost 5554(5554是你打开模拟器后上面显示的数字)

3.命令:gsm call 15555218135

look!是不是模拟器上显示来电了?接听/挂断和实体手机一样。

发短信也一样简单,重复上面1,2两步,第三部命令改一下:

sms send 15555218135 Hello,this is a Message.

14、ListView不能触发OnItemClickListener监听器

检查行所绑定的行布局文件中是否使用了EditText,如果有的话将EditText的focusable 属性设为false。

推荐:http://www.cnblogs.com/roucheng/p/sdka.html

时间: 2024-08-28 21:09:13

Android开发常见错误及技巧的相关文章

Android开发常见错误

1.java.net.SocketTimeoutException: Transport endpoint is not connected 此问题可能是conn的地址填写错误最好填写ipv6的地址

Android NDK开发常见错误

错误一: make: *** No rule to make target `/cygdrive/d/1-workspace/showmap-android-opengles/jni/showmap_opengles_OpenGLESRenderer.c', needed by `/cygdrive/d/1-workspace/showmap-android-opengles/obj/local/armeabi/objs/OpenGLESMap/showmap_opengles_OpenGLES

android开发常见编程错误总结

1.设置TextView的文本颜色 1 2 3 TextView tv; ... tv.setTextColor(R.color.white); 其实这样设置的颜色是 R.color.white的资源ID值所代表的颜色值,而不是资源color下的white颜色值:正确的做法如下: 1 tv.setTextColor(getResources().getColor(R.color.white)); 这个出错的概率满高的,就是因为二者都是int类,导致编译器不报错. 2.读取Cursor中的值 1

提高eclipse使用效率(二) 提高Android开发效率的小技巧

2013-09-04 22:49:33cnblogs.com-sw926-点击数:214 XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Window - Preferences,在右边的目录树中切换到XML - XML Files - Editor - Content Assist,是不是很熟悉,没错,就是Content Assist 接下来就简单了,延迟设为50ms,提示字母把能填的都填上去,设置完之后可以试一下.输入

如何在u不能图上搭配android开发环境——ubuntu小技巧4

如何在linux下用eclipse配置android开发环境 好长时间没有搭配android开发环境了,前几天在win下配了一个用了一下,开始经常使用linux系统的我无法满足于win,今天在ubuntu下试了下,配置了linux下的android环境,在这里分享给想学安卓 的朋友!在另外一篇博客里面介绍了如何搭配win下的android开发环境,有兴趣的朋友可以看一看! 搭配android环境有两种方法:第一种使用集成开发包,第二种自己下载配置插件. 至于是否方便,当然第一种比较容易,省时,合

Android 开发之错误整理java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10088 nor current process has android.permission.READ_PHONE_STATE.

java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10088 nor current process has android.permission.READ_PHONE_STATE. 今天写了一款发短信的软件,拿了个酷派5879,试了下,结果不能用,把try{}catch{}去掉了,报这个错误, android.permission.READ_PHONE_STATE.没有READ_PHONE_STATE权限,

Linux以及Android开发中的小技巧和长繁命令记录收集

不断更新收集中.... 2014071743 ssh以nx_guest的身份登录到172.24.221.137,然后在172.24.221.137与172.24.61.252的8080端口建立网络连接,同时创建端口为5678的本地代理服务 ssh -C -f -N -o 'TCPKeepAlive=yes' -L 5678:172.24.61.252:8080 [email protected] 反编译android下的二进制程序 ./prebuilts/gcc/linux-x86/arm/ar

WCF分布式开发常见错误解决(1):An error occurred while attempting to find services at...添加服务引用出错

      WCF分布式开发常见错误解决(1):An error occurred while attempting to find services at...添加服务引用出错 当我们在客户端添加WCF服务引用的时候出错,信息如下 下载“http://localhost:8001/WCFService”时出错. 无法连接到远程服务器 由于目标机器积极拒绝,无法连接. 127.0.0.1:8001 Metadata contains a reference that cannot be reso

Android eclipse常见错误开发总结

使用Android 开发工具eclipse的过程中,出现过很多问题,总结了一下,拿出来共同学习: 问题1.This Android SDK requires Android Developer Toolkit version 20.0.0 or above... *在android_sdk_windows/tools/lib下的plugin.prop文件里把 #begin plugin.prop plugin.verson=20.0.0 #end plugin.prop 变更为: #begin