main cannot be resolved or is not a field

前端 未结 9 1315
盖世英雄少女心
盖世英雄少女心 2020-12-07 20:44

This error occurs in setContentView line in this code snippet:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedI         


        
相关标签:
9条回答
  • 2020-12-07 21:21

    Make sure you don't have this in your imports:

    import android.R;
    

    but:

    import your.application.packagename.R;
    
    0 讨论(0)
  • 2020-12-07 21:23

    for me changing

    import org.opencv.R;
    

    to

    import com.example.opencvtryagain.R;
    

    solved it, where as my package name was com.example.opencvtryagain;

    0 讨论(0)
  • 2020-12-07 21:25

    Also, sometimes it still won't compile even after making that needed change; but if you go ahead and run the app, the errs will clear up.

    0 讨论(0)
  • 2020-12-07 21:30

    Also try: add

    import your.application.packagename.R;
    

    and run: eclipse->project->clean..

    The error should be gone.

    0 讨论(0)
  • 2020-12-07 21:34

    If you are using eclipse, then

    1. Delete all your import statements from the class where you are getting this error message.

    2. press CTRL + SHIFT + O --> OR select Source from menu and select Organize import. This will import all the necessary classes.

    Note: Since you have a local R.class file, it will import your local file instead of the android.R file.

    Hope this will help someone.

    0 讨论(0)
  • 2020-12-07 21:36

    If this is the problem you are encountering then check at the top of your code - you will see:

    import android.R
    

    Delete that line and and change it into

    import (com.xx.yy)
    

    Replace (com.xx.yy) with your actual package name for the class. This problem occurs mainly when you copy all the XML and Java code of another application and paste it into your new application.

    This is an example of how I replaced the android.R packages within one of my own apps (a puzzle game):

    package com.pir.PUZZLEGAME_NEW;
    
    import com.pir.PUZZLEGAME_NEW.*;
    import com.pir.puzzlegame_old.R;
    

    Good luck!

    0 讨论(0)
提交回复
热议问题