Android開發常見錯誤小結_Android

來源:互聯網
上載者:User

本文執行個體總結了Android開發的常見錯誤。分享給大家供大家參考。具體如下:

錯誤1:

在intent中添加了一個內容,在調用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來讀取資料。這裡會報錯。

錯誤原因:

在putExtra的時候,代碼intent.putExtra("from", 2);中,2並不是一個字串,而是一個數字。因此,在讀取的時候,調用getStringExtra報錯了。

如果這麼寫:intent.putExtra("from", 2 + "");就不會有問題。

錯誤2:

在做listview使用simplecursoradapter的時候,遇到下面的這個問題。

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

這個錯誤原因是我在main.xml布局檔案中,將TextView控制項包含在了<ListView>控制項內部而出現的錯誤,在設計中用來顯示資料的控制項,最好放在另外一個布局檔案中。

比如說,我的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="初始化資料5000條" />   <Button     android:id="@+id/btnInitListView"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="讀取資料初始化ListView" />   <ListView     android:id="@+id/listView4"     android:layout_width="fill_parent"     android:layout_height="wrap_content" >   </ListView> </LinearLayout>

listview的xml中唯寫listview的xml,而把每行要顯示的內容,放在了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()函數,動畫效果就對了。但是此時有個問題。進入了新的activity之後,按back鍵,不能回到原來的activity了。這是因為原來的activity調用了finish函數,finish函數相當於使用者按下了back鍵。相當於告訴了安卓系統,這個activity可以被回收了(此時在安卓activity棧中這個activity也不存在了)。

希望本文所述對大家的Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.