Summary of common errors in Android development

  • 2020-06-19 11:43:41
  • OfStack

Examples of common errors in Android development are summarized in this article. Share to everybody for everybody reference. The details are as follows:

Error 1:

A content was added to intent, always reporting an error when calling getStringExtra to read. The code is as follows:


// back button  
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(); 
  } 
});

The putExtra method of intent is called.

Read the code as follows:


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    TextView tview = (TextView) findViewById(R.id.textViewResult1);
    if (data != null) { 
      tview.setText(" from " + data.getStringExtra("from") + " return !");
    } 
    super.onActivityResult(requestCode, resultCode, data); 
}

getStringExtra is called to read the data. There's going to be an error.

Reason for error:

With putExtra, the code intent.putExtra ("from", 2); , 2 is not a string, but a number. Therefore, the call to getStringExtra reported an error on the read.

If you write intent.putExtra("from", 2 + ""); There will be no problem.

Mistake 2:

When using simplecursoradapter for listview, you have the following problem.

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

The reason for this error is that I included the TextView control in the ES44en.xml layout file < ListView > Control internal errors, in the design used to display data controls, preferably in another layout file.

For example, my listview's xml is:


<?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=" Initialization data 5000 article " /> 
  <Button 
    android:id="@+id/btnInitListView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=" Read data initialization ListView" /> 
  <ListView 
    android:id="@+id/listView4" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
  </ListView> 
</LinearLayout>

In xml of listview, only xml of listview was written, and the content of each line to be displayed was placed in ES61en.xml, as follows:


<?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> 

Mistake 3:

When clicking on one item of listview to jump to another activity, the animation effect is not correct.


startActivity(intent); 
// finish(); 
  Log.e("mason", "gongneng ani called"); 
  overridePendingTransition(R.anim.slide_out_right, 
   R.anim.slide_in_left);

The code above. But if you call the finish() function first, you're done. But there's a problem here. After entering the new activity, press the back key, it will not return to the original activity. This is because the original activity called the finish function, which is equivalent to the user pressing the back key. This tells Android that the activity can be recycled (at this point the activity does not exist in the Android activity stack).

Hopefully, this article has been helpful in your Android programming.


Related articles: