使片段成为反应原生错误中的UI组件

时间:2017-11-10 11:10:15

标签: android-fragments react-native-android

当我尝试将片段化为UI组件时,我遇到了问题。

下面的代码在第一次渲染in native native时运行良好,但是当组件重新渲染时,App会因transaction?.replace(R.id.id_empty_content, EmptyFragment(), "EmptyFragment")而崩溃。错误显示没有查看id_empty_content for fragment.Is片段在组件未计数时不会被破坏但是id_empty_content已经消失了?希望有人能帮助我解决这个问题。

ViewManager:

class EmptyFragmentManager : SimpleViewManager<RelativeLayout>() {

    override fun createViewInstance(reactContext: ThemedReactContext?): RelativeLayout{
        val manager = reactContext?.currentActivity?.fragmentManager
        val transaction = manager?.beginTransaction()
        val view = LayoutInflater.from(reactContext).inflate(R.layout.layout_empty, null, false) as RelativeLayout
        transaction?.replace(R.id.id_empty_content, EmptyFragment(), "EmptyFragment")
        transaction?.commit()
        return view
    }

    override fun getName(): String = "Empty"

}

layout_empty.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/id_empty_content"></FrameLayout>
</RelativeLayout>

用途:

import React, { Component } from 'react'
import { StyleSheet, Platform } from 'react-native'
import { Container, } from 'native-base'

import { colors, constants } from '../config'
import { SessionList, Empty } from '../components/sdk'

const styles = StyleSheet.create({
  container: {
    flex: 1,
    position: 'relative',
    backgroundColor: colors.white,
  },
  session_list: {
    flex: 1,
    marginTop: constants.STATUS_BAR_HEIGHT,
  },
})

export class InfoListPage extends Component {
  render () {
    return (
      <Container style={styles.container}>
        {Platform.OS === 'ios' ? null : <Empty style={styles.session_list}/>}
      </Container>
    )
  }
}

错误:

    java.lang.IllegalArgumentException: No view found for id 0x7f100acf (cn.samuel.pro:id/id_empty_content) for fragment EmptyFragment{4d538b #0 id=0x7f100acf EmptyFragment}

0 个答案:

没有答案