0

I have a problem when I want to start a new activity wchich contains a reference to object extending SQLiteOpenHelper.

This is how I sart my activity:

// Listener for button
class LoginButtonListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(v.getContext(), MasterUserActivity.class);
        v.getContext().startActivity(intent);

    }
}

My new activity:

// I will be implementing ListView in here
public class MasterUserActivity extends ListActivity  {

    private DBHandler dbHandler = new DBHandler(this);

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

        Toast toast = Toast.makeText(MasterUserActivity.this, "It works :)", Toast.LENGTH_LONG);
        toast.show();

    }

}

And my DBHandler class:

public class DBHandler extends SQLiteOpenHelper {

    public DBHandler(Context contex) {
        super(contex, "bankapplication.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String query = "CREATE TABLE users ( login TEXT, password TEXT, name TEXT, "
                + "phone TEXT, country TEXT, balance TEXT)";
        database.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS users";
        database.execSQL(query);
        onCreate(database);
    }
}

It works fine when I comment out private DBHandler dbHandler = new DBHandler(this);

What is wrong in here ?

Krzysztof Majewski
  • 2,494
  • 4
  • 27
  • 51

1 Answers1

2

This line is causing the error :

private DBHandler dbHandler = new DBHandler(this);

You can not call new while declaring variable, instead initialize inside onCreate method of your activity.

Rohit Rai
  • 286
  • 4
  • 13