我有一个CustomWebViewClass:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class CustomWebView extends Activity{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
//progress bar optional
getWindow().requestFeature(Window.FEATURE_PROGRESS);
final Activity activity = this;
Toast.makeText(activity, "YO! " + url, Toast.LENGTH_SHORT).show();
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl(url);
}
}
来自另一个类(本质上是一个按钮)我试图将这个类称为一个意图,传递一个像这样的URL:
Intent webView = new Intent(getContext(), CustomWebView.class);
webView.putExtra("url", "http://google.com");
webView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(webView);
但我得到黑屏或错误..显然我做错了,请帮忙
哦,我的清单有这个:<activity android:name=".CustomWebView"
android:label="CustomWebView"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="com.sapientnitro.lcinstore2.CUSTOMWEBVIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 0 :(得分:1)
requestFeature()
:之前调用 setContentView
...
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(webview);
...
答案 1 :(得分:0)
当您在params
上传递Intent
时,可以使用getIntent().getExtra[...]
试试这个:
import android.app.Activity;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class CustomWebView extends Activity{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String url = getIntent().getStringExtra("url");
//super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
//progress bar optional
getWindow().requestFeature(Window.FEATURE_PROGRESS);
// alllow js
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
Toast.makeText(activity, "YO! " + url, Toast.LENGTH_SHORT).show();
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl(url);
}
}
答案 2 :(得分:0)
您没有使用参数调用它,您正在启动一个活动并在意图包中添加一个额外的字符串,您永远不会从该包中取出来使用它。
CustomWebView
:
Bundle extras = getIntent().getExtras();
String url = extras.getString("url");
答案 3 :(得分:0)
您似乎误解了Activity的构造函数与其onCreate()
方法之间的区别。
当您显式创建新对象时,将调用构造函数;
Activity customWebView = new CustomWebView(url);
这可能不是您想要创建新活动的方式。相反,你想使用意图系统(这是你开始做的)。
Intent webView = new Intent(getContext(), CustomWebView.class);
webView.putExtra("url", "http://google.com");
webView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(webView);
使用intent系统将调用一个空构造函数,然后一旦创建了Activity,就会调用onCreate()
方法,你应该覆盖它。从这里,您可以访问用于启动Activity的Intent,并且可以获取作为参数传递的URL。
你可能想要这样的东西:
public class CustomWebView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
//Do stuff with your URL
}
}