2

I am building a database class in android which gathers String non-static data from the getIntent().getStringExtra(name) function. The value of this i am putting in a simple string variable (Non-Static).

When im trying to use it inside a static string, i get an error - "Cannot make static reference to a non static field".

How can i solve this? Here is my code:

package ent.com;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;

public class SetSql extends Activity{

String saveExtra = getIntent().getStringExtra("save");

public static final String KEY_ROWID = "_id";
public static final String KEY_SAVE = "save_name";
public static final String KEY_STADUIM = "stadium_size";
public static final String KEY_FINANCE = "total_money";
public static final String KEY_PLAYERS = "players";

private static String DATABASE_NAME = "save" + saveExtra;
private static final String DATABASE_TABLE = "save_data";
private static final int DATABASE_VERSION = 1;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

}

}

Thanks for any help :)

ngesh
  • 13,398
  • 4
  • 44
  • 60
rel-s
  • 6,108
  • 11
  • 38
  • 50

5 Answers5

3

Try this

String saveExtra = null; 
private static String DATABASE_NAME = "save" ;

In OnCreate

String saveExtra = getIntent().getStringExtra("save");
DATABASE_NAME += saveExtra;
user936414
  • 7,574
  • 3
  • 30
  • 29
  • Can you please explain the reason for declaring saveExtra variable inside and outside the onCreate?? Couldn't we just use the same instance of saveExtra(that has already been declared outside) inside the onCreate method. – AndoAiron Apr 12 '12 at 10:40
  • 1
    This is because static variables of a class will be initialized before any other variables in the class. So initializing a static variable with some other non-static variable is not possible as the non-static variable will be initialized only after the static variable is initialized. Refer http://www.javatutorialhub.com/wiki/All_about_%22static%22 – user936414 Apr 12 '12 at 11:06
  • There might be a problem if your SetSql activity is instantiated more than once. – nicopico Apr 12 '12 at 11:21
1

Your static variable DATABASE_NAME is initialized as soon as your SetSql class is loaded, whereas the instance variable saveExtra is initialized only when an instance is created.

You could set DATABASE_NAME value in the SetSql class constructor but beware that it would impact all instances of SetSql, not just the one who received the intent.

IMHO, the simplest option would be to set DATABASE_NAME as an instance variable like saveExtra.

nicopico
  • 3,606
  • 1
  • 28
  • 30
0

Try to make a static "getDatabaseName" methode to access your string.

0

Remove this line String saveExtra = getIntent().getStringExtra("save"); from Activity level, and write it into your onCreate().

Just declare your variable saveExtra at Activity level.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • Do you mean instead of declaring it outside the Activity oncreate method to declare saveExtra inside it? its not actually working :( – rel-s Apr 12 '12 at 10:04
  • Declare - No Problem,...line `getIntent().getStringExtra("save");` write in onCreate() – MKJParekh Apr 12 '12 at 10:09
0
private static String saveExtra =null;

in

onCreate

use this

saveExtra = getIntent().getStringExtra("save");
Its not blank
  • 3,055
  • 22
  • 37