尝试在Android Java中打印Webview时出错

时间:2019-03-28 04:27:38

标签: java android printing webview

我正在构建一个Webview应用程序。我希望在用户单击时打印Webview内容。我正在使用以下代码来处理打印,但是一旦我点击工具栏菜单中的“打印”按钮,就会引发错误...

public  void createWebPagePrint(WebView webView) {
        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
        String jobName = getString(R.string.app_name) + " Document";
        PrintAttributes.Builder builder = new PrintAttributes.Builder();
        builder.setMediaSize(PrintAttributes.MediaSize.ISO_A5);
        PrintJob printJob = printManager.print(jobName, printAdapter, builder.build());

        if(printJob.isCompleted()){
            Toast.makeText(getApplicationContext(), "Print Completed !", Toast.LENGTH_LONG).show();
        }
        else if(printJob.isFailed()){
            Toast.makeText(getApplicationContext(), "Print Failed !", Toast.LENGTH_LONG).show();
        }
    }

Logcat显示这些错误。我无法找到解决方案...

java.lang.NullPointerException: Attempt to invoke virtual method 'android.print.PrintDocumentAdapter android.webkit.WebView.createPrintDocumentAdapter()' on a null object reference
        at my.andyapp.desi.ViewResults.createWebPagePrint(ViewResults.java:161)
at my.andyapp.desi.ViewResults.onOptionsItemSelected(ViewResults.java:151)
        at android.app.Activity.onMenuItemSelected(Activity.java:3556)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:436)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:196)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:64)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:204)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:781)
        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
        at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:128)
        at android.widget.AdapterView.performItemClick(AdapterView.java:318)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1250)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3267)
        at android.widget.AbsListView$6.run(AbsListView.java:4212)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6717)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:445)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)

我在这里调用打印命令

 //Toolbar Menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.refresh_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.refresh_webview:
                createWebPagePrint(webView);
                break;
        }
        return true;
    }

0 个答案:

没有答案