Android Studio : cannot be applied into given types

Neo Toshiya


I'm currently trying to learn SQLite in Android, and followed some tutorials on the internet.
Now I'm stuck with this one error, and I hope someone would point out my mistakes. I'm not sure what caused this problem, I've read most of these threads with the same question as me, but I still can't figure out how to fix it.

Here's the MainActivity.java

package com.nyonyo.sqlitetest;

import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    DatabaseHelper myDB;
    EditText firstname, lastname, marks, id;
    Button btnAdd;
    Button btnDel;
    Button btnView;
    Button btnUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myDB = new DatabaseHelper();

        firstname = (EditText) findViewById(R.id.editFirstName);
        lastname = (EditText) findViewById(R.id.editLastName);
        marks = (EditText)  findViewById(R.id.editMarks);
        id = (EditText) findViewById(R.id.edit_id);

        btnAdd = (Button) findViewById(R.id.button_add);
        btnDel = (Button) findViewById(R.id.button_delete);
        btnView = (Button) findViewById(R.id.button_viewAll);
        btnUpdate = (Button) findViewById(R.id.button_update);

        addData();
        deleteData();
        viewData();
        updateData();

    }

    private void addData() {
        btnAdd.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                boolean isInserted = myDB.insertData(firstname.getText().toString(),
                        lastname.getText().toString(),
                        marks.getText().toString());

                if (isInserted)
                    Toast.makeText(MainActivity.this,"Data Inserted", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(MainActivity.this,"Insert Data Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void deleteData() {
        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer delRow = myDB.deleteData(id.getText().toString());
                if(delRow > 0)
                    Toast.makeText(MainActivity.this,"Row Deleted",Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(MainActivity.this,"Delete Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void viewData() {
        btnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Cursor res = myDB.getData();
                if(res.getCount()==0){
                    showMessage("Error","No Data Found");
                    return;
                }
                StringBuffer buffer = new StringBuffer();
                while(res.moveToNext()){
                    buffer.append("ID :"+ res.getString(0)+"\n");
                    buffer.append("First Name :"+ res.getString(1)+"\n");
                    buffer.append("last Name :"+ res.getString(2)+"\n");
                    buffer.append("Marks :"+ res.getString(3)+"\n\n");
                }
            }
        });
    }

    public void showMessage(String title,String Message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(Message);
        builder.show();
    }


    private void updateData() {
        boolean isUpdate = myDB.updateData(id.getText().toString(),
                firstname.getText().toString(),
                lastname.getText().toString(),
                marks.getText().toString());

        if(isUpdate)
            Toast.makeText(MainActivity.this,"Database Updated", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(MainActivity.this,"Update Failed", Toast.LENGTH_SHORT).show();
    }
}

Here's the DatabaseHelper.java

package com.nyonyo.sqlitetest;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by NyoNyo on 07/08/2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "Student_table";
    public static final String COL_ID = "ID";
    public static final String COL_FIRSTNAME = "Firstname";
    public static final String COL_LASTNAME = "Lastname";
    public static final String COL_MARKS = "Marks";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("Create table" + TABLE_NAME +
                "(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                "FIRSTNAME TEXT," +
                "LASTNAME TEXT," +
                "MARKS INTEGER)");
    }

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

    public boolean insertData(String firstname, String lastname, String marks){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL_FIRSTNAME,firstname);
        values.put(COL_LASTNAME,lastname);
        values.put(COL_MARKS,marks);
        long result = db.insert(TABLE_NAME,null,values);
        if(result == -1)
            return false;
        else
            return true;
    }

    public Cursor getData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("SELECT * FROM"+ TABLE_NAME,null);
        return res;
    }

    public boolean updateData(String id, String firstname, String lastname, String marks){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL_ID, id);
        values.put(COL_FIRSTNAME, firstname);
        values.put(COL_LASTNAME, lastname);
        values.put(COL_MARKS, marks);
        db.update(TABLE_NAME, values, "ID = ?", new String[] {id});
        return true;
    }

    public Integer deleteData(String id){
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME,"ID = ?", new String[] {id});
    }
}

When I try to run the app, this error shows up.

Error:(28, 16) error: constructor DatabaseHelper in class DatabaseHelper cannot be applied to given types;
required: Context
found: no arguments
reason: actual and formal argument lists differ in length

This is the tutorial I'm following http://www.codebind.com/android-tutorials-and-examples/android-sqlite-tutorial-example/

Thanks in advance.

Is this the Stack Trace ? Not really sure.

