質問

When I declare double variables, and later try to assign values to them, it says unknown class . Secondly, any method calls from objects I have created say cannot resolve symbol .

My code:

private double xCurrentPos, yCurrentPos;
private ImageView test = (ImageView) findViewById(R.id.square);

xCurrentPos = test.getLeft(); yCurrentPos = test.getTop();

This code says that xCurrentPos and yCurrentPos are both unknown classes and that it cannot resolve getLeft or getTop.

Any help would be greatly appreciated. :)

役に立ちましたか?

解決

You can't do assignments of variables like that in the context of the class.

From what I can tell you're doing something like this which gives you the errors you described.

public class YourActivity extends Activity {

    private double xCurrentPos, yCurrentPos;
    private ImageView test = (ImageView) findViewById(R.id.square);
    xCurrentPos = test.getLeft();
    yCurrentPos = test.getTop();

    ...
}

You would have to do the assignment as they are being declared like this:

private int xCurrentPos= test.getLeft();
private int yCurrentPos = test.getTop();

Or you do the assignments within an actual method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    xCurrentPos = test.getLeft();
    yCurrentPos = test.getTop();
}

Second, doing this:

private ImageView test = (ImageView) findViewById(R.id.square);

will not work anyway as this will get initialized before onCreate so test will be null.

Even doing this in onCreate after you set the content view will give you values of 0 for both getLeft and getTop as the your views have not been drawn yet. For that you can refer to this. You basically need to do it in an event where you views have already been drawn.

他のヒント

The View class's getLeft() and getTop methods return an int, not a double. You don't need to use double type.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top