-2

My app crashes when clicking on the image button but I don't know why it happens because I'm new in android app development. I don't know how to use intent. Said to me any web or youtube channel to learn android app dev in the best way.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/img"
        android:textColor="@color/purple_700"
        android:textStyle="bold"
        android:textSize="45sp"
        android:layout_marginStart="82dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" app:srcCompat="@drawable/dr1" android:layout_alignParentStart="true"
        android:layout_marginStart="88dp" android:layout_alignParentTop="true" android:layout_marginTop="87dp"
        android:id="@+id/imageButton" android:layout_centerHorizontal="true"/>
     <ImageButton
        android:layout_width="231dp"
        android:layout_height="316dp" app:srcCompat="@drawable/dr2" android:layout_alignParentStart="true"
        android:layout_marginStart="98dp" android:layout_alignParentBottom="true"
        android:layout_marginBottom="61dp" android:id="@+id/imageButton2" android:layout_centerHorizontal="true"/>


    </RelativeLayout>`

I think I don't correctly use intent if that is true pls comment problem in intent

Mainactivity.kt

package com.dualact.picvew

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageButton

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
val mb1 : ImageButton = findViewById(R.id.imageButton)
val mb2 : ImageButton = findViewById(R.id.imageButton2)
    mb1.setOnClickListener(){
        val pic = Intent(this, PicviewActivity::class.java)
        startActivity(pic)
    }
    mb2.setOnClickListener{
        val pic = Intent(this, PicviewActivity::class.java)
        startActivity(pic)
    }
}
}

picview.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
            android:layout_height="match_parent"
      xmlns:tools="http://schemas.android.com/tools"
            tools:context=".PicviewActivity">

<ImageView
        android:layout_width="405dp"
        android:layout_height="match_parent" app:srcCompat="@drawable/dr1" android:layout_alignParentStart="true"
        android:layout_marginStart="6dp" android:layout_alignParentTop="true" android:layout_marginTop="0dp"
        android:id="@+id/imageview"/>
</RelativeLayout>

Picviewactivity.kt

 package com.dualact.picvew

  import android.content.Intent
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import android.widget.ImageView

class PicviewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val mv:ImageView= findViewById(R.id.imageview)
    setContentView(R.layout.picview);
}
}
Zain
  • 37,492
  • 7
  • 60
  • 84
BI IYI
  • 1
  • 1
  • Use Logcat to examine the stack trace associated with your crash: https://commonsware.com/Jetpack/pages/chap-debug-001.html – CommonsWare Dec 31 '22 at 18:39

1 Answers1

1

The issue is in your PreviewActivity, you should call setContentView before fetching views. Change your onCreate() of PreviewActivity as below

package com.dualact.picvew

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView

class PicviewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.picview);
val mv:ImageView= findViewById(R.id.imageview)

 }
}