片段布局为空白

Posted

技术标签:

【中文标题】片段布局为空白【英文标题】:Fragment Layout is blank 【发布时间】:2021-05-13 00:29:34 【问题描述】:

我正在传递一个联系人类型的对象(联系人),在 RecyclerView 中单击并在另一个活动中使用意图和序列化将其显示在片段中。但是内容是空白的,没有显示字段或标题。空白布局不确定问题出在哪里...

使用片段调用活动

        holder.itemView.setOnClickListener 
        val intent = Intent(context, ContactDetailsHome::class.java)
        intent.putExtra("contact", contact as Serializable)
        intent.putExtra("Edit", true)
        context.startActivity(intent)
    

生成布局的片段联系人详细信息

类 ContactDetails : Fragment()

companion object 
    @JvmStatic
    fun start(context: Context, contact: Contact?, isEdit: Boolean) 
        val starter = Intent(context, ContactDetails::class.java)
            .putExtra("contact", contact as Serializable)
            .putExtra("Edit", isEdit)
        context.startActivity(starter)
    


private lateinit var contact: Contact
private lateinit var numEditList: ArrayList<EditText>
private lateinit var emailEditList: ArrayList<EditText>


override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? 
    // Inflate the layout for this fragment
    inflater.inflate(R.layout.fragment_view_contact_details, container, false)

    numEditList = ArrayList()
    emailEditList = ArrayList()

    return view


override fun onViewCreated(view: View, savedInstanceState: Bundle?) 

    if (activity?.intent?.getBooleanExtra("Edit", false) == true)
        initEdit()
    else
        initAdd()
    

    delete_contact!!.setOnClickListener 
        deleteContact()
        Toast.makeText(activity, "Contact Deleted Successfully", Toast.LENGTH_SHORT).show()
        activity?.finish()
    



private fun initEdit() 
    //getting the value from the object
    contact = (activity?.intent?.getSerializableExtra("contact") as Contact)
    //assigning it to fields to be displayed
    contDetails_name.setText(contact.name)

    if (contact.numbers.isEmpty()) 
        phoneTitle!!.visibility = View.GONE
    

    if (contact.emails.isEmpty()) 
        title_emails!!.visibility = View.GONE
    

    for (number in contact.numbers) 
        val editText = EditText(activity)
        editText.setText(number)
        editText.setTextColor(Color.parseColor("#0D0D0D"))
        editText.typeface = Typeface.DEFAULT_BOLD
        editText.textSize = 20f
        editText.isEnabled = false
        numEditList.add(editText)
        contact_numbers_layout!!.addView(editText)
    

    for (email in contact.emails) 
        val editText = EditText(activity)
        editText.setText(email)
        editText.setTextColor(Color.parseColor("#0D0D0D"))
        editText.typeface = Typeface.DEFAULT_BOLD
        editText.textSize = 20f
        editText.isEnabled = false
        emailEditList.add(editText)
        contact_emails_layout!!.addView(editText)
    

    edit_contact!!.setOnClickListener 
        if (contDetails_name.isEnabled) 
            if (contDetails_name.text.toString().isEmpty()) 
                Toast.makeText(activity, "Contact name can't be empty", Toast.LENGTH_SHORT)
                    .show()
            
            contDetails_name.isEnabled = false
            for (editText in numEditList) 
                editText.isEnabled = false
            

            for (editText in emailEditList) 
                editText.isEnabled = false
            

            deleteContact()
            addContact()
            edit_contact.text = "Edit"
            Toast.makeText(activity, "Contact updated successfully", Toast.LENGTH_SHORT).show()
         else 
            contDetails_name.isEnabled = true
            for (editText in numEditList) 
                editText.isEnabled = true
            

            for (editText in emailEditList) 
                editText.isEnabled = true
            

            edit_contact.text = "Save"
        
    


布局文件

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".Contacts.ui.ContactDetails">

<androidx.cardview.widget.CardView
    android:layout_
    android:layout_
    android:layout_margin="10dp"
    android:padding="10dp"
    app:cardCornerRadius="10dp">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <TextView
            android:layout_
            android:layout_
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="8dp"
            android:text="Name"
            android:textAllCaps="true"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/contDetails_name"
            android:layout_
            android:layout_
            android:layout_marginHorizontal="20dp"
            android:enabled="false"
            android:hint="Contact Name"
            android:inputType="textPersonName"
            android:textColor="@color/txtcolor"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/phoneTitle"
            android:layout_
            android:layout_
            android:layout_margin="20dp"
            android:layout_marginTop="8dp"
            android:text="Phone/Mobile No:"
            android:textAllCaps="true"
            android:visibility="visible"
            android:textSize="16sp" />

        <LinearLayout
            android:id="@+id/contact_numbers_layout"
            android:layout_
            android:layout_
            android:layout_marginHorizontal="20dp"
            android:orientation="vertical" />

        <TextView
            android:id="@+id/title_emails"
            android:layout_
            android:layout_
            android:layout_margin="20dp"
            android:layout_marginTop="8dp"
            android:text="Email"
            android:textAllCaps="true"
            android:textSize="16sp" />

        <LinearLayout
            android:id="@+id/contact_emails_layout"
            android:layout_
            android:layout_
            android:layout_marginHorizontal="20dp"
            android:orientation="vertical" />

        <Button
            android:id="@+id/edit_contact"
            android:layout_
            android:layout_
            android:layout_marginHorizontal="30dp"
            android:layout_marginTop="20dp"
            android:background="@color/colorPrimary"
            android:text="Edit"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:id="@+id/delete_contact"
            android:layout_
            android:layout_
            android:layout_marginHorizontal="30dp"
            android:layout_marginTop="20dp"
            android:background="@color/colorPrimary"
            android:layout_marginBottom="8dp"
            android:text="Delete"
            android:textColor="#fff"
            android:textSize="20sp" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

【问题讨论】:

【参考方案1】:

发现我没有在oncreateView创建过程中声明视图,然后返回。导致没有视图被生成,因此空白片段。

Val 视图:视图 = .... 返回视图

【讨论】:

以上是关于片段布局为空白的主要内容,如果未能解决你的问题,请参考以下文章

片段中的Android webView显示空白页面

使用 Laravel 在主布局中输出为空白

片段内的LinearLayout总是在顶部留下空白

使用选项卡第二次返回片段显示空白片段

加载带有片段的 SherlockFragmentActivity 时屏幕长时间空白

多个视图与多个片段