Após implementar o Adapter, ocorre uma quebra de App com o seguinte erro no logcat:
2020-12-15 10:29:19.748 8152-8152/com.example.aluraviagens E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aluraviagens, PID: 8152
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aluraviagens/com.example.aluraviagens.ui.activity.ListaPacotesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.aluraviagens.ui.activity.ListaPacotesActivity.onCreate(ListaPacotesActivity.java:25)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Ele diz que a causa desse erro é uma NullPointException na linha 25 da minha ListaPacotesActivity, em que o código é esse:
package com.example.aluraviagens.ui.activity;
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.aluraviagens.R;
import com.example.aluraviagens.dao.PacoteDAO;
import com.example.aluraviagens.model.Pacote;
import com.example.aluraviagens.ui.adapter.ListaPacotesAdapter;
import java.util.List;
public class ListaPacotesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_pacotes);
ListView listaDePacotes = findViewById(R.id.lista_pacotes_listview);
List<Pacote> pacotes = new PacoteDAO().lista();
listaDePacotes.setAdapter(new ListaPacotesAdapter(pacotes, this));
}
}
E meu adapter:
package com.example.aluraviagens.ui.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.aluraviagens.R;
import com.example.aluraviagens.model.Pacote;
import java.util.List;
public class ListaPacotesAdapter extends BaseAdapter {
private final List<Pacote> pacotes;
private Context context;
public ListaPacotesAdapter(List<Pacote> pacotes, Context context) {
this.pacotes = pacotes;
this.context = context;
}
@Override
public int getCount() {
return pacotes.size();
}
@Override
public Pacote getItem(int position) {
return pacotes.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View viewCriada = LayoutInflater.from(context).inflate(R.layout.item_pacote, parent, false);
return viewCriada;
}
}