【发布时间】:2019-04-19 16:13:11
【问题描述】:
我正在尝试使用 webview 布局启动活动,而不打开新的 web 浏览器。所以它会被嵌入到应用程序中。
似乎它工作了几天,现在......我只是得到一个白色的空白屏幕。你能帮我一下吗?
这是我的 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/backBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:height="50dip"
android:background="#4d867f"
android:text="Back"
android:textColor="#f9f9f9"
android:textSize="20dip" />
</LinearLayout>
</RelativeLayout>
我是这样称呼这个活动的:
promotionBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, PromotionActivity.class);
intent.putExtra("url", "https://www.test.com/apppromotion.html");
startActivity(intent);
}
});
最后是活动:
package br.com.test.androidtools;
import android.content.Intent;
import android.net.Uri;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
import android.webkit.WebViewClient;
public class PromotionActivity extends Activity {
private WebView webView;
private Button backBtn;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
Intent it = new Intent(PromotionActivity.this, MainActivity.class);
startActivity((Intent)it);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.promotion_layout);
backBtn = (Button) findViewById(R.id.backBtn);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
Bundle bundle = getIntent().getExtras();
webView.loadUrl(bundle.getString("url"));
webView.setWebViewClient(new myWebViewClient());
backBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent it = new Intent(PromotionActivity.this, MainActivity.class);
startActivity((Intent)it);
}
});
}
public class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
有什么办法可以解决这个问题吗? 谢谢!
【问题讨论】:
-
调试这个,在 webView.loadUrl(bundle.getString("url")); 之前,使用 Log.d("test", "url = " + bundle.getString("网址"));那里设置正确吗?此外,请检查您的日志以了解您的 WebView 可能打印的任何警告。
-
嗨弗兰克!它正在获取正确的网址...
-
但我也收到此错误:“E/chromium: [ERROR:buffer_manager.cc(313)] [.Parent-Compositor-0xe2f17b00]GL ERROR :GL_INVALID_OPERATION : glBufferData: