1

我尝试了很多解决方案,但模拟器仍然说,不幸的是,当我尝试打开 webview 时,应用程序已停止。

这是我的 Webviewactivity.java 代码:

  package com.example.jpg;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Webviewactivity extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          //setContentView(R.layout.main);

          // Let's display the progress in the activity title bar, like the
          // browser app does.
          getWindow().requestFeature(Window.FEATURE_PROGRESS);

          WebView webview = new WebView(this);
          setContentView(webview);

          webview.getSettings().setJavaScriptEnabled(true);

          final Activity activity = this;
          webview.setWebChromeClient(new WebChromeClient() {
          public void onProgressChanged(WebView view, int progress) {
               // Activities and WebViews measure progress with different scales.
               // The progress meter will automatically disappear when we reach 100%
               activity.setProgress(progress * 1000);
          }
        });

webview.setWebViewClient(new WebViewClient() {

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
          //Users will be notified in case there's an error (i.e. no internet connection)
          Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
 });
       //This will load the webpage that we want to see
        webview.loadUrl("http://www.google.com");

     }
}

这是我的日志:

01-07 10:54:54.829: E/AndroidRuntime(940): FATAL EXCEPTION: main
01-07 10:54:54.829: E/AndroidRuntime(940): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.jpg/com.example.jpg.Webviewactivity}; have you declared this activity in your AndroidManifest.xml?
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1541)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Activity.startActivityForResult(Activity.java:3351)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Activity.startActivityForResult(Activity.java:3312)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Activity.startActivity(Activity.java:3522)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Activity.startActivity(Activity.java:3490)
01-07 10:54:54.829: E/AndroidRuntime(940):  at com.example.jpg.MainActivity$1.onClick(MainActivity.java:32)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.view.View.performClick(View.java:4084)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.view.View$PerformClick.run(View.java:16966)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.os.Handler.handleCallback(Handler.java:615)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.os.Looper.loop(Looper.java:137)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-07 10:54:54.829: E/AndroidRuntime(940):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:54:54.829: E/AndroidRuntime(940):  at java.lang.reflect.Method.invoke(Method.java:511)
01-07 10:54:54.829: E/AndroidRuntime(940):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-07 10:54:54.829: E/AndroidRuntime(940):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-07 10:54:54.829: E/AndroidRuntime(940):  at dalvik.system.NativeStart.main(Native Method)
01-07 10:54:57.249: I/Process(940): Sending signal. PID: 940 SIG: 9
01-07 10:54:54.829: E/AndroidRuntime(940): FATAL EXCEPTION: main
01-07 10:54:54.829: E/AndroidRuntime(940): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.jpg/com.example.jpg.Webviewactivity}; have you declared this activity in your AndroidManifest.xml?
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1541)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Activity.startActivityForResult(Activity.java:3351)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Activity.startActivityForResult(Activity.java:3312)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Activity.startActivity(Activity.java:3522)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.Activity.startActivity(Activity.java:3490)
01-07 10:54:54.829: E/AndroidRuntime(940):  at com.example.jpg.MainActivity$1.onClick(MainActivity.java:32)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.view.View.performClick(View.java:4084)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.view.View$PerformClick.run(View.java:16966)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.os.Handler.handleCallback(Handler.java:615)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.os.Looper.loop(Looper.java:137)
01-07 10:54:54.829: E/AndroidRuntime(940):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-07 10:54:54.829: E/AndroidRuntime(940):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:54:54.829: E/AndroidRuntime(940):  at java.lang.reflect.Method.invoke(Method.java:511)
01-07 10:54:54.829: E/AndroidRuntime(940):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-07 10:54:54.829: E/AndroidRuntime(940):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-07 10:54:54.829: E/AndroidRuntime(940):  at dalvik.system.NativeStart.main(Native Method)
01-07 10:54:57.249: I/Process(940): Sending signal. PID: 940 SIG: 9

请帮忙。我对此非常着急。谢谢你。

4

1 回答 1

1

在 LogCat 中,它会告诉您在添加内容之前必须调用 requestFeature()。

这是您可以解决的方法:

      @Override
       public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // Let's display the progress in the activity title bar, like the
       // browser app does.
       super.getWindow().requestFeature(Window.FEATURE_PROGRESS);
       setContentView(R.layout.main);
       WebView webview = new WebView(this);
       setContentView(webview);
于 2014-11-16T03:45:34.660 回答