Yermo Lamers
Yermo Lamers

Reputation: 1951

Resources$NotFoundException thrown when creating a Fragment in NativeScript on Android

I am attempting to create an Android Fragment in Nativescript with the intent of putting a Mapbox map into it so I can get access to the Fragment lifecycle callbacks.

However, I keep running into this exception that I do not understand:

System.err: android.content.res.Resources$NotFoundException: Unable to find resource ID #0xfffffff6
System.err:     at android.content.res.ResourcesImpl.getResourceTypeName(ResourcesImpl.java:269)
System.err:     at android.content.res.Resources.getResourceTypeName(Resources.java:1965)
System.err:     at android.support.v4.app.FragmentManagerImpl.loadAnimation(FragmentManager.java:1172)
System.err:     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1808)
System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
System.err:     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
System.err:     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
System.err:     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
System.err:     at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
System.err:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
System.err:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
System.err:     at android.os.Handler.handleCallback(Handler.java:790)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
System.err:     at android.os.Looper.loop(Looper.java:164)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6518)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Googling around for hours has yielded few results.

The documentation is unclear, but it seems that the Placeholder tag is the correct tag to use to insert native controls so my guess was that this is what I would use to insert an Android Fragment.

Is this correct?

So, I have a Placeholder tag in my component html:

<ActionBar title="Ng Mapbox Demo" class="action-bar">
</ActionBar>

<StackLayout class="page" id="page">
  <Placeholder (creatingView)="creatingView($event)" id="mapContainer"></Placeholder>
</StackLayout> 

As a simple proof of concept component implementation based off a number of snippets, I've come up with:


    public static newInstance() : MapFragment {

      return new MapFragment();

    } 

  creatingView( args : CreateViewEventData ) {

    let fragment = MapFragment.newInstance();

    let fragmentManager = args.context.getSupportFragmentManager();

    let fragmentTransaction = fragmentManager.beginTransaction(); 

    fragmentTransaction.replace( fragment.getId(), fragment );

    fragmentTransaction.commit();

    args.view = fragment;

  } 

and in my mapfragment.android.ts file I have:


    public onCreateView( inflater: android.view.LayoutInflater, container: android.view.ViewGroup, savedInstanceState: android.os.Bundle ) {

      let result = super.onCreateView( inflater, container, savedInstanceState );

      // just doing this as an experiment to see if I can get it working.

      let nativeView = new android.widget.TextView( this.getActivity() );
      nativeView.setSingleLine(true);
      nativeView.setEllipsize(android.text.TextUtils.TruncateAt.END);
      nativeView.setText("Native");

// If I uncomment this line, the content appears but it takes over the
// entire screen clobbering the actionbar.
// 
//      container.addView( nativeView );

// if I return the nativeView like this I get the exception.

      return nativeView;

    } 

I figure I'm missing something obvious or maybe have a fundamental misunderstanding of Fragments. I have not been able to find any clear examples of how to do this in NativeScript.

If I call addView() on the container the content appears but it clobbers the actionbar and takes over the whole screen.

If I return the nativeView, I get the exception.

I'm not sure what I'm missing or whether this is a bug.

Is there an example that demonstrates how to do this in Nativescript somewhere?

I am running:

✔ Getting NativeScript components versions information...
✔ Component nativescript has 5.3.1 version and is up to date.
✔ Component tns-core-modules has 5.3.1 version and is up to date.
✔ Component tns-android has 5.3.1 version and is up to date.

Upvotes: 0

Views: 115

Answers (1)

Yermo Lamers
Yermo Lamers

Reputation: 1951

PEBKAC - Problem exists between keyboard and chair.

Based on the suggestion of seeing if the exception tracks with the presence of the Placeholder tag, I discovered I was mistakenly calling setFragmentClass(MapFragment).

To get the example to work I also added:

nativeView.setId( android.view.View.generateViewId() ); 

to onCreateView() and modified creatingView() to:

  creatingView( args : CreateViewEventData ) {

    let fragment = new MapFragment();

    let fragmentManager = args.context.getSupportFragmentManager(); 
    let fragmentTransaction = fragmentManager.beginTransaction();
    let framelayout = new android.widget.FrameLayout( args.context );
    framelayout.setId(android.view.View.generateViewId());

    fragmentTransaction.add( framelayout.getId(), fragment );

    fragmentTransaction.commit();

    args.view = framelayout; 
}

Upvotes: 1

Related Questions