當前位置:編程學習大全網 - 編程語言 - Android開發問題怎麽解決?

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="/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="/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

參照/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插件。

  • 上一篇:考生誠信考試承諾書
  • 下一篇:linux的線程和進程linux的線程
  • copyright 2024編程學習大全網