0
I’m having difficulty finding the solution to this problem, in the code I want to go from Activity "Mainactivity" to "Plantalist", but when testing the application in the virtual machine the app closes after this action and in the terminal displays the following error:
-17 16:05:44.130 2082-2082/com.example.adriano.campanha_flora_pre_alpha E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.adriano.campanha_flora_pre_alpha, PID: 2082 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adriano.campanha_flora_pre_alpha/com.example.adriano.campanha_flora_pre_alpha.PlantaList}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.adriano.campanha_flora_pre_alpha.PlantaList.onCreate(PlantaList.java:32)at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
Follow Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/campanha_flora_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PlantaList"/>
<activity android:name=".ConsultaAdapter" />
<activity android:name=".TelaCadastro"/>
</application>
Follow Main Activity
package com.example.adriano.campanha_flora_pre_alpha;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button botaocadastrar = (Button) findViewById(R.id.btnCadastrar);
botaocadastrar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TelaCadastro.class));
}
});
Button botaoconsultar = (Button) findViewById(R.id.btnConsultar);
botaoconsultar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, PlantaList.class));
}
});
Button botaopreferencias = (Button) findViewById(R.id.btnPreferencias);
botaopreferencias.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, TelaCadastro.class));
}
});
}
}
Segue Plantalist:
package com.example.adriano.campanha_flora_pre_alpha;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.GridView;
import java.util.ArrayList;
public class PlantaList extends AppCompatActivity {
GridView gridView;
ArrayList<Planta> list;
ConsultaAdapter adapter = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tela_consulta);
gridView = (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
adapter = new ConsultaAdapter(this, R.layout.tela_consulta_2, list);
gridView.setAdapter(adapter);
// get all data from sqlite
Cursor cursor = TelaCadastro.sqLiteHelper.getData("SELECT * FROM PLANTA");
list.clear();
while (cursor.moveToNext()){
int id = cursor.getInt(0);
String NomePopular = cursor.getString(1);
String Especie = cursor.getString(2);
String Familia = cursor.getString(3);
String TipoDeRaiz = cursor.getString(4);
String CorDeFlor = cursor.getString(5);
byte[] image = cursor.getBlob(6);
list.add(new Planta(NomePopular, Especie, Familia, TipoDeRaiz, CorDeFlor, image, id));
}
adapter.notifyDataSetChanged();
}
}
What can it be?
Check whether
gridView
orcursor
is void.– ramaral