Estou seguindo a aula, mas meu programa quebrou e não sei o que estou fazendo errado
AndroidManifest.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.orgs">
<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.Orgs"
tools:targetApi="31" >
<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>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="108dp"
tools:layout_editor_absoluteY="99dp"
tools:listitem="@layout/produto_item"/>
</androidx.constraintlayout.widget.ConstraintLayo
produto_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/nome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Frutas"
android:layout_margin="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/descricao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
tools:text="Laranja"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/nome"/>
<TextView
android:id="@+id/valor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="R$5.00"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/descricao"/>
</androidx.constraintlayout.widget.ConstraintLay
E por fim
MainActivity
package com.example.orgs
import android.app.Activity
import android.os.Bundle
import android.widget.TextView
class MainActivity : Activity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val nome = findViewById<TextView>(R.id.nome)
}
}
Com o código assim o programa executa sem erros. Apenas que aparece apenas uma tela branca
quando chamo nome.text="Salada de Frutas"
ou qualquer outra das strings do exemplo o programa quebra
no logcat tem algo assim:
(022-08-26 16:20:09.625 31365-31365/com.example.orgs E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.orgs, PID: 31365 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.orgs/com.example.orgs.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference)