在Android的NativeScript中创建片段时抛出Resources $ NotFoundException

时间:2019-04-05 17:08:12

标签: android android-fragments nativescript

我试图在Nativescript中创建一个Android Fragment,目的是将Mapbox映射放入其中,以便可以访问Fragment生命周期回调。

但是,我一直遇到这个我不理解的异常:

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)

谷歌搜索了几个小时,结果却很少。

文档尚不清楚,但是Placeholder标签似乎是用于插入本机控件的正确标签,所以我猜这就是我将用于插入Android Fragment的地方。

这正确吗?

因此,我在组件html中有一个占位符标签:

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

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

作为基于许多代码片段的概念组件实现的简单证明,我提出了:


    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;

  } 

并且在我的mapfragment.android.ts文件中,我拥有:


    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;

    } 

我认为我缺少明显的东西,或者可能是对片段有根本的误解。我无法在NativeScript中找到任何明确的示例。

如果我在容器上调用addView(),则会显示内容,但它会掩盖动作栏并占据整个屏幕。

如果返回nativeView,则会出现异常。

我不确定我缺少什么或者这是否是错误。

是否有一个示例演示了如何在Nativescript中执行此操作?

我正在跑步:

✔ 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.

1 个答案:

答案 0 :(得分:1)

PEBKAC-键盘和椅子之间存在问题。

基于一项建议,即查看异常是否跟踪有占位符标记的情况,我发现我错误地调用了setFragmentClass(MapFragment)。

为了使示例生效,我还添加了:

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

设置为onCreateView(),并将creatingView()修改为:

  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; 
}