Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

Erro quando insiro o texto (.text) no MainActivity

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)

4 respostas

log cat completo:

2022-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 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4035) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.orgs.MainActivity.onCreate(MainActivity.kt:13) at android.app.Activity.performCreate(Activity.java:8290) at android.app.Activity.performCreate(Activity.java:8270) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4009) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 2022-08-26 16:20:09.642 31365-31365/? I/Process: Sending signal. PID: 31365 SIG: 9

Agradeço toda a ajuda que vier

solução!

Fala Fernando, tudo bem?

Cara o seu programa quebrou pq ainda não tem um Adapter para esse RecyclerView. O professor fala no final do video que o app não vai funcionar, mas fica tranquilo, que na aula seguinte ele já arruma isso!

Valeu! Bons Estudos!

Obrigado Leonardo