Search code examples
crashandroid-sqlite

What is making my app crash without an error


I am writing an app. that you can search info about celebs and save it to a SQlite database. I followed tutorials in showing the information but when I run the app it crashes without an error... please assist

When I request the data I only want the names display in a Listview, then when the user clicks on the name of the Celeb, it will open a page with all the saved data..I have no idea where to even start looking for the problem..I can open the app, save the data but when I open the activity to show the ListView it crashes without an error...

my xml for displaying the listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/mway"
    android:orientation="vertical"
    tools:context=".MainActivity">
        <TextView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:background="@color/colorPrimaryDark"
            android:text="@string/app_name"
            android:textAlignment="center"
            android:textColor="@color/colorPrimary"
            android:textSize="32sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_view"
        android:background="@drawable/mway">
    </ListView>
</LinearLayout>

Here is my java file for displaying the listview

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class viewceleb extends AppCompatActivity {

    DatabaseHelper db;
    ArrayList<String> listItems;
    ArrayAdapter adapter;
    ListView userList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewceleb);

        userList = findViewById(R.id.list_view);
        listItems = new ArrayList<>();

        viewData();

        userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                String text = userList.getItemAtPosition(i).toString();
                Toast.makeText(viewceleb.this, "Now something can happen", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void viewData() {
        Cursor cursor = db.viewData();

        if (cursor.getCount()== 0){
            Toast.makeText(this,"No Data To Show", Toast.LENGTH_LONG).show();
        }else {
            while(cursor.moveToNext()){
                listItems.add(cursor.getString(1));//index 1 is name, 0 is ID
            }

            adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);
            userList.setAdapter(adapter);

        }
    }
}

Solution

  • You are only declaring db, it needs to be instantiated.

    That is you have DatabaseHelper db; and you additionall need db = new DatabaseHelper(?); where ? are the parameters required by the DatabaseHelper constructor. Typically just the Context so perhaps db = new DatabaseHelper(this);. This would be coded in the activity's onCreate method BEFORE calling the viewData method.

    e.g.

    public class viewceleb extends AppCompatActivity {
    
        DatabaseHelper db;
        ArrayList<String> listItems;
        ArrayAdapter adapter;
        ListView userList;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_viewceleb);
            dn = new DatabaseHelper(this); //<<<<<<<<<< ADDED note just the context has been assumed, the parameters depend upon the constructor as per what is coded in the DatabaseHelper class.
    
            userList = findViewById(R.id.list_view);
            listItems = new ArrayList<>();
    
            viewData();
    
            userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                    String text = userList.getItemAtPosition(i).toString();
                    Toast.makeText(viewceleb.this, "Now something can happen", Toast.LENGTH_LONG).show();
                }
            });
        }
    
        private void viewData() {
            Cursor cursor = db.viewData();
    
            if (cursor.getCount()== 0){
                Toast.makeText(this,"No Data To Show", Toast.LENGTH_LONG).show();
            }else {
                while(cursor.moveToNext()){
                    listItems.add(cursor.getString(1));//index 1 is name, 0 is ID
                }
    
                adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);
                userList.setAdapter(adapter);
    
            }
        }
    }