Android N:PrintManager.print()导致java.lang.IllegalStateException:只能从活动中打印 [英] Android N: PrintManager.print() results in java.lang.IllegalStateException: Can print only from an activity
问题描述
我的应用程序正在使用PrintManager进行PDF打印.此功能在Android L和M上运行正常,但在Android N下无法运行.
My app is using PDF printing using PrintManager. This functionality is running just fine in Android L and M but fails under Android N.
在onfragment活动中,从onOptionsItemSelected()的选项菜单中调用打印操作.
Print action is called within a non-fragment activity from options menu in onOptionsItemSelected().
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
String jobName = "createPDFReport";
printManager.print(jobName, new InterimReportAdapter(AuditValidation.this, auditObject), new PrintAttributes.Builder().build());
}
Android N抛出
Android N throws
FATAL EXCEPTION: ec.kat.kataudit, PID: 4052
java.lang.IllegalStateException: Can print only from an activity
at android.print.PrintManager.print(PrintManager.java:525)
at ec.kat.kataudit.AuditValidation.onOptionsItemSelected(AuditValidation.java:124)
at android.app.Activity.onMenuItemSelected(Activity.java:3204)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
...
AuditValidation.java:124指向printManager.print()调用.
AuditValidation.java:124 is pointing to printManager.print() call.
Activity扩展了android.support.v7.app.AppCompatActivity. 编译目标版本为25,应用程序正在使用最新的库(截至今天为25.3.1).
Activity extends android.support.v7.app.AppCompatActivity. Compile target version is 25, app is using latest libraries (25.3.1 as of today).
非常感谢任何想法!
最诚挚的问候!
推荐答案
发现问题.在我的活动的attachBaseContext()中,我实现了语言交换,Android N通过createConfigurationContext()创建了新的上下文.显然,这会使引用的PrintManager实例无效,从而导致上述IllegalStateException. 我现在必须为此找到解决方法.
Problem found. In attachBaseContext() of my activity I have a language swap implemented, which has Android N create a new context by createConfigurationContext(). This obviously voids the reference PrintManager instance is using, resulting in above IllegalStateException. I will now have to find a workaround for this.
我的解决方案是将对传递给attachBaseContext()的原始Context的引用存储在我的活动的成员中.然后通过调用原始Context引用(而不是活动的Context引用)上的getSystemService()来检索PrintManager实例.
My solution is to store a reference of the original Context passed to attachBaseContext() in a member of my activity. PrintManager instance then is retrieve by invoking getSystemService() on the original Context reference, not the than active one.
private Context primaryBaseActivity;//THIS WILL KEEP ORIGINAL INSTANCE
@Override
protected void attachBaseContext(Context newBase) {
primaryBaseActivity=newBase;//SAVE ORIGINAL INSTANCE
/*Some locale handling stuff right here*/
/*LocaleHelper's onAttach is returning a *new* context in Android N which will void PrintManager's context*/
super.attachBaseContext(LocaleHelper.onAttach(newBase,appLocale));
}
开始打印(生成PDF)时:
When starting printing (PDF generation):
PrintManager printManager = (PrintManager) primaryBaseActivity.getSystemService(Context.PRINT_SERVICE);
这篇关于Android N:PrintManager.print()导致java.lang.IllegalStateException:只能从活动中打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!