how to make this static

331 Views Asked by At
public static Bitmap bmp;
bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.nehe);

the error for above code "Syntax error on token ";", , expected" I already put a semi colon after bmp but still the error occurs

here is the full code:

public class CustomActivity extends AndARActivity {

ARToolkit artoolkit;
CustomObject someObject;
CustomObject1 someObject1;


public static Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.nehe);  


@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    CustomRenderer renderer = new CustomRenderer();     

    super.setNonARRenderer(renderer);

    try {
        //register a object for each marker type
        artoolkit = super.getArtoolkit();

        someObject = new CustomObject
            ("test", "androidpirate.patt", 80.0, new double[]{0,0});
        artoolkit.registerARObject(someObject);

        someObject = new CustomObject
        ("test", "andson.patt", 80.0, new double[]{0,0});
        artoolkit.registerARObject(someObject);

        someObject1 = new CustomObject1("test","andrev.patt", 80.0, new double[]{0,0});
        artoolkit.registerARObject(someObject1);

        someObject1 = new CustomObject1("test","androidpat.patt", 80.0, new double[]{0,0});
        artoolkit.registerARObject(someObject1);

    } catch (AndARException ex){
        //handle the exception, that means: show the user what happened
        System.out.println("");
    }       
    startPreview();
}


@Override
public void uncaughtException(Thread thread, Throwable ex) {
    Log.e("AndAR EXCEPTION", ex.getMessage());
    finish();
}   
}
4

There are 4 best solutions below

0
On

You are calling "this" in a static context, "this" refers to the class instance object, therefore using it in a static manner is not a valid Java syntax. Additionally, I don't think "this.getResources()" would return anything but null until the time onCreate(Bundle) method is called.

You should do something like:

public static Bitmap bmp;

public void onCreate(Bundle savedInstanceState) {
    if(bmp!=null) {
        BitmapFactory.decodeResource(this.getResources(), R.drawable.nehe);
    }
...other code here
2
On
public static Bitmap bmp =
    BitmapFactory.decodeResource(this.getResources(), R.drawable.nehe);

or

public static Bitmap bmp;

static {
    bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.nehe);
}
3
On

If you are writing those statements in a method, then public is not a valid access modifier for local variables.

If you are writing those statements outside a method within a class, then those statements should be combined as follows:

public Bitmap bmp = 
    BitmapFactory.decodeResource(this.getResources(), R.drawable.nehe);

This is because, only initializers are allowed outside methods or blocks, but not assignment statements like your second statement.

Since you seem to have a dependency on the return value of getResources() member method, bmp cannot be static. Also, it is better to initialize bmp only when you are sure that the return value of getResources() method is meaningful. You should show more code from your class to get better help w.r.t this.

0
On

public declarations can be used only for class variables not for function variables