Saturday, September 1, 2012

Android Development - FAQs, Errors and Solutions


This page is dedicated to all Android developers to save them a lot of pain, time and bandwidth. The page is continuously being updated with the latest tips and solutions for Android Development.

1. R.java is missing

Possible Reason(s) -
One of your XMLs has an error. Either a tag is common between two XMLs or a tag is not closed .

Solution -
Expand the res/values and assets folder. Check whether any XML has Error icon in it. if there is any conflict, resolve it. Then Go to Project->Clean in the top menu bar.

Entire project will be rebuilt and you'll be good to go.

2. AlertDialog not working when passing Context parameter as this.

Possible Reason(s) -
You've passed this instead of YourActivityName.this in the AlertDialog.Builder function.

Solution -
Pass YourActivityName.this inside the Builder function as shown below -

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder
.setTitle("Are You Sure?")
 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
               //Do Something          
       }
 })
.setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
              dialog.cancel();
       }
 }).show();

3. How to check whether the application is installed in the device programmatically?

Simple. You just need to know the package name of the program. Pass the package name in the following function -

private boolean appInstallStatus(String packageName)
{
try{
ApplicationInfo appinfo= getPackageManager().getApplicationInfo(packageName , 0);  
//Application exists
return true;
}
catch( PackageManager.NameNotFoundException e ){
//Application doesn't exist
return false;    
}
}

4. ADW Theme: How to launch ADW Theme selection window from your Application?

So you've made an ADW Theme, an interface with an apply button and you want to show the ADW theme section menu on clicking it.

Sadly, normal ADW launcher doesn't have anything to handle that. However ADW EX has that effect. Use the code below -

Intent i=new Intent("org.adw.launcher.SET_THEME");
i.putExtra("org.adw.launcher.theme.NAME","com.yourtheme.package.name");
startActivity(Intent.createChooser(i,"Apply theme to..."));

5. ADW Theme: How to change the app drawer icon in ADW?

Well, according to this, you can't

6. How to install an apk in Android simulator?

Simple.

a) Start the simulator.
b) After it boots into android screen, go to Android directory (generally c:/program files/Android) & browse to platform-tools folder.
c) Copy and paste the apk there.
d) Open cmd and change the directory to platform-tools folder.
e) type adb install <apkname>.apk

Done...

7. While installing, I'm getting application already installed error.

Follow the above steps till d.
Then type adb install -r <apkname>.apk

8. How to see debug info in Android?

In eclipse, go to Window > Share View > LogCat. The logcat window will open. Now, in AVD, run the application and the log will be shown in LogCat.

9. Emulator showing "Waiting for device" when trying to enter adb command.

Your adb server is having problem. Type adb kill-server and then try to install.

10. My app is crashing only in QVGA/LDPI in emulator.

Most likely you haven't included icons or images for LDPI screen (inside res/drawable-ldpi) and thus the application is trying to render an hdpi/mdpi icon in an ldpi screen and crashing.

Just try to include icons for LDPI screen (36px X 36px) and see whether it is working.

11. How will I find launcher actions for an application?

Two ways -
a. Go to http://activities.droidicon.com/
b. Install Sony AppXplore - https://play.google.com/store/apps/details?id=com.sonyericsson.androidapp.AppExplore

12. How to check the number of times an application has started?

@OnCreate (Bundle bundle){
  mPref = getPreferences();
  int c = mPref.getInt("numRun",0);
  c++;
  mPref.edit().putInt("numRun",c).commit();
  //do other stuff...
}
 
@Override
protected void OnPause(){
  if(!onFinishing()){
    c = mPref.getInt("numRun",0);
    c--;
    mPref.edit().putInt("numRun",c).commit();
  }
  //Other pause stuff.
}

 
To be continued....

0 comments:

Post a Comment