I want to implement javascript in my android application. I was successful in implementing that using PhoneGap. Now my application runs the webpage with javascript, but there is one small problem. I want to place an AdMob banner at the bottom, which i have already implemented in my other application, so i followed the same procedure but that is resulting in force close error. If i run the application without android layout and code it runs perfectly but not with it.
import org.apache.cordova.DroidGap;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
public class MainActivity extends DroidGap {
private LayoutInflater mInflater;
private View paramView;
@Override
public void onCreate(Bundle savedInstanceState) {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
LayoutParams params = new LayoutParams(height, width);
paramView = mInflater.inflate(R.layout.activity_main, null);
super.loadUrl("file:///android_asset/www/webPageWithJavascript.htm");
super.addContentView(paramView, params);
AdView adview = (AdView) findViewById(R.id.adView);
AdRequest re = new AdRequest();
adview.loadAd(re);
}
}
Log Cat
11-02 10:15:09.031: E/AndroidRuntime(1249): FATAL EXCEPTION: main
11-02 10:15:09.031: E/AndroidRuntime(1249): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.myapplication/com.test.myapplication.MainActivity}: java.lang.NullPointerException
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.os.Handler.dispatchMessage(Handler.java:99)
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.os.Looper.loop(Looper.java:130)
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-02 10:15:09.031: E/AndroidRuntime(1249): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 10:15:09.031: E/AndroidRuntime(1249): at java.lang.reflect.Method.invoke(Method.java:507)
11-02 10:15:09.031: E/AndroidRuntime(1249): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-02 10:15:09.031: E/AndroidRuntime(1249): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-02 10:15:09.031: E/AndroidRuntime(1249): at dalvik.system.NativeStart.main(Native Method)
11-02 10:15:09.031: E/AndroidRuntime(1249): Caused by: java.lang.NullPointerException
11-02 10:15:09.031: E/AndroidRuntime(1249): at com.test.myapplication.MainActivity.onCreate(MainActivity.java:26)
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-02 10:15:09.031: E/AndroidRuntime(1249): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-02 10:15:09.031: E/AndroidRuntime(1249): ... 11 more
It sounds strange but the same code worked after restarting the computer. I don’t know the reason but i thought let me restart the computer and try it again. After restarting the computer, I started eclipse and clicked on run button and the application was working perfectly.