문제

I'm getting a strange error eclipse says "R cannot be resolved to a variable"

I don't know wath to do.

Do i have to poste my Manifest file? My xml file?

I have tryed : clean project update AVD update SDK

this is my code:

//package

//imports

 public class MainActivity extends Activity implements OnClickListener{

public Handler timer = new Handler();

public int counter = 0;

 //vars

TextView mTextView;
ImageButton finishhimbutton;
ImageButton resetbutton;

 //images

public ImageView jb1;
public ImageView jb2;
public ImageView jb3;
public ImageView jb4;
public ImageView jb5;
public ImageView jb6;
    TextView txtCount;




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

//define

        txtCount = (TextView)findViewById(R.id.timer);              
        txtCount.setText(String.valueOf(counter));
        jb1 = (ImageView) findViewById(R.id.JB1);
        jb2 = (ImageView) findViewById(R.id.JB2);
        jb3 = (ImageView) findViewById(R.id.JB3);
        jb4 = (ImageView) findViewById(R.id.JB4);
        jb5 = (ImageView) findViewById(R.id.JB5);
        jb6 = (ImageView) findViewById(R.id.JB6);

 //click button
        finishhimbutton = (ImageButton) findViewById(R.id.finishhim);
        resetbutton = (ImageButton) findViewById(R.id.reserbutton);
        timer = new Handler();


        final Runnable hMyTimeTask = new Runnable() {
               public void run() {

                    if (counter < 49) {

                    counter = 0;
                    txtCount.setText(String.valueOf(counter));
                    jb1.setVisibility(ImageView.VISIBLE);
                    jb2.setVisibility(ImageView.INVISIBLE);
                    jb3.setVisibility(ImageView.INVISIBLE);
                    jb4.setVisibility(ImageView.INVISIBLE);
                    jb5.setVisibility(ImageView.INVISIBLE);
                    jb6.setVisibility(ImageView.INVISIBLE);
                    }

                   else if (counter > 50) {     
                    Toast.makeText(MainActivity.this, "WINNER", Toast.LENGTH_SHORT).show();

                }
               }

            }; 



        finishhimbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                    if (counter == 1) {
                    timer.removeCallbacks(hMyTimeTask);
                    timer.postDelayed(hMyTimeTask, 4000);
                 }

           switch(counter){

     //case's

           case 1:
                    counter++;
                    txtCount.setText(String.valueOf(counter));
                    jb1.setVisibility(ImageView.VISIBLE);
                    jb2.setVisibility(ImageView.INVISIBLE);
                    jb3.setVisibility(ImageView.INVISIBLE);
                    jb4.setVisibility(ImageView.INVISIBLE);
                    jb5.setVisibility(ImageView.INVISIBLE);
                    jb6.setVisibility(ImageView.INVISIBLE);
                    break;

                        //................and more case's
                                //there are more case's


                }

                }


}

);

    //reset button

resetbutton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
            counter = 0;
            txtCount.setText(String.valueOf(counter));
            jb1.setVisibility(ImageView.VISIBLE);
            jb2.setVisibility(ImageView.INVISIBLE);
            jb3.setVisibility(ImageView.INVISIBLE);
            jb4.setVisibility(ImageView.INVISIBLE);
            jb5.setVisibility(ImageView.INVISIBLE);
            jb6.setVisibility(ImageView.INVISIBLE);
              Intent screen = new Intent(MainActivity.this, startscreen.class);
              MainActivity.this.startActivity(screen);
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {

}
}
도움이 되었습니까?

해결책

check if you import android.R and check your xmls for errors and also rebuild all project

다른 팁

  1. close Eclipse and open your Workspace directory.
  2. look for a folder named ".metadata" and delete that folder.

the Error should go away :)

I suggest you search Stack Overflow before opening a new report.

This will help :

"R cannot be resolved to a variable"?

1.Remove import android.R; and your main project import.

2.Then go to the error variable and import your project reference.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top