Anshul Khare Anshul Khare - 4 months ago 10
Android Question

How to start activity when a link is clicked in webview?

I am developing an android app, which has a webview. I followed this tutorial but my Activity is still not starting from web view.How do i start activity? Here is my code

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;
}
}

}
}


my xml file

<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


Where i am doing wrong?

Answer

You are correctly setting your custom MyWebViewClient, but in the following line you just replace it with a custom one. Try this, replacing both mWebView.setWebViewClient lines:

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);
    }

});

This is one of the solutions. You can also implement onPageFinished in your custom MyWebViewClient class, or implement shouldOverrideUrlLoading in the anonymous child class of WebViewClient.

Comments