开发者

Common Errors while writing Android Project

开发者 https://www.devze.com 2023-02-20 11:18 出处:网络
What are the Common Errors which should be taken开发者_开发问答 care of, while working on Android project?

What are the Common Errors which should be taken开发者_开发问答 care of, while working on Android project?

These can be

  • Android Specific Error
  • Java Specific Error


I will list some errors i m always getting.

Article - Top 10 Java Errors

  • NullPointer error

    1. when i use un initialized variable or object we are creating. (Java)
    2. when we use some layout out view that is not in xml what we set in context.(Android)


  • ClassCast Exception
    • when a program attempts to cast a an object to a type with which it is not compatible. (eg: when i try to use a linear layout which is declared as a relative layout in xml layout).


  • StackOverflowError
    • it can also occur in correctly written (but deeply recursive) programs.(java and android)
    • when a program becomes infinitely recursive.
    • we create layout (deep and complex) that exceeds that stack of platform or virtual machine . recursive or too much of layout will create Stack overflow error in Android
    • Too many inner layouts.


  • ActivityNotFoundException: Unable to find explicit activity class exception

    • The activity is not declared in manifest.


  • Android securityException
    • You need to declare all permission in the application Manifest that your application check this link (internet, access to contact,gps,wifi state,write to SDCard, etc).


  • OutofMemoryError
    • when a request for memory is made that can not be satisfied using the available platform resources . mainly using bit map, gallery , etc.


  • Application Not Responding (ANR)

    • Mainly comes when you are making network function,or some long process.

    this will block UI Thread so user can not do any work. to avoid ANR read this & this

This are thing i mainly get while creating Android Project.

  • Try to use Try - Catch block in All Place of program. Dont leave your catch block empty as this can hide errors:

Yes:

 try{
    // try something
  } catch (Exception e) {
      Log.e("TAG", "Exception in try catch", e);
      return false;
  }
  return true;

No:

  try{
    // try something
  } catch (Exception e) {
    return false;
  }
  return true;


Use proper Naming conversion for all variable and ID's in Layout.*


I read one article from net it contains some error now i am adding that alos if it have redundancy please forgive me.

Issue : My previously nice RelativeLayout is making an ugly heap or some elements aren't visible anymore...What's going on ??? I just moved an element in it... Solution : Never forget that in a RelativeLayout, elements are referenced and placed in relation to their neighbours. Maybe there is something wrong in the hierarchy of relationship between your element. Try opening the outline view in Eclipse and clicking each element to see where there is a rupture.

Issue : Circular dependencies cannot exist in RelativeLayout Solution : You have probably written the same dependency in two different way. For instance an ImageView as the attribute android:layout_toRightOf a TextView and the TextView has android:layout_toLeftOf the ImageView. Only one of them is necessary

Issue : I wrote a style for one of my view/layout, but when I apply it in my xml, I have no display in the layout viewer of Eclipse Solution : Unfortunately, this seems to be a bug of the android ADT, I reported it but no news so far. Anyway, no panic, styles are working well, but they aren't displayed properly in Eclipse. Just build the app and launch it on the emulator or phone and you will see if everything is fine or not.

Issue : Toast is written properly but nothing is displayed Solution : This is a common error of use : just add the .show() method to show the Toast and see if it is working well

Issue : I tried to display a String from strings.xml but I just had a number like 0x7f060001 Solution : This is not a bug, just a display due to the way android deals with resources. When you want to retrieve a resource, you have to use a method like getString(R.id.something), getDrawable, …Otherwise, you just display the reference written in the R class

Issue : Some change in code doesn't have any effect in the application Solution : there are 2 options, either you have forgotten something like the .show() of the Toast, or the emulator is not updating properly your application. In that case, you have to check the option “Wipe user Data” in your launch configuration of the emulator in Eclipse.

Issue : How to display borders on my table? Solution : There is no direct way to do that in android, you have to use a trick :http://www.droidnova.com/display-borders-in-tablelayout,112.html

Issue : the emulator is writing in japaneese withtout you having changed any parameter Solution : This happens sometimes, quite easy to fix, just long click in any EditText field, and change the input type to what you want

Issue : I can't get the Context Menu to appear in the emulator Solution : long click on emulator does not seem to register on every kind of view, you have to push the button in the center of the 4 directionnal arrows

Issue : I'm following a tutorial about map route but I can't get it work, android does not find a package Solution : You might have been following a tutorial written for 1.5 SDK. At this time, there was a package to display route in android, but it was removed in the next SDK and is not available anymore. It just not possible anymore. There seems to be a trick with KML files but nothing official

Issue : Sending coordinates to the emulator gives wrong position Solution : ensure that you wrote the coordinate like 51.16548 and not 51,16548 nor 5116548

Issue : Only the original thread that created a view hierarchy can touch its views. Solution : You must have tried to update a view content from another thread than the UI thread. 2 options to patch this : either create a handler in your UI thread and post your Runnable to this handler OR use the method runOnUIThread to run the lines of code that are doing the update

Issue : accessing localhost 127.0.0.1 doesn't work Solution : it works, you are just not doing it the right way : use 10.0.2.2


  • Forgetting to register new activities in the manifest.
  • Casting to the wrong type on findViewById() when getting references to the ui widgets


Another typical beginner's error is not clean the project after having had a bug in the xml...


Though cannot be called an error (but mistake) Most of the time I forget to add the permissions (i.e. CAMERA, WRITE_EXTERNAL_STORAGE) in the AndroidManifest file.


  • declaring the <uses-library android:name="com.google.android.maps" /> in the wrong place within the manifest.
  • forgetting to show() a toast.
  • (Added 2011/05/28) db adapter that relies on a _id column . See this thread

Also not directly an error but a misconception:

  • That the SQLite database (so a file) is located in the hard disk. It is located in the emulator image file.. More info here


Monster error OutOfMemoryError: bitmap size exceeds VM budget


Another common error or mistake is:

While we extend our Activity with the ListActivity

public class ListViewDemo extends ListActivity
{
    // some code here
}

And always forgot to add id android:id="@id/android:list" to the ListView tag in XML layout.

And adapter throws NullPointerException because of default id android:id="@+id/ListView01


The most common error or problem is occured when we create a project in Eclipse and we have to give a pakage name which contanis atleast one dot (.) , but most of beginner's not aware of this.

Like Android application package name must have two levels.

e.g. - com.myapp

but every beginner stuck at that point.


java.net.SocketException if user not define permission for internet in mainfest file while using webservice


one common error is nullPointerException...


One mistake that I make over and over again is trying to set a colour property using a resource identifier.

I can set my view's text by providing an integer resource ID,

view.setText(R.string.my_text);

but if I try the same thing for a colour I will end up with some shade of dark grey, regardless of the actual colour value specified:

view.setBackgroundColor(R.color.my_colour);

The problem is that in Android colours are represented as 32-bit ARGB integer values, so that's what is expected in the int parameter. It should be:

view.setBackgroundColor(context.getResources().getColor(R.color.my_colour));


We have to properly close Sqlite Database and Cursor .

Else memory leak and exception to close cusror and database will come


Application crash without any notice when phone is rotated (auto-rotation is enabled in phone). This has to do with "orientation". There are some solutions, the simplest one is to have android:preferedOrientation="portrait" set in manifest file for each activity. Also, set in code this if you are dynamically creating activities. Other solutions include proper treat of onPause,onRestart ... and so on, methods for activity (like saving state & stuff).

0

精彩评论

暂无评论...
验证码 换一张
取 消