gpt4 book ai didi

Android 应用程序在拉动刷新操作后停止

转载 作者:行者123 更新时间:2023-12-02 07:32:12 24 4
gpt4 key购买 nike

我遇到了以下问题 :我正在使用 fragment 制作一个 android webview 应用程序,并且我想实现一个 SwipeToRefresh 操作。

我试图这样做,但应用程序在用户的拉动刷新操作后立即停止。
以下是不同的代码:

fragment_home.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light">

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/sr"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:id="@+id/webviewhome"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>

HomeFragment.java
公共(public)类 HomeFragment 扩展 fragment {
public HomeFragment(){
// Required
}

private SwipeRefreshLayout mRefreshLayout;
private WebView mWebView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_home, container, false);
mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.sr);
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mWebView.reload();
mRefreshLayout.setRefreshing(false);
}
});
final WebView webView = (WebView) v.findViewById(R.id.webviewhome);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("MYURL");
webView.setWebViewClient(new WebViewClient());
webView.canGoBack();

webView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
});

webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}

if (webView.canGoBack()) {
webView.goBack();
}

webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
alertDialog.setTitle("Erreur !");
alertDialog.setMessage("Veuillez vérifier votre connexion internet et réessayer");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Réessayer", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
startActivity(getActivity().getIntent());
}
});

alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});

return v;
}

你能帮我吗?

最佳答案

如果我们没有遗漏您的部分代码,那么您已经为 webview 创建了两个实例,在刷新布局中您正在调用尚未初始化的 mWebView。
试试这个:

public HomeFragment(){
// Required
}

private SwipeRefreshLayout mRefreshLayout;
private WebView WebView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_home, container, false);
mRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.sr);
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mWebView.reload();
mRefreshLayout.setRefreshing(false);
}
});
webView = (WebView) v.findViewById(R.id.webviewhome);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("MYURL");
webView.setWebViewClient(new WebViewClient());
webView.canGoBack();

webView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
});

webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}

if (webView.canGoBack()) {
webView.goBack();
}

webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
alertDialog.setTitle("Erreur !");
alertDialog.setMessage("Veuillez vérifier votre connexion internet et réessayer");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Réessayer", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
startActivity(getActivity().getIntent());
}
});

alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});

return v;
}

关于Android 应用程序在拉动刷新操作后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60336853/

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