Tengo un problema al mostrar los datos en un listView
. El adaptador me da un error que pone nullpointerexception
pero no se que pasa, no encuentro el error. Os adjunto el código del adaptador y de la clase de datos:
En la base de datos no da ningun error me da error al llamar al adapter desde el metodo createlistview()
pero no me dice en que linea esta el error
Muchisimas gracias si me indicais el error o si me decir una manera para que esto funcione
Aquí tenéis todo el error que da android:
01-30 09:51:52.550 3541-3541/com.app.a2.studio.android.aprendiendo.appmanejarcuentas E/AndroidRuntime: FATAL EXCEPTION: main Process: com.app.a2.studio.android.aprendiendo.appmanejarcuentas, PID: 3541 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.a2.studio.android.aprendiendo.appmanejarcuentas/com.app.a2.studio.android.aprendiendo.appmanejarcuentas.Screens.MainScreen}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:203) at java.util.Arrays$ArrayList.(Arrays.java:3826) at java.util.Arrays.asList(Arrays.java:3813) at android.widget.ArrayAdapter.(ArrayAdapter.java:139) at com.app.a2.studio.android.aprendiendo.appmanejarcuentas.DataBase.FolderAdapter.(FolderAdapter.java:0) at com.app.a2.studio.android.aprendiendo.appmanejarcuentas.Screens.MainScreen.createListView(MainScreen.java:86) at com.app.a2.studio.android.aprendiendo.appmanejarcuentas.Screens.MainScreen.onCreate(MainScreen.java:50) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
La clase FolderDatos es así:
public class FolderDatos {
public int icon;
public String title;
public int ID;
public int numberUsers;
public FolderDatos(int icon, String title, int numberUsers, int ID){
this.icon = icon;
this.title = title;
this.numberUsers = numberUsers;
this.ID = ID;
}
}
Clase del adaptador:
package com.app.a2.studio.android.aprendiendo.appmanejarcuentas.DataBase;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.app.a2.studio.android.aprendiendo.appmanejarcuentas.R;
public class FolderAdapter extends ArrayAdapter<FolderDatos> {
private Context context;
private int layoutResourceID;
private FolderDatos[] folderDatos = null;
public FolderAdapter(Context context, int layoutResourceID, FolderDatos[] folderDatos){
super(context, layoutResourceID, folderDatos);
this.context = context;
this.layoutResourceID = layoutResourceID;
this.folderDatos = folderDatos;
}
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
FolderDatosHolder holder = null;
if(row == null){
LayoutInflater folderInflate = ((Activity)context).getLayoutInflater();
row = folderInflate.inflate(layoutResourceID, parent, false);
holder = new FolderDatosHolder();
holder.groupIcon = (ImageView) row.findViewById(R.id.iv_lvd1_GroupImage);
holder.groupName = (TextView) row.findViewById(R.id.tv_lvd1_GroupTitle);
holder.numberUsers = (TextView) row.findViewById(R.id.tv_lvd1_numberUsers);
holder.ID = (TextView) row.findViewById(R.id.tv_lvd1_ID);
row.setTag(holder);
}
else {
holder = (FolderDatosHolder) row.getTag();
}
FolderDatos folderDatos = this.folderDatos[position];
holder.groupIcon.setImageResource(folderDatos.icon);
holder.groupName.setText(folderDatos.title);
holder.numberUsers.setText(String.valueOf(folderDatos.numberUsers));
holder.ID.setText(String.valueOf(folderDatos.ID));
return (row);
}
private static class FolderDatosHolder{
ImageView groupIcon;
TextView groupName;
TextView numberUsers;
TextView ID;
}
}
La base de datos:
package com.app.a2.studio.android.aprendiendo.appmanejarcuentas.DataBase;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class BaseDeDatos extends SQLiteOpenHelper {
public static final String DB_NAME = "AppCuentas.db";
public static final String TABLE_FOLDERS = "Folders";
public static final String TF_COLOM_ID = "_ID";
public static final String TF_COLOM_NUMBER_USERS = "NumberUsers";
public static final String TF_COLOM_NAME = "Nombre";
public static final String TABLE_USUARIOS = "Usuarios";
public static final String TU_COLOM_PRIMARY_ID = "_ID";
public static final String TU_COLOM_FOLDER = "Folder";
public static final String TU_COLOM_NAME = "Nombre";
public static final String TU_COLOM_DESCRIPTION = "Descripcion";
public static final String TU_COLOM_PAGOS = "Pagos";
public BaseDeDatos(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_FOLDERS
+ " (" + TF_COLOM_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ TF_COLOM_NAME + " TEXT, "
+ TF_COLOM_NUMBER_USERS + " INTEGER) ");
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_USUARIOS
+ " (" + TU_COLOM_PRIMARY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ TU_COLOM_FOLDER + " TEXT, "
+ TU_COLOM_NAME + " TEXT, "
+ TU_COLOM_DESCRIPTION + " TEXT, "
+ TU_COLOM_PAGOS + " INTEGER) ");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXIST" + TABLE_FOLDERS);
sqLiteDatabase.execSQL("DROP TABLE IF EXIST" + TABLE_USUARIOS);
onCreate(sqLiteDatabase);
}
public boolean addData(String name, int numberUsers){
SQLiteDatabase db = getWritableDatabase();
ContentValues valuesFolders = new ContentValues();
valuesFolders.put(TF_COLOM_NAME, name);
valuesFolders.put(TF_COLOM_NUMBER_USERS, numberUsers);
long resultFolders = db.insert(TABLE_FOLDERS, null, valuesFolders);
if(resultFolders == -1){
return false;
}
else {
return true;
}
}
public boolean addData(int folder, String name, String description, float pagos){
SQLiteDatabase db = getWritableDatabase();
ContentValues valuesUsuarios = new ContentValues();
valuesUsuarios.put(TU_COLOM_FOLDER, folder);
valuesUsuarios.put(TU_COLOM_NAME, name);
valuesUsuarios.put(TU_COLOM_DESCRIPTION, description);
valuesUsuarios.put(TU_COLOM_PAGOS, pagos);
long resultUsuarios = db.insert(TABLE_USUARIOS, null, valuesUsuarios);
if(resultUsuarios == -1){
return false;
}
else {
return true;
}
}
public Cursor getAllFoldersData(){
String folderColoms[] = {TF_COLOM_ID, TF_COLOM_NAME, TF_COLOM_NUMBER_USERS};
Cursor c = this.getReadableDatabase().query(TABLE_FOLDERS, folderColoms, null, null, null, null, null);
return c;
}
public Integer deleteFolder(String folderID){
SQLiteDatabase db = this.getWritableDatabase();
Integer usuers = db.delete(TABLE_USUARIOS, "Folder = ?", new String[] { folderID });
Integer folders = db.delete(TABLE_FOLDERS, "ID = ?", new String[] { folderID});
if(usuers > 0){
if(folders > 0){
return 1;
}
else {
return -1;
}
}
else {
return -1;
}
}
}
y el listView:
private void createListView() {
db = new BaseDeDatos(this);
Cursor cursor = db.getAllFoldersData();
FolderDatos[] folderDatos = null;
FolderAdapter folderAdapter = null;
int ID, numberUsers;
String nombre;
if(cursor.moveToFirst()){
do{
ID = cursor.getInt(0);
numberUsers = cursor.getInt(2);
nombre = cursor.getString(1);
folderDatos = new FolderDatos[]{new FolderDatos(R.drawable.ic_group, nombre, numberUsers, ID)};
}while (cursor.moveToNext());
}
if(folderDatos == null){
folderAdapter = new FolderAdapter(this, R.layout.listview_design1, null);
}
else {
folderAdapter = new FolderAdapter(this, R.layout.listview_design1, folderDatos);
}
listview.setAdapter(folderAdapter);
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView tv_title = (TextView) view.findViewById(R.id.tv_lvd1_GroupTitle);
Toast.makeText(getApplicationContext(), tv_title.getText(), Toast.LENGTH_SHORT).show();
return true;
}
});
}