08-08 09:50:52.968 22463-22463/? I/art: Late-enabling -Xcheck:jni08-08 09:50:53.429 22463-22463/com.nyonyo.sqlitetest W/System: ClassLoader referenced unknown path: /data/app/com.nyonyo.sqlitetest-1/lib/x8608-08 09:50:53.458 22463-22463/com.nyonyo.sqlitetest I/InstantRun: starting instant run server: is main process08-08 09:50:53.715 22463-22463/com.nyonyo.sqlitetest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable08-08 09:50:54.018 22463-22463/com.nyonyo.sqlitetest D/AndroidRuntime: Shutting down VM08-08 09:50:54.020 22463-22463/com.nyonyo.sqlitetest E/AndroidRuntime: FATAL EXCEPTION: main                                                                       Process: com.nyonyo.sqlitetest, PID: 22463                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyonyo.sqlitetest/com.nyonyo.sqlitetest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6186)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                           at com.nyonyo.sqlitetest.MainActivity.addData(MainActivity.java:48)
                                                                           at com.nyonyo.sqlitetest.MainActivity.onCreate(MainActivity.java:40)
                                                                           at android.app.Activity.performCreate(Activity.java:6684)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751) 
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6186) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 08-08 09:50:54.055 22463-22463/com.nyonyo.sqlitetest I/Process: Sending signal. PID: 22463 SIG: 9
John Joe

In your MainActivity, you miss out this. Thats why you will get actual and formal argument lists differ in length error.

 myDB = new DatabaseHelper(this);

You also miss setContentView(R.layout.activity_main);. Place it after super.onCreate(savedInstanceState);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

constructor in class cannot be applied to given types android studio

From Dev

Constructor cannot be applied to given types?

From Dev

Construtor cannot be applied to given types

From Dev

Android: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types

From Dev

Constructor in class cannot be applied to given types

From Dev

"Cannot be applied to given types" Error during PrintStream

From Dev

java constructor in class cannot be applied to given types

From Dev

Java Polymorphism "cannot be applied to given types"

From Dev

Issue with "Method in Class cannot be applied to given types"

From Dev

How to fix method cannot be applied to given types

From Dev

Constructor in class cannot be applied to given types

From Dev

Java, JOptionPane cannot be applied to given types

From Dev

Issue with "Method in Class cannot be applied to given types"

From Dev

SQLite addData cannot be applied to given types

From Dev

How to resolve the Android error "method setSupportActionBar in class AppCompatActivity cannot be applied to given types"?

From Dev

android: how to fix error constructor CardPagerAdapter in class CardsActivity.CardPagerAdapter cannot be applied to given types

From Dev

Constructor in class cannot be applied to given types. Hope for assistance

From Dev

Method cannot be applied to given types. Hope for assistance

From Dev

Error: Constructor Room in class Room cannot be applied to given types

From Dev

Java error: constructor in class cannot be applied to given types

From Dev

"constructor JSONParser in class JSONParser cannot be applied to given types" error occured

From Dev

Constructor in class cannot be applied to given types. Hope for assistance

From Dev

Method cannot be applied to given types. Hope for assistance

From Dev

Constructor cannot be applied to given types, and name should be declared

From Dev

java: constructor ranngeIpScanner in class cannot be applied to given types;

From Dev

error: method addOutcome in class OutcomesTable cannot be applied to given types

From Dev

Method edit in class data access cannot be applied to given types

From Dev

method in class cannot be applied to given types; required: HashMap<String,Integer>

From Dev

new BufferedReader in class Files cannot be applied to given types

Related Related

  1. 1

    constructor in class cannot be applied to given types android studio

  2. 2

    Constructor cannot be applied to given types?

  3. 3

    Construtor cannot be applied to given types

  4. 4

    Android: constructor ContactsAdapter in class ContactsAdapter cannot be applied to given types

  5. 5

    Constructor in class cannot be applied to given types

  6. 6

    "Cannot be applied to given types" Error during PrintStream

  7. 7

    java constructor in class cannot be applied to given types

  8. 8

    Java Polymorphism "cannot be applied to given types"

  9. 9

    Issue with "Method in Class cannot be applied to given types"

  10. 10

    How to fix method cannot be applied to given types

  11. 11

    Constructor in class cannot be applied to given types

  12. 12

    Java, JOptionPane cannot be applied to given types

  13. 13

    Issue with "Method in Class cannot be applied to given types"

  14. 14

    SQLite addData cannot be applied to given types

  15. 15

    How to resolve the Android error "method setSupportActionBar in class AppCompatActivity cannot be applied to given types"?

  16. 16

    android: how to fix error constructor CardPagerAdapter in class CardsActivity.CardPagerAdapter cannot be applied to given types

  17. 17

    Constructor in class cannot be applied to given types. Hope for assistance

  18. 18

    Method cannot be applied to given types. Hope for assistance

  19. 19

    Error: Constructor Room in class Room cannot be applied to given types

  20. 20

    Java error: constructor in class cannot be applied to given types

  21. 21

    "constructor JSONParser in class JSONParser cannot be applied to given types" error occured

  22. 22

    Constructor in class cannot be applied to given types. Hope for assistance

  23. 23

    Method cannot be applied to given types. Hope for assistance

  24. 24

    Constructor cannot be applied to given types, and name should be declared

  25. 25

    java: constructor ranngeIpScanner in class cannot be applied to given types;

  26. 26

    error: method addOutcome in class OutcomesTable cannot be applied to given types

  27. 27

    Method edit in class data access cannot be applied to given types

  28. 28

    method in class cannot be applied to given types; required: HashMap<String,Integer>

  29. 29

    new BufferedReader in class Files cannot be applied to given types

HotTag

Archive