ViewStub.inflate on a null object reference: when i use PdfViewCtrlTabFragment2 to show PDF doc

Product: PDFTron Android SDK

Product Version:Latest

Please give a brief summary of your issue:ViewStub.inflate on a null object reference: when i use PdfViewCtrlTabFragment2 to show PDF doc.
(Think of this as an email subject)

Please describe your issue and provide steps to reproduce it:

Please correct me where have i implemented wrong & what should be the correct approach.
I have used ,
val fragment = ViewerBuilder2
.withFile(file)
.usingConfig(getConfig())
.usingTabClass(PdfFragment::class.java)
.build(activity)

    // Add the fragment to the layout fragment container
    activity.supportFragmentManager.beginTransaction()
        .replace(fragmentContainer, fragment)
        .commit()

class PdfFragment : PdfViewCtrlTabFragment2() {}

Error:

2021-08-12 12:39:31.768 9959-9959/com.pdftron.android.pdfviewer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pdftron.android.pdfviewer, PID: 9959
java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View android.view.ViewStub.inflate()’ on a null object reference
at com.pdftron.pdf.controls.PdfViewCtrlTabBaseFragment.loadStubPDFViewCtrl(PdfViewCtrlTabBaseFragment.java:3272)
at com.pdftron.pdf.controls.PdfViewCtrlTabBaseFragment.loadPDFViewCtrlView(PdfViewCtrlTabBaseFragment.java:3283)
at com.pdftron.pdf.controls.PdfViewCtrlTabFragment2.loadPDFViewCtrlView(PdfViewCtrlTabFragment2.java:276)
at com.pdftron.pdf.controls.PdfViewCtrlTabBaseFragment.onViewCreated(PdfViewCtrlTabBaseFragment.java:1088)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:501)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
at android.app.Activity.performStart(Activity.java:8024)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-08-12 12:39:31.803 9959-9959/com.pdftron.android.pdfviewer I/Process: Sending signal. PID: 9959 SIG: 9

(The more descriptive your answer, the faster we are able to help you)

Please provide a link to a minimal sample where the issue is reproducible:

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi,

Could you confirm if you are calling PDFNet.initialize() ?
Please see here for more info: add-license

Best Regards,
Eamon

Hi Emallon,
Thanks for the reply. I do not have any commercial licence key. Now, I am checking the feasibility for my app functionalities.

AndroidManifest file looks like,




<application
    android:name="androidx.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
    <activity
        android:name=".PdfFragmentActivity"
        android:theme="@style/PDFTronAppTheme"
        android:windowSoftInputMode="adjustPan"></activity>
    <!-- Reference to our PDFTron license key -->
    <meta-data
        android:name="pdftron_license_key"
        android:value="${pdftronLicenseKey}" />

Project properties file :slight_smile:

android.enableJetifier=true
android.useAndroidX=true
PDFTRON_LICENSE_KEY=YOUR_PDFTRON_LICENSE_KEY_GOES_HERE

Do i need to add purchased licence key to achieve this behaviour?

You do not need a valid license key to run the project. But we are having a lot of trouble reproducing this on our end. Could you try out any of our sample project, for example pdftron-android-samples/CustomUI at master · PDFTron/pdftron-android-samples · GitHub to see if it works on your end? If the sample works and your project does not, could you try to compare them? Thanks.

Hi Shirley,

Thank you for the reply.

I have cross checked, the app has been configured as per add-license for free licence user.

I do not have any crash issue when i am using default fragment to show the pdf ( Case -1 )

App is getting crash when i use MyFragment as PdfViewCtrlTabFragment2 passing as param in “usingTabClass()” method ( Case -2 ).

============== Case 1 ===============
val fragment = ViewerBuilder2
.withFile(file)
.usingConfig(getConfig())
.build(this, PdfViewCtrlTabHostFragment2::class.java)

    // Add the fragment to the layout fragment container
    activity.supportFragmentManager.beginTransaction()
        .replace(fragmentContainer, fragment)
        .commit()

============ Case 2 ===========

val fragment = ViewerBuilder2
.withFile(file)
.usingConfig(getConfig())
.usingTabClass(PdfTabFragment::class.java)
.build(this)

    // Add the fragment to the layout fragment container
    activity.supportFragmentManager.beginTransaction()
        .replace(fragmentContainer, fragment)
        .commit()

where
class PdfTabFragment : PdfViewCtrlTabFragment2() { }

Any git sample or your input, where we can use fragment extending PdfViewCtrlTabFragment2() to show the pdf would be grateful

Our React Native wrapper actually does this. The custom tab class is RNPdfViewCtrlTabFragment and it is added here. If it still does not work for you, please feel free to zip and upload your project here so we can take a look. Thanks.

Hi Shirley,

I have attached sample app zip file here WeTransfer.
Please look into this. Your suggestion/ input will be helpful.

Thank you.

Hi, I’m unsure what you are trying to achieve here… but of course it won’t work… In your “onCreateView” you called “val view = inflater.inflate(R.layout.fragment_pdf, container, false)”. so you basically got rid of everything from the base class PdfViewCtrlTabFragment2… this is not how class extension work on Android. Please take a look at pdftron-react-native/RNPdfViewCtrlTabFragment.java at master · PDFTron/pdftron-react-native · GitHub to see how others do class extension. Thanks.

What exactly you are trying to achieve here? Typically you just want to overwrite some functions/behaviour when doing class extension.

Hi Shirley,
Thanks for the reply. I understood, we do not need onCreateView() when we extend PdfViewCtrlTabFragment2 base class.

The requirement is ,when i do long-press on the document, one custom annotation should create on the touch rect point.

I want to use:

  1. ViewerConfig.Builder().longPressQuickMenuEnabled(false)
  2. Show custom annotation

what would you suggest to use Fragment or PDFViewCtrl view?

You can still extend PdfViewCtrlTabFragment2, but the only thing you need to override is “onLongPress” and add in your own logic, could you please give it a try? Thanks.