Home > other >  How can 'no zero argument constructor' be avoided?
How can 'no zero argument constructor' be avoided?

Time:04-05

An activity that retrieves and displays data from a database failed to instantiate because there is no zero argument constructor - why is it important to include it?

Here is the activity (I'm quite new to this so there will be parts where it could have been executed better...open to suggestions!)

package com.example.createrecipefinal;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import java.util.List;

public class preview_recipe extends AppCompatActivity {
    private DBHelper dbHelper;
    private TextView viewRecipeName, viewCountry, viewTime, viewTxtEquipment, viewTxtIngredients, viewTxtMethod, viewPersonalStory;

    public preview_recipe(DBHelper dbHelper) {
        this.dbHelper = dbHelper;
    }

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

        viewRecipeName = findViewById(R.id.viewRecipeName);
        viewCountry = findViewById(R.id.viewCountry);
        viewTime = findViewById(R.id.viewTime);
        viewTxtEquipment = findViewById(R.id.viewTxtEquipment);
        viewTxtMethod = findViewById(R.id.viewTxtMethod);
        viewPersonalStory = findViewById(R.id.viewPersonalStory);
        viewTxtIngredients = findViewById(R.id.viewTxtIngredients);

        DBHelper dbHelper = new DBHelper(preview_recipe.this);
        //call read function and array
        List<RecipeModel> returnRecipe = dbHelper.readRecipe();

        //Setting values into text views
        viewRecipeName.setText((CharSequence) returnRecipe.get(1));
        viewCountry.setText((CharSequence) returnRecipe.get(2));
        viewTxtEquipment.setText((CharSequence) returnRecipe.get(5));
        viewTxtIngredients.setText((CharSequence) returnRecipe.get(6));
        viewTxtMethod.setText((CharSequence) returnRecipe.get(7));
        viewPersonalStory.setText((CharSequence) returnRecipe.get(8));
    }

}

2022-04-04 19:37:05.963 14815-14815/com.example.createrecipefinal E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.createrecipefinal, PID: 14815
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.createrecipefinal/com.example.createrecipefinal.preview_recipe}: java.lang.InstantiationException: java.lang.Class<com.example.createrecipefinal.preview_recipe> has no zero argument constructor
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3591)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     

CodePudding user response:

The zero argument constructor is required because the android runtime doesn't know how to create the object (DBHelper dbHelper) that your constructor requires.

The dbHelper that your constructor requires as useless anyway (you create a new DBHelper(preview_recipe.this) in onCreate()), so the solution is to remove this useless parameter:

public class preview_recipe extends AppCompatActivity {
    private TextView viewRecipeName, viewCountry, viewTime, viewTxtEquipment, viewTxtIngredients, viewTxtMethod, viewPersonalStory;

    public preview_recipe() {
    }

    //... remainder of the code
}

Please also note that in Java (and Android) it is the convention to write class names in CamelCase (like the AppCompatActivity), so your class should be named:

public class PreviewRecipe extends AppCompatActivity {
    private TextView viewRecipeName, viewCountry, viewTime, viewTxtEquipment, viewTxtIngredients, viewTxtMethod, viewPersonalStory;

    public PreviewRecipe() {
    }

    //... remainder of the code
}
  • Related