Android常見(jiàn)錯(cuò)誤匯總
1.R.java消失或解析異常
查看res中資源文件,圖片,xml等。比如圖片文件名不能有大寫(xiě)不能有空格。
搞定錯(cuò)誤之后Project->clean就可以了。
2.自定義title欄。
首先要z在values->styles中定義一個(gè)style,然后在mainfest文件中設(shè)置android:theme.
最后在Activity中按照這個(gè)順序?qū)懀?/p>
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方法的區(qū)別:
看下面一段代碼
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();
代碼的作用是逆序輸出表中的內(nèi)容,第三行如果用的是isFirst()的話(huà)就無(wú)法輸出第一行,正確做發(fā)是用isBeforeFirst()。
4.eclipse刪除空行
在eclipse中刪除某一行就用ctrl+D快捷鍵。如果你想刪除一個(gè)文件中的所有空行呢。
可以用下面方法。
1)打開(kāi)源碼編輯器
2)使用快捷鍵Ctrl+f
3)在Find輸入框中輸入:^\s*\n
4)Replace With輸入框的值為空
5)在【Options】選中的"Regular expressions"
6)點(diǎn)擊【Replace All】按鈕。
7)OK!
5.getX()和getRawX()的區(qū)別
getX()是表示W(wǎng)idget相對(duì)于自身左上角的x坐標(biāo)
而getRawX()是表示相對(duì)于屏幕左上角的x坐標(biāo)值(注意:這個(gè)屏幕左上角是手機(jī)屏幕左上角,不管activity是否有titleBar或是否全屏幕),getY(),getRawY()一樣的道理
6.imagView居中顯示問(wèn)題
xml設(shè)置如下:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="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引發(fā)了 java.util.ConcurrentModificationException
如果多個(gè)線程同時(shí)訪問(wèn)一個(gè) ArrayList 實(shí)例,而其中至少一個(gè)線程從結(jié)構(gòu)上修改了列表,那么它必須 保持外部同步。
解決方法:初始化的時(shí)候 將ArrayList改為CopyOnWriteArrayList;
原理:
JAVA中將引用從一個(gè)對(duì)象移到另外一個(gè)對(duì)象不需要消耗CPU時(shí)間,可以看做是一個(gè)原子操作。
JAVA中如果一個(gè)對(duì)象仍舊存在引用,則不會(huì)被CG回收。
CopyOnWriteArrayList就是根據(jù)以上兩個(gè)特點(diǎn),在列表有更新時(shí)直接將原有的列表復(fù)制一份,并再新的列表上進(jìn)行更新操作,完成后再將引用 移到新的列表上。舊列表如果仍在使用中(比如遍歷)則繼續(xù)有效。如此一來(lái)就不會(huì)出現(xiàn)修改了正在使用的對(duì)象的情況(讀和寫(xiě)分別發(fā)生在兩個(gè)對(duì)象上),同時(shí)讀操 作也不必等待寫(xiě)操作的完成,免去了鎖的使用加快了讀取速度。
8.獲取隨機(jī)顏色
并不用每次都生成三個(gè)隨機(jī)數(shù),下面兩條語(yǔ)句就可以了:
Random myRandom=new Random();
int ranColor = 0xff000000 | mRandom.nextInt(0x00ffffff);
10.去掉Activity的標(biāo)題欄,全屏顯示
在manifest文件中修改對(duì)應(yīng)的Avtivity屬性。
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
11.如何修改應(yīng)用名稱(chēng)及應(yīng)用圖標(biāo)
修改程序的圖標(biāo),修改drawable文件夾的i→→c_launcher.png圖標(biāo),把新的圖標(biāo)改名覆蓋就可以了。
如果你要自己的名稱(chēng),可以修改AndroidManifest.xml的這個(gè)節(jié)點(diǎn),application android:icon="@drawable/ic_launcher",不需要加文件擴(kuò)展名。
即使這么做了,真機(jī)調(diào)試的時(shí)候可能還是會(huì)有一些問(wèn)題,比如圖標(biāo)沒(méi)辦法改變,這個(gè)時(shí)候就需要在Eclipse中新建一個(gè)不同名的項(xiàng)目,然后轉(zhuǎn)移代碼(有點(diǎn)小麻煩~_~!)。
12.關(guān)于調(diào)試方法
調(diào)試的時(shí)候程序如果出錯(cuò),一般是查看logcat,看error發(fā)生的地方,會(huì)提示在程序的第幾行,然后去找就可以了。
但有些錯(cuò)誤沒(méi)辦法定位,那就把日志輸出成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.
錯(cuò)誤1:請(qǐng)求的字段在數(shù)據(jù)庫(kù)的表中不存在,一般是大小寫(xiě)沒(méi)寫(xiě)對(duì);
錯(cuò)誤2:編程的中途改變表的字段,實(shí)際字段并沒(méi)有改變,解決方法是卸載當(dāng)前版本,再安裝調(diào)試。
14.android.content.res.Resources.loadXmlResourceParser
在傳遞string類(lèi)做參數(shù)的地方傳了int形變量。
15.android.content.res.Resources$NotFoundException
出現(xiàn)此類(lèi)異常時(shí),可以根據(jù) Resource ID到資源類(lèi)R中找相關(guān)的資源。比如0x7f030000,對(duì)應(yīng)的是city_item布局文件,就可以將問(wèn)題縮小到更小的范圍。對(duì)于這類(lèi)運(yùn)行時(shí)找不到資 源,但資源又確實(shí)存在的問(wèn)題,可能的編譯打包時(shí)出現(xiàn)問(wèn)題,沒(méi)有將該資源加入??尚薷囊幌略撡Y源,讓編譯器重新編譯。
還有試一下Project ->Clean一下這個(gè)項(xiàng)目 也可以的。[!--empirenews.page--]
16.交互性的button定義的方法:
首先是準(zhǔn)備好按鈕不同狀態(tài)的圖片
然后 在res/drawable中定義selector的xml文件
最后Button的background屬性中設(shè)置
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/addbtn_selector"/>
17在超級(jí)終端中執(zhí)行程序報(bào)錯(cuò)-Permission deny
參照http://android.stackexchange.com ... fo-on-error-message
主要原因是不能在sdcard中執(zhí)行,直接進(jìn)入data/目錄下面創(chuàng)建文件,然后執(zhí)行就可以了。
18.從svn導(dǎo)入工程項(xiàng)目有驚嘆號(hào)
錯(cuò)誤提示Archive for required library: 'libs/armeabi/libvudroid.so' in project 'DocumentViewer' cannot be read or is not a valid ZIP file
主要是路徑出了問(wèn)題
解決方法:在project的build-path將外部包(庫(kù))的引用刪掉就可以了。
19.首次進(jìn)入帶有EditText的Activity不自動(dòng)彈出軟鍵盤(pán),再次點(diǎn)擊才彈出。
只有設(shè)置manifest的方法有用,在activity的設(shè)置中添加:
[html] view plaincopyprint?
android:windowSoftInputMode="adjustPan|stateHidden"
20.Gallery中OnItemClickListener與OnItemSelectedListener的區(qū)別
OnItemClickListener:只有單擊Gallery中的View才會(huì)觸發(fā)事件,準(zhǔn)確的說(shuō)是當(dāng)點(diǎn)擊之后抬起手的時(shí)候觸發(fā),滑動(dòng)不會(huì)觸發(fā)。
OnItemSelectedListener:當(dāng)Gallery中的View被選中的時(shí)候就會(huì)觸發(fā),Galler初次顯示就會(huì)觸發(fā)一次,選中第一個(gè)iew,滑動(dòng)和單擊都會(huì)觸發(fā)。
20.從16進(jìn)制中提取顏色的rgb分量。
主要就是通過(guò)位運(yùn)算來(lái)實(shí)現(xiàn)。
[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中簽名導(dǎo)出apk崩潰,手動(dòng)簽名。
工程沒(méi)問(wèn)題,調(diào)試也沒(méi)問(wèn)題,但打包的時(shí)候eclipse會(huì)崩潰,解決方法是手動(dòng)打包。
首先去工程目錄下的bin文件夾下找到apk文件,解壓后刪除META-INF文件夾,重新打包成壓縮包,改后綴名為.apk
首先是簽名(假設(shè)你已經(jīng)在根目錄下生產(chǎn)了密鑰keystore):
進(jìn)入java安裝目錄/bin文件夾下:
[plain] view plaincopyprint?
./jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore android.keystore ~/Output.apk android
然后是優(yōu)化,進(jìn)入sdk的tools文件夾下,運(yùn)行。
[plain] view plaincopyprint?
./zipalign -v 4 ~/Output.apk Output_realase.apk
當(dāng)前目錄下Output_realase.apk就是打包簽名好的apk了。
22.android.view.InflateException: Binary XML file line #異常的解決
創(chuàng)建自定義view的時(shí)候,碰到 android.view.InflateException: Binary XML file line #異常,反復(fù)研究
后發(fā)現(xiàn)是缺少一個(gè)構(gòu)造器造成。
[java] view plaincopyprint?
public MyView(Context context,AttributeSet paramAttributeSet)
{
super(context,paramAttributeSet);
}
補(bǔ)齊這個(gè)構(gòu)造器,異常就消失了.
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);[!--empirenews.page--]
out.write(buffer, 0, read);
}
}
24.判斷是否有root權(quán)限
[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.最簡(jiǎn)單的Root 模擬器的方法
啟動(dòng)一個(gè)模擬器,開(kāi)始-運(yùn)行-輸入cmd,打開(kāi)dos,依次輸入
adb shell
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
cd /system/bin
cat sh > su
chmod 4755 su
su
即可獲得root權(quán)限