为什么在尝试引用类时会出现“无法解析为类型”错误 - android

时间:2012-02-21 21:27:21

标签: android class

这是我的代码,我试图在屏幕上滑动多个图像。特定错误显示在“TimerTask thisTimerTask = new ThisClass();”中的“ThisClass”线。我很感激任何可用的反馈

package your.package2.namespace2;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.widget.Button;
import android.widget.ImageView;
@SuppressWarnings("unused")

public class ChangeuppicksActivity extends Activity {

Display thisDisplay;
ImageView img, img4, img5, img7;
Timer timer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    thisDisplay = getWindowManager().getDefaultDisplay();
    TimerTask thisTimerTask = new ThisClass();
    timer = new Timer();
    timer.scheduleAtFixedRate(thisTimerTask, 1000, 16);

    img = (ImageView) findViewById(R.id.pic1);
    img4 = (ImageView) findViewById(R.id.pic4);
    img5 = (ImageView) findViewById(R.id.pic5);
    img7 = (ImageView) findViewById(R.id.pic7);
}

public void doSomething() {
    int slideToPOS_X = (img.getLeft() - 7); 
    if (slideToPOS_X <= -img.getWidth()) {  
    int slideToPOS_X2 = (img4.getLeft() - 5);  
    if (slideToPOS_X2 <= -img4.getWidth()) {  
        slideToPOS_X2 = thisDisplay.getWidth();
    }

    img.layout(slideToPOS_X, 0, (slideToPOS_X + img.getWidth()), (0 + img.getHeight()));;

    img4.layout(slideToPOS_X2, 0, (slideToPOS_X2 + img4.getWidth()), (0 + img4.getHeight()));
}


class ThisClass extends TimerTask {
    @Override
    public void run() {
        ChangeuppicksActivity.this.runOnUiThread(new Runnable() {

            public void run() {
                doSomething();
            }
        });
    }
}
}
}

3 个答案:

答案 0 :(得分:0)

不确定这是否是您问题的根本原因,但看起来您的课程底部有一个太多的近括号。尝试删除类底部的'}'之一,看看是否会让你的编译器更快乐。

您在doSomething()方法中也缺少一个近距离。我可能已经破坏了你的逻辑,但尝试用这个替换底部:

public void doSomething() {
int slideToPOS_X = (img.getLeft() - 7); 
if (slideToPOS_X <= -img.getWidth()) {  
    int slideToPOS_X2 = (img4.getLeft() - 5); 
}
if (slideToPOS_X2 <= -img4.getWidth()) {  
    slideToPOS_X2 = thisDisplay.getWidth();
}

img.layout(slideToPOS_X, 0, (slideToPOS_X + img.getWidth()), (0 + img.getHeight()));;

img4.layout(slideToPOS_X2, 0, (slideToPOS_X2 + img4.getWidth()), (0 + img4.getHeight()));
}


class ThisClass extends TimerTask {
@Override
    public void run() {
        ChangeuppicksActivity.this.runOnUiThread(new Runnable() {

            public void run() {
                doSomething();
            }
        });
    }
}
}

答案 1 :(得分:0)

最后的一个小括号是多余的,我认为应该有一个额外的括号关闭doSomething方法。就目前而言,ThisClass是在doSomething方法中定义的 - doSomething的第二行有一个开括号,直到类的末尾才关闭,由于代码在开始括号后没有缩进,所以很难看到。 / p>

答案 2 :(得分:0)

ThisClass定义移到doSomething()方法之外。