嗨,我的应用程序正在强制关闭,我不知道我的代码有什么问题。任何人都可以帮助..
当我运行 apk 文件时,它说不幸的是应用程序已停止
包装代码
package net.learnscripture.webviewapp;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.net.MailTo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import net.learnscripture.webviewapp.R;
@SuppressWarnings("deprecation")
public class Dashboard extends Activity {
public String BASE_URL = "http://mixss.com/Mobile";
public String DASHBOARD_URL = BASE_URL + "dashboard/";
public String CONTACT_URL = BASE_URL + "contact/";
private JavascriptInterface jsInterface;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
WebView engine = (WebView) findViewById(R.id.web_engine);
// Progress bar.
// With full screen app, window progress bar (FEATURE_PROGRESS) doesn't seem to show,
// so we use an explicitly created one.
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar);
engine.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
progressBar.setProgress(progress);
}
});
engine.setWebViewClient(new FixedWebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
jsInterface.enablePreferencesMenu = false;
jsInterface.modalIsVisible = false;
jsInterface.urlForSharing = null;
progressBar.setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url)
{
progressBar.setVisibility(View.GONE);
}
});
engine.getSettings().setJavaScriptEnabled(true);
jsInterface = new JavascriptInterface();
try {
ComponentName comp = new ComponentName(this, Dashboard.class);
PackageInfo pinfo = getPackageManager().getPackageInfo(comp.getPackageName(), 0);
jsInterface.versionCode = pinfo.versionCode;
} catch(android.content.pm.PackageManager.NameNotFoundException e) {
}
engine.addJavascriptInterface(jsInterface, "androidlearnscripture");
engine.loadUrl(BASE_URL);
}
private WebView getEngine() {
return (WebView) findViewById(R.id.web_engine);
}
public void onBackPressed() {
WebView engine = getEngine();
String url = engine.getUrl();
if (jsInterface.modalIsVisible) {
engine.loadUrl("javascript: learnscripture.hideModal();");
} else if (url != null && (
url.equals(BASE_URL) ||
url.equals(DASHBOARD_URL) ||
!engine.canGoBack())) {
// exit
super.onBackPressed();
} else {
// go back a page, like normal browser
engine.goBack();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem prefs = menu.findItem(R.id.preferences_menuitem);
if (prefs != null) {
prefs.setVisible(jsInterface.enablePreferencesMenu);
}
super.onPrepareOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.dashboard_menuitem:
getEngine().loadUrl(DASHBOARD_URL);
return true;
case R.id.refresh_menuitem:
getEngine().reload();
return true;
case R.id.preferences_menuitem:
getEngine().loadUrl("javascript: learnscripture.showPreferences()");
return true;
case R.id.contact_menuitem:
getEngine().loadUrl(CONTACT_URL);
return true;
case R.id.share_url_menuitem:
final String url = (jsInterface.urlForSharing != null
? jsInterface.urlForSharing
: getEngine().getUrl());
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "LearnScripture URL");
i.putExtra(Intent.EXTRA_TEXT, url);
startActivity(Intent.createChooser(i, "Share URL"));
default:
return super.onOptionsItemSelected(item);
}
}
private class FixedWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(BASE_URL) || url.startsWith("javascript:")) {
// handle by the WebView
return false;
} else if (url.startsWith("mailto:")) {
MailTo mt = MailTo.parse(url);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{mt.getTo()});
i.putExtra(Intent.EXTRA_SUBJECT, mt.getSubject());
i.putExtra(Intent.EXTRA_CC, mt.getCc());
i.putExtra(Intent.EXTRA_TEXT, mt.getBody());
view.getContext().startActivity(i);
view.reload();
return true;
} else {
// Use external browser for anything not on this site
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(i);
return true;
}
}
}
// The methods of JavascriptInterface are called from javascript.
// The attributes are accessed from the Dashboard class.
// This is deliberately a dumb container class to stop possible
// security issues of javascript controlling Java app.
final class JavascriptInterface {
public boolean enablePreferencesMenu = false;
public boolean modalIsVisible = false;
public int versionCode = 0;
public String urlForSharing = null;
public void setEnablePreferencesMenu() {
enablePreferencesMenu = true;
}
public void setModalIsVisible(boolean visible) {
modalIsVisible = visible;
}
// This is useful for allowing the web site to be able to detect
// old app versions and prompt the user to upgrade.
public int getVersionCode() {
return versionCode;
}
public void setUrlForSharing(String url) {
urlForSharing = url;
}
}
}
布局 Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:max="100"
android:visibility="gone" />
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="a151b9a90b51ae7"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<WebView
android:id="@+id/web_engine"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
安卓清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
package="net.learnscripture.webviewapp"
android:versionCode="11"
android:versionName="1.5" >
<uses-sdk
android:minSdkVersion="6"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" />
<application
android:allowBackup="true"
android:configChanges="keyboard|keyboardHidden|orientation"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation"/>
<activity
android:name="net.learnscripture.webviewapp.Dashboard"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
日志猫
06-14 04:53:17.850: D/AndroidRuntime(1105): Shutting down VM
06-14 04:53:17.850: W/dalvikvm(1105): threadid=1: thread exiting with uncaught exception (group=0xb3f764f0)
06-14 04:53:17.850: I/Process(1105): Sending signal. PID: 1105 SIG: 9
06-14 04:53:17.850: D/AndroidRuntime(1105): procName from cmdline: net.learnscripture.webviewapp
06-14 04:53:17.850: E/AndroidRuntime(1105): in writeCrashedAppName, pkgName :net.learnscripture.webviewapp
06-14 04:53:17.850: D/AndroidRuntime(1105): file written successfully with content: net.learnscripture.webviewapp StringBuffer : ;net.learnscripture.webviewapp
06-14 04:53:17.850: E/AndroidRuntime(1105): FATAL EXCEPTION: main
06-14 04:53:17.850: E/AndroidRuntime(1105): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.learnscripture.webviewapp/net.learnscripture.webviewapp.Dashboard}: android.view.InflateException: Binary XML file line #16: Error inflating class com.google.ads.AdView
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.os.Looper.loop(Looper.java:130)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 04:53:17.850: E/AndroidRuntime(1105): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 04:53:17.850: E/AndroidRuntime(1105): at java.lang.reflect.Method.invoke(Method.java:507)
06-14 04:53:17.850: E/AndroidRuntime(1105): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
06-14 04:53:17.850: E/AndroidRuntime(1105): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
06-14 04:53:17.850: E/AndroidRuntime(1105): at dalvik.system.NativeStart.main(Native Method)
06-14 04:53:17.850: E/AndroidRuntime(1105): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.google.ads.AdView
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-14 04:53:17.850: E/AndroidRuntime(1105): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.app.Activity.setContentView(Activity.java:1660)
06-14 04:53:17.850: E/AndroidRuntime(1105): at net.learnscripture.webviewapp.Dashboard.onCreate(Dashboard.java:39)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 04:53:17.850: E/AndroidRuntime(1105): ... 11 more
06-14 04:53:17.850: E/AndroidRuntime(1105): Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView in loader dalvik.system.PathClassLoader[/data/app/net.learnscripture.webviewapp-1.apk]
06-14 04:53:17.850: E/AndroidRuntime(1105): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
06-14 04:53:17.850: E/AndroidRuntime(1105): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
06-14 04:53:17.850: E/AndroidRuntime(1105): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
06-14 04:53:17.850: E/AndroidRuntime(1105): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
06-14 04:53:17.850: E/AndroidRuntime(1105): ... 20 more