[FIXED] When opening a second activity, its just a black screen

Issue

I have an app with a recycler view as the first page you see, and based off which item you click, it should take you to a separate, more detailed page. The problem is that when it takes me to these separate pages, they are completely black, only showing the action bar title- even though there is content in the xml files. I’m very new to Kotlin and android development, so I would appreciate any type of help.

My Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.worthit">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.WorthIt"
        tools:targetApi="31">
        <activity
            android:name=".DetailsAct"
            android:exported="true"
            android:parentActivityName=".MainActivity"/>
        <activity
            android:name=".DetailsAct2"
            android:exported="true"
            android:parentActivityName=".MainActivity"/>
        <activity
            android:name=".DetailsAct3"
            android:exported="true"
            android:parentActivityName=".MainActivity"/>
        <activity
            android:name=".DetailsAct4"
            android:exported="true"
            android:parentActivityName=".MainActivity"/>
        <activity
            android:name=".DetailsAct5"
            android:exported="true"
            android:parentActivityName=".MainActivity"/>
        <activity
            android:name=".DetailsAct6"
            android:exported="true"
            android:parentActivityName=".MainActivity"/>
        <activity
            android:name=".DetailsAct7"
            android:exported="true"
            android:parentActivityName=".MainActivity"/>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My RecyclerView.kt

package com.example.worthit
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView

class Recycler(var app:Context): RecyclerView.Adapter<Recycler.ViewHolder>() {

    private  var titles = arrayOf("Sunday", "Monday" ,"Tuesday" ,"Wednesday", "Thursday", "Friday", "Saturday")
    private var details = arrayOf("SundayD", "MondayD","TuesdayD" ,"WednesdayD", "ThursdayD", "FridayD", "SaturdayD")
    private var images = intArrayOf(R.drawable.everyday_photo, R.drawable.everyday_photo, R.drawable.everyday_photo, R.drawable.everyday_photo, R.drawable.everyday_photo, R.drawable.everyday_photo, R.drawable.everyday_photo)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Recycler.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.card_layout, parent, false)
        return ViewHolder(v)
    }
    override fun getItemCount(): Int {
        return titles.size
    }

    override fun onBindViewHolder(holder: Recycler.ViewHolder, position: Int) {
        holder.itemTitle.text = titles[position]
        holder.itemDetail.text = details[position]
        holder.itemImage.setImageResource(images[position])
    }

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        var itemImage: ImageView
        var itemTitle: TextView
        var itemDetail : TextView

        init {
            itemImage = itemView.findViewById(R.id.item_image)
            itemTitle = itemView.findViewById(R.id.item_title)
            itemDetail = itemView.findViewById(R.id.item_detail)

            itemView.setOnClickListener {
                when(bindingAdapterPosition)
                {
                    0 -> app.startActivity(Intent(app, DetailsAct::class.java))
                    1 -> app.startActivity(Intent(app, DetailsAct2::class.java))
                    2 -> app.startActivity(Intent(app, DetailsAct3::class.java))
                    3 -> app.startActivity(Intent(app, DetailsAct4::class.java))
                    4 -> app.startActivity(Intent(app, DetailsAct5::class.java))
                    5 -> app.startActivity(Intent(app, DetailsAct6::class.java))
                    6 -> app.startActivity(Intent(app, DetailsAct7::class.java))
                }
            }
        }
    }
}

My MainActivity.kt

package com.example.worthit

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

    private var layoutManager: RecyclerView.LayoutManager? = null
    private var adapter: RecyclerView.Adapter<Recycler.ViewHolder>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        layoutManager = LinearLayoutManager(this)
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = layoutManager

        adapter = Recycler(this)
        recyclerView.adapter = adapter
    }
}

One of my DetailsAct.kt(one of the more detailed pages, the second activity page)

package com.example.worthit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class DetailsAct : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_details)

        val actionBar = supportActionBar

        if(actionBar != null)
        {
            actionBar.title = "SundayDetails"
            actionBar.setDisplayHomeAsUpEnabled(true)
        }
    }
}

example content for details page

EDIT:

Details XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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_width="wrap_content"
    android:layout_height="match_parent"
    tools:context=".DetailsAct">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="176dp"
        android:layout_marginTop="351dp"
        android:layout_marginEnd="177dp"
        android:layout_marginBottom="361dp"
        android:text="@string/sunday_text"
        android:textColor="#F44336"
        android:textSize="60sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <ImageView
        android:id="@+id/details1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/text1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.212"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/textd1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text1"
        app:layout_constraintVertical_bias="0.313" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

I’m not very clear your problem but you can consider a few things:

Make your constraint layout width match parent android:layout_width="match_parent".

Don’t use wrap_content with Imageview, replace it by 0dp. Then you can set padding or margin horizontal to scale it.

Don’t use tools:srcCompat, try android:src, example: android:src="@drawable/avatars". Don’t forget to create an avatar resource in drawable folder.

Hope it will solve your problem.

Answered By – KiluSs

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published