Android開發(fā)常見錯誤小結
本文實例總結了Android開發(fā)的常見錯誤。分享給大家供大家參考。具體如下:
錯誤1:
在intent中添加了一個內(nèi)容,在調用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(); } });
其中調用了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); }
調用了getStringExtra來讀取數(shù)據(jù)。這里會報錯。
錯誤原因:
在putExtra的時候,代碼intent.putExtra("from", 2);中,2并不是一個字符串,而是一個數(shù)字。因此,在讀取的時候,調用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點擊,跳轉到另外一個activity的時候,動畫效果是不對的。
startActivity(intent); // finish(); Log.e("mason", "gongneng ani called"); overridePendingTransition(R.anim.slide_out_right, R.anim.slide_in_left);
如上面的代碼。但是如果先調用finish()函數(shù),動畫效果就對了。但是此時有個問題。進入了新的activity之后,按back鍵,不能回到原來的activity了。這是因為原來的activity調用了finish函數(shù),finish函數(shù)相當于用戶按下了back鍵。相當于告訴了安卓系統(tǒng),這個activity可以被回收了(此時在安卓activity棧中這個activity也不存在了)。
希望本文所述對大家的Android程序設計有所幫助。
相關文章
Android利用CursorLoader實現(xiàn)短信驗證碼自動填寫
這篇文章主要為大家詳細介紹了Android利用CursorLoader實現(xiàn)短信驗證碼自動填寫的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android ListView實現(xiàn)圖文列表顯示
這篇文章主要為大家詳細介紹了Android ListView實現(xiàn)圖文列表顯示,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01android 使用OkHttp上傳多張圖片的實現(xiàn)代碼
這篇文章主要介紹了android 使用OkHttp上傳多張圖片的相關資料,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07