java .lang.Class не имеет конструктора с нулевым аргументом - PullRequest
0 голосов
/ 30 января 2020

Привет, может кто-нибудь помочь мне с кодом android java? потому что я не знаю, что является причиной этой ошибки:

Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joshua.r0th.crud2/com.joshua.r0th.crud2.SqliteHelper}: java.lang.InstantiationException: java.lang.Class<com.joshua.r0th.crud2.SqliteHelper> has no zero argument constructor

вы можете знать и помочь мне. я пытаюсь использовать представление корзины и получить данные из базы данных, пожалуйста, укажите что-то, потому что я так застрял здесь, это класс, который вызывает базу данных

public class ToolsFragment extends Fragment {


    TextView norumah, jentikdalam, jentikluar;

    private RecyclerView recyclerView;
    private ArrayList<adapterdata> items=new ArrayList<adapterdata>();
    private database1 database;
    public adapterdata adapter1;
    private vieholderadapter adapter2;
    private Cursor cursor;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView =(ViewGroup) inflater.inflate(R.layout.fragment_riwayat, container, false);
        recyclerView=rootView.findViewById(R.id.recycleView);
        loadDatabase();
        return rootView;
    }
    public void loadDatabase(){
        database = new database1(getActivity());
        try {
            database.cekdatabasecopas();
            database.opendatabase();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            cursor = database.QueryData("SELECT * FROM data_jentik");
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        adapterdata item = new adapterdata();
                        item.setNomorRumah(cursor.getString(1));
                        item.setJentikDalam(cursor.getString(2));
                        item.setJentikLuar(cursor.getString(3));
                        items.add(item);
                    } while (cursor.moveToNext());

                }

            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        adapter2= new vieholderadapter(getActivity(),items);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter2);
    }
}

, и это моя база данных

public class database1 extends SQLiteOpenHelper {
    public SQLiteDatabase sqLiteDatabase;

    private static final int VERSION = 1;
    public static final String DBNAME = "JENTIK.db";
    public static final String TABLENAME = "data_jentik";
    private static String db_path = "";
    public static String colID = "id";
    public static String COL_1 = "NomorRumah";
    public static String COL_2 = "JentikDalam";
    public static String COL_3 = "JentikLuar";
    private final Context mycontext;

