Android開發(fā)常見錯誤小結(jié)
本文實例總結(jié)了Android開發(fā)的常見錯誤。分享給大家供大家參考。具體如下:
錯誤1:
在intent中添加了一個內(nèi)容,在調(diào)用getStringExtra讀取的時候,總是報錯。代碼如下:
// 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來讀取數(shù)據(jù)。這里會報錯。
錯誤原因:
在putExtra的時候,代碼intent.putExtra("from", 2);中,2并不是一個字符串,而是一個數(shù)字。因此,在讀取的時候,調(diào)用getStringExtra報錯了。
如果這么寫:intent.putExtra("from", 2 + "");就不會有問題。
錯誤2:
在做listview使用simplecursoradapter的時候,遇到下面的這個問題。
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
這個錯誤原因是我在main.xml布局文件中,將TextView控件包含在了<ListView>控件內(nèi)部而出現(xiàn)的錯誤,在設計中用來顯示數(shù)據(jù)的控件,最好放在另外一個布局文件中。
比如說,我的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>
錯誤3:
從listview的一個item點擊,跳轉(zhuǎn)到另外一個activity的時候,動畫效果是不對的。
startActivity(intent);
// finish();
Log.e("mason", "gongneng ani called");
overridePendingTransition(R.anim.slide_out_right,
R.anim.slide_in_left);
如上面的代碼。但是如果先調(diào)用finish()函數(shù),動畫效果就對了。但是此時有個問題。進入了新的activity之后,按back鍵,不能回到原來的activity了。這是因為原來的activity調(diào)用了finish函數(shù),finish函數(shù)相當于用戶按下了back鍵。相當于告訴了安卓系統(tǒng),這個activity可以被回收了(此時在安卓activity棧中這個activity也不存在了)。
希望本文所述對大家的Android程序設計有所幫助。
相關文章
Android 調(diào)用系統(tǒng)應用的方法總結(jié)
這篇文章主要介紹了Android 調(diào)用系統(tǒng)應用的方法總結(jié)的相關資料,這里提供調(diào)用錄像,錄音,拍照等功能,需要的朋友可以參考下2017-08-08
Android ListView的item中嵌套ScrollView的解決辦法
有時候,listview 的item要顯示的字段比較多,考慮到顯示問題,item外面不得不嵌套ScrollView來實現(xiàn),糾結(jié)怎么解決此問題呢?下面小編給大家分享下Android ListView的item中嵌套ScrollView的解決辦法,感興趣的朋友一起看看吧2016-10-10
Android 使用Vitamio打造自己的萬能播放器(8)——細節(jié)優(yōu)化
本文主要介紹Android Vitamio開發(fā)播放器,這里給大家提供了一些小的細節(jié)優(yōu)化,更加完善播放器的功能,希望能幫助有需要的小伙伴2016-07-07
Android編程實現(xiàn)分頁加載ListView功能示例
這篇文章主要介紹了Android編程實現(xiàn)分頁加載ListView功能,結(jié)合實例形式分析了listview分頁加載的原理、實現(xiàn)技巧與相關注意事項,需要的朋友可以參考下2017-02-02
Android入門之使用SharedPreference存取信息詳解
這篇文章主要為大家詳細介紹了Android如何使用SharedPreference實現(xiàn)存取信息,文中的示例代碼講解詳細,對我們學習Android有一定的幫助,需要的可以參考一下2022-12-12
Android studio導出APP測試包和構建正式簽名包
大家好,本篇文章主要講的是Android studio導出APP測試包和構建正式簽名包,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2021-12-12
Android Rreact Native 常見錯誤總結(jié)
這篇文章主要介紹了Android Rreact Native 常見錯誤總結(jié)的相關資料,需要的朋友可以參考下2017-06-06

