我正在开发一个具有webview的Android应用程序。我遵循了这个tutorial,但我的活动仍未从网络视图开始。我如何开始活动?这是我的代码
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
我做错了什么?
答案 0 :(得分:1)
您正在设置自定义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);
}
});
这是解决方案之一。您还可以在自定义onPageFinished
课程中实施MyWebViewClient
,或在shouldOverrideUrlLoading
的匿名子课程中实施WebViewClient
。
答案 1 :(得分:0)
我建议你直接从onPageFinished
类似的东西:
@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);
}
答案 2 :(得分:0)
您必须覆盖shouldOverrideUrlLoading
:
这应该做:
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;
}
}
}
<强>更新强>
<强>例外:强>
Caused by: java.lang.ClassCastException:
com.apks.mumbaifashion.MainActivity2
cannot be cast to android.app.Activity
<强>原因:强>
您的MainActivity2
课程似乎无法延长Activity
。只有manifest
和StackTrace
才能看到这一切。