我想在我的 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);
}
}
原木猫
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