    public database1(Context context) {
        super(context, DBNAME, null, VERSION);
        this.mycontext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLENAME + " (" +
                colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_1 + " TEXT," + COL_2 + "  TEXT," +
                COL_3 + " TEXT)";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLENAME);
        onCreate(db);
    }

    //metode untuk tambah data

    public boolean insertData(String NomorRumah, String JentikDalam, String JentikLuar) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();

        contentValues.put(COL_1, NomorRumah);

        contentValues.put(COL_2, JentikDalam);

        contentValues.put(COL_3, JentikLuar);


        long result = db.insert(TABLENAME, null, contentValues);

        if (result == -1)

            return false;

        else

            return true;

    }

    //metode untuk mengambil data

    public Cursor getAllData() {

        SQLiteDatabase db = this.getWritableDatabase();

        Cursor res = db.rawQuery("select * from data_jentik", null);

        return res;

    }

    public void updateData(int id, String NomorRumah, String JentikDalam, String JentikLuar) {
        String updateData = "UPDATE " + TABLENAME + " SET " + COL_1 + "= '" + NomorRumah + "', " + COL_2 + "= '" + JentikDalam + "'," + COL_3 + "='" + JentikLuar + "' WHERE " + colID + " =" + id;
        this.getWritableDatabase().execSQL(updateData);
    }

    public void deleteData(int id) {
        String deleteData = "DELETE FROM " + TABLENAME + " WHERE id=" + id;
        this.getWritableDatabase().execSQL(deleteData);
    }

    public datamodel getData(int id) {
        datamodel model = null;
        String selectData = "SELECT * FROM " + TABLENAME + " WHERE id=" + String.valueOf(id);
        Cursor data = this.getWritableDatabase().rawQuery(selectData, null);
        if (data.moveToFirst()) {
            model = new datamodel(Integer.parseInt(data.getString(data.getColumnIndex(colID))),
                    data.getString(data.getColumnIndex(COL_1)), data.getString(data.getColumnIndex(COL_2)), data.getString(data.getColumnIndex(COL_3)));
        }
        return model;
    }

    public List<datamodel> getAll() {
        List<datamodel> model = new ArrayList<>();
        String selectData = "SELECT * FROM " + TABLENAME;
        Cursor data = this.getWritableDatabase().rawQuery(selectData, null);
        if (data.moveToPosition(1)) {
            do {
                model.add(new datamodel(Integer.parseInt(data.getString(data.getColumnIndex(colID))),
                        data.getString(data.getColumnIndex(COL_1)), data.getString(data.getColumnIndex(COL_2)), data.getString(data.getColumnIndex(COL_3))));
            } while (data.moveToNext());
        }
        return model;
    }

    public Cursor queueAll() {
        String[] columns = new String[]{colID, COL_1, COL_2, COL_3};
        Cursor cursor = sqLiteDatabase.query(TABLENAME, columns,
                null, null, null, null, null);

        return cursor;
    }


    public Cursor getlistall() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLENAME, null);
        return data;
    }

    // Getting All Countries
    public List ListAllData() {
        List listdata = new ArrayList();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLENAME;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                adapterdata adapter1 = new adapterdata();
                adapter1.setNomorRumah(cursor.getString(1));
                adapter1.setJentikDalam(cursor.getString(2));
                adapter1.setJentikLuar(cursor.getString(3));
                // Adding country to list
                listdata.add(adapter1);
            } while (cursor.moveToNext());
        }

        // return country list
        return listdata;
    }

    // Deleting single country
    public void deleteCountry(adapterdata adapter1) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLENAME, KEY_ID + " = ?",
                new String[]{String.valueOf(adapter1.getNomorRumah())});
        db.close();
    }

    // Updating single country
    public int updateData1(adapterdata adapter) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COL_1, adapter.getNomorRumah());
        values.put(COL_2, adapter.getJentikDalam());
        values.put(COL_3, adapter.getJentikLuar());

        // updating row
        return db.update(TABLENAME, values, COL_1 + " = ?",
                new String[]{String.valueOf(adapter.getNomorRumah())});
    }

    // Adding new country
    void addCountry(adapterdata country) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COL_1, country.getNomorRumah()); // Country Name
        values.put(COL_2, country.getJentikDalam()); // Country Population
        values.put(COL_3, country.getJentikLuar()); // Country Population
        // Inserting Row
        db.insert(TABLENAME, null, values);
        db.close(); // Closing database connection
    }

    public Cursor QueryData(String query) {
        return sqLiteDatabase.rawQuery(query, null);

    }

    public void cekdatabasecopas() {
        boolean dbexist = cekdatabase();
        if (dbexist) {
            Log.d("TAG", "database already Exist");
        } else {
            this.getReadableDatabase();

        }
    }

    public void copyDatabase() throws IOException {
        InputStream myinput = mycontext.getAssets().open(DBNAME);
        String outfilename = db_path + DBNAME;
        OutputStream myoutput = new FileOutputStream(outfilename);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myinput.read(buffer)) > 0) {
            myoutput.write(buffer, 0, length);

        }
        myoutput.flush();
        myoutput.close();
        myinput.close();
    }

    public boolean cekdatabase() {
        SQLiteDatabase checkdb = null;
        try {
            String mypath = db_path + DBNAME;
            checkdb = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (checkdb != null) {
                checkdb.close();
            }
            return checkdb != null ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public void opendatabase(){
        String mypath = db_path+DBNAME;
        sqLiteDatabase=SQLiteDatabase.openDatabase(mypath,null,SQLiteDatabase.OPEN_READWRITE);
    }
}

кто-нибудь знает в чем причина проблемы? спасибо, я застрял здесь на мгновение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...