-3

I'm trying to export my database using FAB. This is the error

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.qrac1/com.example.qrac1.ClassInfoActivity}: java.lang.InstantiationException: java.lang.Class (com.example.qrac1.ClassInfoActivity) has no zero argument constructor

Everything was working until I inserted this codes:

FloatingActionButton fab1 = (FloatingActionButton) findViewById(R.id.fab1);
    fab1.setOnClickListener(new View.OnClickListener() {
        Intent sIntent = getIntent();
        ExampleClass sClass = sIntent.getParcelableExtra("selected");
        String selCode = sClass.getqCode();

        @Override
        public void onClick(View v) {
            exportDataBaseIntoCSV();
        }
    });
}



public void exportDataBaseIntoCSV(){
    Intent sIntent = getIntent();
    ExampleClass sClass = sIntent.getParcelableExtra("selected");
    String selCode = sClass.getqCode();

    ClassDB db = new ClassDB(context);//here CredentialDb is my database. you can create your db object.
    File exportDir = new File(Environment.getExternalStorageDirectory(), "");

    if (!exportDir.exists())
    {
        exportDir.mkdirs();
    }

    File file = new File(exportDir, selCode +".csv");

    try
    {
        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
        SQLiteDatabase sql_db = db.getReadableDatabase();//here create a method ,and return SQLiteDatabaseObject.getReadableDatabase();
        Cursor curCSV = sql_db.rawQuery("SELECT * FROM "+selCode,null);
        csvWrite.writeNext(curCSV.getColumnNames());

        while(curCSV.moveToNext())
        {
            //Which column you want to export you can add over here...
            String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2), curCSV.getString(3), curCSV.getString(4), curCSV.getString(5), curCSV.getString(6), curCSV.getString(7), curCSV.getString(8), curCSV.getString(9), curCSV.getString(10), curCSV.getString(11)};
            csvWrite.writeNext(arrStr);
        }

        csvWrite.close();
        curCSV.close();
    }
    catch(Exception sqlEx)
    {
        Log.e("Error:", sqlEx.getMessage(), sqlEx);
    }

}

This is the first time that I see this error so I don't have any idea how can I fix this.

  • 1
    I don't see any use of `java.lang.Class` in your code, so are you sure that's the code causing the error? – Andreas Sep 09 '19 at 16:07
  • @Andreas I edited the error. It didn't read the part – Jerome Agda Sep 09 '19 at 16:13
  • What is `ClassInfoActivity`? Does it have a zero argument constructor? If not, why not? It obviously needs one to be part of an activity. – Andreas Sep 09 '19 at 16:59
  • What is the purpose of the 3 fields in the `View.OnClickListener` anonymous class? – Andreas Sep 09 '19 at 17:01
  • Did you try a web search for the error message "Unable to instantiate activity"? There are a lot of articles on the subject, so it's extremely likely that the cause of your problem is already covered by one of them. Please do your own **research** before asking a question here. – Andreas Sep 09 '19 at 17:05
  • 1
    Please [edit] your question to provide a [mcve] that demonstrates the issue, as well as the complete [stack trace from the crash](https://stackoverflow.com/a/23353174). It is unlikely that the stated Exception is coming from the given code. – Mike M. Sep 10 '19 at 01:37

2 Answers2

0

It seems like the object cant get instantiated.Try adding a parameterized constructor in the class.

0

I think you should not write this code:

Intent sIntent = getIntent();
ExampleClass sClass = sIntent.getParcelableExtra("selected");
String selCode = sClass.getqCode();

Inside your new View.OnClickListener() anonymous class. It is not in any method, and is already written inside exportDataBaseIntoCSV() method.

Yoav Gibri
  • 158
  • 5