在Android Studio中从片段打印HTML文档

在Android Studio中从片段打印HTML文档,android,android-studio,printing,Android,Android Studio,Printing,我正在尝试制作一个Android应用程序,在一个片段上向HTML文档添加数据,然后在另一个片段上单击按钮打印出该文档。我试图使用Android文档中的页面作为指南,但当点击按钮时,按钮没有任何作用。在模拟器上测试时,我没有在logcat中看到任何内容,但在真实设备上测试时,我看到以下错误: E/DatabaseIndexingManager:找不到类名为com.android.settings.print.PrintSettingsFragment的SearchIndexableResource

我正在尝试制作一个Android应用程序,在一个片段上向HTML文档添加数据,然后在另一个片段上单击按钮打印出该文档。我试图使用Android文档中的页面作为指南,但当点击按钮时,按钮没有任何作用。在模拟器上测试时,我没有在logcat中看到任何内容,但在真实设备上测试时,我看到以下错误:

E/DatabaseIndexingManager:找不到类名为com.android.settings.print.PrintSettingsFragment的SearchIndexableResources

更新:尽管我还没有改变任何东西,但现在只要选择了片段,应用程序就会崩溃。logcat现在给出以下错误:

E/ActivityManager:com.google.android.ims中的ANR
PID:3635
原因:意图广播{act=android.telephony.action.CARRIER\u CONFIG\u CHANGED flg=0x15000010 cmp=com.google.android.ims/.receivers.CarrierConfigChangedReceiver(有额外功能)}

然后,它继续给出一系列关于CPU使用情况的统计数据

片段代码:

public class CalendarFragment extends Fragment {
    private WebView mWebView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Button button = container.findViewById(R.id.button);

        //declaring OnClickListener as an object
        View.OnClickListener btnClick = (View v) ->  doWebViewPrint();

        button.setOnClickListener(btnClick);

        return inflater.inflate(R.layout.placeholder2, container, false);
    }

    private void doWebViewPrint() {
        // Create a WebView object specifically for printing
        WebView webView = new WebView(getActivity());
        webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
        });
        webView.loadUrl("file:///android_asset/calendar.html");
        mWebView = webView;
    }
    private void createWebPrintJob(WebView webView) {

        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) getActivity()
                .getSystemService(Context.PRINT_SERVICE);

        String jobName = getString(R.string.app_name) + " Document";

        // Get a print adapter instance
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName);

        printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());

    }
}
Placeholder 2.xml(稍后我将更改名称):



正确的方法是什么?

我试图在打开OnClickListener之前设置它。我移动了这些行:

Button-Button=container.findviewbyd(R.id.Button);
//将OnClickListener声明为对象
View.OnClickListener btnClick=(视图v)->doWebViewPrint();
按钮。setOnClickListener(btnClick);
到onViewCreated函数。雷迪托注意到了我的错误,这要归功于他

<?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">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wv">
    </WebView>
    <Button
        android:id="@+id/button"
        android:layout_marginStart="30dp"
        android:layout_marginEnd="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:layout_centerVertical="true"
        android:backgroundTint="@color/colorPrimary"
        android:drawableStart="@drawable/ic_action_print"
        android:paddingStart="50dp"
        android:paddingEnd="50dp"
        android:textColor="#fff"/>
</RelativeLayout>