在Android应用程序中实现javascript导致问题

时间:2012-11-02 06:34:30

标签: javascript android android-layout admob

我想在我的Android应用程序中实现javascript。我使用PhoneGap成功实现了这一点。现在我的应用程序使用javascript运行网页,但是有一个小问题。我想在底部放置一个AdMob横幅,我已经在我的其他应用程序中实现了,所以我遵循相同的过程,但这导致强制关闭错误。如果我在没有android布局和代码的情况下运行应用程序,它可以完美运行但不能运行它。

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

1 个答案:

答案 0 :(得分:0)

这听起来很奇怪但重启计算机后代码相同。我不知道原因,但我想让我重新启动计算机再试一次。重新启动计算机后,我启动了eclipse并单击了运行按钮,应用程序运行正常。