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个参数混淆起来。
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。
15、R.java消失或解析异常
查看res中资源文件,图片,xml等。比如图片文件名不能有大写不能有空格。
搞定错误之后Project->clean就可以了。
16、自定义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);
17、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()。
18、获取随机颜色
并不用每次都生成三个随机数,下面两条语句就可以了:
Random myRandom=new Random();
int ranColor = 0xff000000 | mRandom.nextInt(0x00ffffff);
19、从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将外部包(库)的引用删掉就可以了。
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权限