gpt4 book ai didi

android - 在 webview 中单击链接时如何启动 Activity ?

转载 作者:行者123 更新时间:2023-11-30 01:58:19 26 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,它有一个 webview。我跟着这个tutorial但是我的 Activity 仍然不是从 WebView 开始的。我该如何开始 Activity ?这是我的代码

    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>

日志

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 行:

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 类中实现 onPageFinished,或者在 WebViewClient 的匿名子类中实现 shouldOverrideUrlLoading

关于android - 在 webview 中单击链接时如何启动 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31848526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com