Android開發(fā)常見錯(cuò)誤小結(jié)
本文實(shí)例總結(jié)了Android開發(fā)的常見錯(cuò)誤。分享給大家供大家參考。具體如下:
錯(cuò)誤1:
在intent中添加了一個(gè)內(nèi)容,在調(diào)用getStringExtra讀取的時(shí)候,總是報(bào)錯(cuò)。代碼如下:
// back按鈕 Button btnBack = (Button) findViewById(R.id.btnActivity2Back); btnBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("from", 2); setResult(RESULT_OK, intent); finish(); } });
其中調(diào)用了intent的putExtra方法。
讀取代碼如下:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { TextView tview = (TextView) findViewById(R.id.textViewResult1); if (data != null) { tview.setText("從" + data.getStringExtra("from") + "返回!"); } super.onActivityResult(requestCode, resultCode, data); }
調(diào)用了getStringExtra來(lái)讀取數(shù)據(jù)。這里會(huì)報(bào)錯(cuò)。
錯(cuò)誤原因:
在putExtra的時(shí)候,代碼intent.putExtra("from", 2);中,2并不是一個(gè)字符串,而是一個(gè)數(shù)字。因此,在讀取的時(shí)候,調(diào)用getStringExtra報(bào)錯(cuò)了。
如果這么寫:intent.putExtra("from", 2 + "");就不會(huì)有問題。
錯(cuò)誤2:
在做listview使用simplecursoradapter的時(shí)候,遇到下面的這個(gè)問題。
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
這個(gè)錯(cuò)誤原因是我在main.xml布局文件中,將TextView控件包含在了<ListView>控件內(nèi)部而出現(xiàn)的錯(cuò)誤,在設(shè)計(jì)中用來(lái)顯示數(shù)據(jù)的控件,最好放在另外一個(gè)布局文件中。
比如說,我的listview的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="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnInitData5000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="初始化數(shù)據(jù)5000條" /> <Button android:id="@+id/btnInitListView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取數(shù)據(jù)初始化ListView" /> <ListView android:id="@+id/listView4" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
listview的xml中只寫listview的xml,而把每行要顯示的內(nèi)容,放在了myrow.xml中,如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textViewItemName" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
錯(cuò)誤3:
從listview的一個(gè)item點(diǎn)擊,跳轉(zhuǎn)到另外一個(gè)activity的時(shí)候,動(dòng)畫效果是不對(duì)的。
startActivity(intent); // finish(); Log.e("mason", "gongneng ani called"); overridePendingTransition(R.anim.slide_out_right, R.anim.slide_in_left);
如上面的代碼。但是如果先調(diào)用finish()函數(shù),動(dòng)畫效果就對(duì)了。但是此時(shí)有個(gè)問題。進(jìn)入了新的activity之后,按back鍵,不能回到原來(lái)的activity了。這是因?yàn)樵瓉?lái)的activity調(diào)用了finish函數(shù),finish函數(shù)相當(dāng)于用戶按下了back鍵。相當(dāng)于告訴了安卓系統(tǒng),這個(gè)activity可以被回收了(此時(shí)在安卓activity棧中這個(gè)activity也不存在了)。
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android利用CursorLoader實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫
這篇文章主要為大家詳細(xì)介紹了Android利用CursorLoader實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android Service自啟動(dòng)注意事項(xiàng)分析
這篇文章主要介紹了Android Service自啟動(dòng)注意事項(xiàng),結(jié)合實(shí)例分析了Android Service自啟動(dòng)過程中屬性設(shè)置的相關(guān)技巧,需要的朋友可以參考下2016-03-03Android ListView實(shí)現(xiàn)圖文列表顯示
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)圖文列表顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android Init進(jìn)程對(duì)信號(hào)的處理流程詳細(xì)介紹
這篇文章主要介紹了Android Init進(jìn)程對(duì)信號(hào)的處理流程詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02Android數(shù)據(jù)存儲(chǔ)方式操作模式解析
這篇文章主要為大家介紹了Android數(shù)據(jù)存儲(chǔ)方式操作模式解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08android 使用OkHttp上傳多張圖片的實(shí)現(xiàn)代碼
這篇文章主要介紹了android 使用OkHttp上傳多張圖片的相關(guān)資料,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07Android實(shí)現(xiàn)滾動(dòng)刻度尺效果
本篇文章主要介紹了Android實(shí)現(xiàn)滾動(dòng)刻度尺效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-05-05