我正在开发一个具有webview的android应用。我遵循了这个tutorial,但是我的活动仍然不是从Web视图开始。如何开始活动?这是我的代码

    public class MainActivity extends ActionBarActivity {

    WebView mWebView;
    ProgressBar progressBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bundle extras=getIntent().getExtras();

        mWebView = (WebView) findViewById(R.id.webView1);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.setWebViewClient(new WebViewClient() {



            @Override
            public void onPageFinished(WebView view, String url) {
                //hide loading image
                findViewById(R.id.imageLoading1).setVisibility(View.GONE);
                //show webview
                findViewById(R.id.webView1).setVisibility(View.VISIBLE);
            }




        });
        mWebView.loadUrl("http://fashion.example.com/my-account/");
    }



    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.equals("http://fashion.example.com/my-account/lost-password/")) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
                return false;
            }else{
                view.loadUrl(url);
                return true;
            }
        }

  }
}


我的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/vert_loading"
    tools:context="com.apks.mumbaifashion.MainActivity" >

        <ImageView android:id="@+id/imageLoading1"
        android:layout_width="match_parent"
    android:layout_height="match_parent"
        />

    <ProgressBar
            android:id="@+id/progressbar"
            android:layout_width="40dp"
            style="?android:attr/progressBarStyleLarge"
            android:layout_height="40dp"
            android:layout_centerInParent="true"
            android:visibility="visible"
            android:progressDrawable="@drawable/progress"/>

    <WebView android:id="@+id/webView1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:visibility="gone"
        />

</RelativeLayout>


Logcat

08-06 03:23:03.853: D/AndroidRuntime(1168): Shutting down VM
08-06 03:23:03.853: W/dalvikvm(1168): threadid=1: thread exiting with uncaught exception (group=0xb2aadba8)
08-06 03:23:03.893: E/AndroidRuntime(1168): FATAL EXCEPTION: main
08-06 03:23:03.893: E/AndroidRuntime(1168): Process: com.apks.mumbaifashion, PID: 1168
08-06 03:23:03.893: E/AndroidRuntime(1168): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.apks.mumbaifashion/com.apks.mumbaifashion.MainActivity2}: java.lang.ClassCastException: com.apks.mumbaifashion.MainActivity2 cannot be cast to android.app.Activity
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.os.Looper.loop(Looper.java:136)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.app.ActivityThread.main(ActivityThread.java:5001)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invoke(Method.java:515)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at dalvik.system.NativeStart.main(Native Method)
08-06 03:23:03.893: E/AndroidRuntime(1168): Caused by: java.lang.ClassCastException: com.apks.mumbaifashion.MainActivity2 cannot be cast to android.app.Activity
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-06 03:23:03.893: E/AndroidRuntime(1168):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
08-06 03:23:03.893: E/AndroidRuntime(1168):     ... 11 more
08-06 03:23:06.653: I/Process(1168): Sending signal. PID: 1168 SIG: 9


我在哪里做错了?

最佳答案

您已正确设置了自定义MyWebViewClient,但在下面一行中,只需将其替换为自定义mWebView.setWebViewClient。尝试此操作,同时替换两个onPageFinished行:

mWebView.setWebViewClient(new MyWebViewClient() {

    @Override public void onPageFinished(WebView view, String url) {
        //hide loading image
        findViewById(R.id.imageLoading1).setVisibility(View.GONE);
        //show webview
        findViewById(R.id.webView1).setVisibility(View.VISIBLE);
    }

});


这是解决方案之一。您还可以在自定义MyWebViewClient类中实现shouldOverrideUrlLoading,或在WebViewClient的匿名子类中实现。

10-04 11:42