2

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?

Community
  • 1
  • 1
Anshul Khare
  • 391
  • 1
  • 4
  • 13

3 Answers3

1

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.

lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
  • Thank you for fast reply.I have replaced the following code.Now when i click on the link from which i have to start second Activity i am getting error of "unfortunately app has stopped" – Anshul Khare Aug 06 '15 at 07:05
  • Post your relevant portion from your logcat, you should see lot of details about your error. – lorenzo-s Aug 06 '15 at 07:07
  • I have posted the Logcat the error is "java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.apks.mumbaifashion/com.apks.mumbaifashion.YourActivity}: java.lang.ClassCastException: com.apks.mumbaifashion.YourActivity cannot be cast to android.app.Activity" – Anshul Khare Aug 06 '15 at 07:28
  • I can't see `com.apks.mumbaifashion.YourActivity` anywhere in your code. What is it and where are you using it? – lorenzo-s Aug 06 '15 at 07:29
  • Sorry its MainActivity2 instead of YourActivity. MainActivity2 is my second activity which i have to start from webview – Anshul Khare Aug 06 '15 at 07:36
  • If `MainActivity2` can't be casto to `Activity`, it means it is not an activity. Please make it extend `ActionBarActivity` (or some other subclass of `Activity`) like your `MainActivity` does. And declare it in the *AndroidManifest.xml* file. If you have difficulties doing that, post the content of *MainActivity2.java* and *AndroidManifest.xml*. Anyway, that's now a different problem from the original question. – lorenzo-s Aug 06 '15 at 07:40
  • Thank You Very Much Its Finally Worked, You Saved My Life :D – Anshul Khare Aug 06 '15 at 07:57
1

I suggest you fire an intent with the activity you want directly from onPageFinished

Something like:

@Override
public void onPageFinished(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);                
        }
moud
  • 729
  • 9
  • 19
1

You have to override shouldOverrideUrlLoading:

This should do:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.equals("YOURLINK")) {
            Intent intent = new Intent(getContext(), YourActivity.class);
            startActivity(intent);
            return false;
        }else{
            view.loadURL(url);
            return true;
        }
    }
}

UPDATE:

Exception:

Caused by: java.lang.ClassCastException:    
com.apks.mumbaifashion.MainActivity2 
cannot be cast to android.app.Activity

Reason:

Your MainActivity2 class doesn't seem to extend Activity. That's all I can see with only your manifest and StackTrace.

sjain
  • 23,126
  • 28
  • 107
  • 185