I created a blank template and chose API 15 as minimum.
1- The activity_main.xml preview asked me to choose another theme. Why?
2- The MainActivity.java class is extending AppCompatActivity instead of Activity. I tried to delete it and typed Activity and imported the corresponding jar but then the IDE doesn't reconize R in R.layout.activity_main. The IDE says it can't resolve AppCompatActivityin import section of the code. Where is the problem?
package com.company.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks
P.S.
It seems the gradle was somehow unsuccessful in building the project. I'd used -Xmx256m in its settings.(What was the problem with this curb?) I removed the curb and it built the project. The weird thing is Android Studio recognized AppCompat stuff after building the project! (Shouldn't the IDE know all the classes used in code before compile?). Anyway, my problem is solved now but I don't know the reason
If you are still having that error then you can try going step by step via this blog : Hello World App on android studio
It would be easier if you uploaded your code
If the error is Cannot resolve R you just need to press ctrl+shift+o to organize imports and restart android studio
Related
Earlier I was asking about how to come over from Windows phone development. I didn't just sit and wait for replies I got started.
I installed Android Studio. I learned that it didn't have the 4.4 API by default (the version of Android my new phone will be running) so I located the SDK manager and installed the missing SDK.
The preview pane for the layout editor could not render...I learnt I needed to go to the module settings and change the target there, then to the preview pane drop-down and change the targeted Android version there.
I added a simple button and then realised I have an error with the java ...that I have yet to touch!
public class MainDisplay extends ActionBarActivity {
ActionBarActivity is deprecated.
...but that's the default template!
getMenuInflater().inflate(R.menu.menu_main, menu);
cannot resolve symbol R.
...again, it's default "blank activity" template.
I checked for updates and it is the latest version of Android Studio: 1.2.1.1. My JDK is the latest version: jdk1.8.0_45 (64bit).
Any ideas why my Android Studio (freshly installed today) is generating broken templates or any other ideas about how to fix?
EDIT
I uninstalled Android Studio, the SDK and deleted all folders created by it
then I reinstalled it all. I installed version 1.7 of Java JDK.
I got the same errors on a new project.
This is the source code it generates:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
As you can see, it is extending the deprecated class, and adding those R's it can't resolve .....and this is before I have done anything.
But for tonight, I have run out of time. I had 7 hours free to start porting my app to Android and it's all gone setting up the dev environment. I wouldn't mind but it's not working and I have no idea why.
According to this video you should only make two changes
After I did this 2 changes all worked fine
For more information about this you have this [1] and this blog
UPDATE 1
After this change, clean your project and Sync Project with Gradle File.
UPDATE 2
If after that you have problem resolving symbol R errors after an SDK update in Android Studio you can follow the steps showing in this blog
First one. The ActionBarActivity is deprecated since API 21 (or 22 maybe) and now you should simply extend from Activity and use AppCompatDelegate. It's a really new feature so I suppose they haven't fixed it yet. I haven't tried using it yet so can't help with that. Even if the ActionBarActivity is deprecated it will work fine so it's not a problem for you.
Second. About the R being not found. The problem is inside your Gradle file (not the app one but the second one). In it you will need to fix a line under the dependencies tag (classpath) and change it to newer version. I can't seem to find the right version now but I have already solved this problem today for my friend so it will definitely work
It is also a bug in a current Android Studio version and after you fix it it will work fine.
When I try to create an android project in eclipse the code has errors.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I'm getting "R cannot be resolved to a variable"
I think it can't detect the resources?
My SRC code is:
package com.android.h4;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
In all of R eclipse detects an error. I tried to create a new project with appcompat_v7 and I deleted the libs and res folders as well as the AndroidManifest.xml. Then i imported these folder from a working project with the same name(someone emailed them to me) and at emptied the src but the error remains.
Any idea what I should do?
There are some possibilities that may evoke that error.
The easiest way (seems stupid but often helps)
Go to "Project" in the Menu of Eclipse and click on "Clean"
After Cleaning, the workspace is rebuild and hopefully the R-file
is recognized than. (Sometimes even a restart of Eclipse helps)
Check all your IDs. As already said in comments, it's possible that you have
misspelled the ID (you define your IDs in the layout, there you have to check it)
Last but not least: DON'T TRY TO CHANGE THE R-FILE YOURSELF! :)
I hope I could help you. If not, just specify your problem (i.e send a copy of your layout xml)
You should clean your project and build again. For doing that you should go to
tab project -> clean -> select the project
And for more details you can also dig out on these links
R cannot be resolved - Android error
“R cannot be resolved to a variable”? duplicate
I am new with JavaScript and Developement on Android. I installed Eclipse but I can't even run the "Hello World" program, included when you created a new project !
Please help me, I spent the two last days installing this IDE (even tried Android Studio),
but there is always the same error :
Could not find Chwet.apk!
And there seems to be an error with the "R", like in
setContentView(R.layout.activity_main)
Here is my code :
package com.example.chwet;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Look at following tutorial to get start with Android application development.
Hello world example.
Instead of importing android R class , import R class with your package name.
Press Ctrl+shift+o to add missing packages. It will automatically remove your errors.
I am getting the error "R cannot be resolved to a variable" that many people seems to be getting. However, none of the answers that I have seen has worked for me. I've gone to the extreme basic and created a helloworld project, with all default settings. The "R cannot be resolved to a variable" error is created instantly, and I cannot see what is wrong... no xml errors, all lowercase in /res/ folder, no difference in AndroidManifest.xml.
For reference, the code on MainActivity.java is:
package com.test.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Is there something wrong with eclipse setting?
I'm using Windows 7 64-bit
Whenever I had a problem with R not been generated, or even disappeared, first clean and rebuild the project, if this is not worked then it was due to some problem in the XML layout file that prevented the application from being built. Check for the /res directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.
Ok, I know there are several questions like this, but I've been through all of them and nothing seems to work! I have been using eclipse for almost a year now. I installed new softwares from the Help menu, and since then nothing is working! I re-installed eclipse and now I'm getting this "R cannot be resolved to a variable", even when I create a brand new project. Here's the code:
package test.pro;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I am getting an error in the setContentView and getMenuInflater lines. I looked in the 'gen' folder, there's nothing there! I also checked the Java Build Path, it's fine. The ADT is updated. Can someone help me out here!!? I really need it!
Usually this error is because of some error in layout, strings, dimensions or any other xml file. Look for such xml error, resolve it and you should be good to go.
Also do perform a clean of your project. This error is very common with me too.