Friday, June 24, 2011

Met with a CRASH , What to do next ??

A Crash is something that deters developer's pace. Trust me, to analyze the android app crash is a piece of cake with android's adb. It's just that we need to be familiar with certain internal stuff. I'm mentioning few of the things that i came across these days that might be handy for a new android developer. This is will be like a check list that u need to make sure before stepping through the code.

First and foremost thing is "LogCat", its already there in your eclipse IDE. If you are not able to see the logcat window, then go to Window -> show View -> Other -> Android -> Log Cat. This is an important tool, which catches all the application logs, exceptions, debug statements and of course the verbose. If you have set this up, then i'm sure you have almost fixed your crash ;)

Second, Now you can scroll and see all the red coloured horrible lines of your code's exception message inside the LogCat. Please do not start with the last line of the exception thrown, I'm sure you will not have the slightest understanding of what has went wrong. Scroll to the top and read the function which is at the top of call stack (similar to ur windbg call stack). You may get a clue - which function has the problem.

Third, Most often you might end up with an error of missing activity or unable to start the activity. You would created the xml file, and u might used the proper id in the java file. But stil, most of the time we might forget to add them to the androidManifest.xml file. If you have not added it here, AOS does not identify your new activity. So make sure all the activities you have used are mentioned here. This includes all the broadcast receivers, services etc.,

Fourth one also involves the androidManifest.xml file. This time, you need to use this line :
e.g., uses-permission android:name="android.permission.CALL_PHONE" in ur manifest file which asks the permission to the user. If you have not mentioned something like this, and if you programmatically try to do those operations, again its a crash !
Examples of uses permission:
uses-permission android:name="android.permission.READ_SMS"
uses-permission android:name="android.permission.RECEIVE_SMS"
uses-permission android:name="android.permission.READ_CONTACTS"
uses-permission android:name="android.permission.READ_PHONE_STATE"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-permission android:name="android.permission.MODIFY_PHONE_STATE"
uses-permission android:name="android.permission.SEND_SMS"
uses-permission android:name="android.permission.WRITE_SMS"
make sure to use the appropriate permission in ur manifest, not all are required !

Fifth, its the most familiar problem "java-NullPointer-exception".. This problem can be caused in a variety of ways and i'm sure i have caused this issue in almost all possible ways :P Anyways, most common issues are caused when we try to access the variable or the object without initializing it.
For e.g, say you have a text view with an id = textview01 and if you need to access it in your java file.
You must : mTextView = (EditText) findViewById(R.id.textview01) only after this you can do this -> String str = mTextView.getText().toString();
There are other possibilities like string array initialization, cursor out of bounds, array index out of bounds, etc. which you must be very familiar. Please take spl care when you are accessing a cursor adapter to query values.

Sixth is, try to use "try catch" where you do string operations, db queries, web service communications,threaded functions,content resolver operations and array parsing etc., Try catching can avoid that terrible "sorry -- close" dialog. (Which i know is terrible to look at ;))

Thursday, June 23, 2011

What will you do when you have an xyz.aidl file to use ??

You need to be pretty sure whats an aidl file and whats the point in using them ! Yes, an aidl file is the interface through which we can perform an IPC. Since each application runs in its own VM (dalvik vm), each process has its unique user id and its restricted to access data or the memory part of the other processes. For example, if your app needs to communicate with the call application of the phone's process you need an interface through which you can communicate. People familiar with marshalling & unmarshalling will have the idea of the purpose behind. To put them in simple words : "they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL" - from the docs.

Fine, if you are well aware of the aidl's purpose you can get it working in your test application. All that you need is to obtain the xyz.aidl from the correspoding repo. (Whenever you need it -> sync it from the google code repo -> use tortoise svn (win 32 client)). As soon as you sync it, cpy that aidl file along with its package structure (com.android.internal.telephony) and place it in your src file. After adding it there, open eclipse and refresh your project. If it has not been added yet, then perform a clean and make sure the particular interface gets added in your project explorer.

Once that is listed, you can make use of the interface and call the functions directly from the local thread. You can notice the xyz.java file generated in the project explorer.

Wednesday, June 22, 2011

All about developing an android app !

I have planned to share all the information that i came across while developing the ANDROID application, in that way i can keep a log of the new things that i have learnt so far. This may be interesting for the newbies !

Here we go ..
The first interesting thing that i came across was the ksoap2 library provided by google. Well, we need to know the purpose of this external jar file first ! My application had to connect to a web-service application hosted in a test server. For an android client to communicate with the web-server (.Net) we need add this ksoap2 library to our project properties (java build path).
download link : http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2

The first noticeable thing with ksoap2 is to pick the right version from the google code page. I detected some issues with the 2.4 version which i downloaded initially. Later on, i downloaded the 2.5.5 from the download page and i had no example code to work on and the older example codes were not compatible with the latest version. So i decided to pick an intermediate version 2.5.2 from the google code page using tortoise svn from windows. (bcoz repo is maintained in svn).

Once you have obtained the 2.5.2, you can use this example code to work on.

public void GetResults()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("lastName", "sm");
request.addProperty("middleName", "");
request.addProperty("firstName", "");
request.addProperty("pageSize", 25);
request.addProperty("pageNumber", 1);

SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);

//AndroidHttpTransport aht = new AndroidHttpTransport(URL);
HttpTransportSE aht = new HttpTransportSE(URL);

try
{
aht.setXmlVersionTag("");
aht.call(SOAP_ACTION, soapEnvelope);

//SoapObject result = (SoapObject)soapEnvelope.bodyIn;
SoapObject result = (SoapObject)soapEnvelope.getResponse();
//SoapPrimitive result = (SoapPrimitive) soapEnvelope.getResponse();
//Object result = soapEnvelope.getResponse();

Log.d("WS", String.valueOf(result));
}
catch(Exception e)
{
e.printStackTrace();
}
}
you may be wondering, what to import since ctrl+shift+O does not automatically fix the project settings. So here is the import list required :
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
See I faced a lot of issues with the import statements when i used the latest 2.5.5. So be careful with the import statements.
Now.. In the above code u might see somethings like NAMESPACE, METHOD_NAME ,URL etc. What are these ? and why are they important ??
this is the place where we are going to specify the webservice, the method exposed and its corresponding address.
private static final String SOAP_ACTION = "http://tempuri.org/MethodName";
private static final String METHOD_NAME = "MethodName";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://your_ip_where_service_running/Service1.asmx";

Many of you might ignore the last four lines of code, but believe me a small issue in the namespace took me 2 days to fix a certain deadly issue. When ever i passed a property / parameter to the web service - i was getting a null parameter there. I tried all the possible ways of sending the params, nothing worked. Finally found that the namespace name needs to end with a "/" else you will face with this kinda issues. There is one more notable thing here, we need to tell the soapEnvelope that this is going to communicate with a dotNet service. So soapenv.dotNet = true; is a compulsory thing !!

Once you have taken care about all these things, hope u can get it to work ;) more posts on its way !! If you have doubts post it here.