I'm trying to create the simplest hello world program but as soon as I create a new project, I get the following error: "main cannot be resolved or is not a field".
package com.example.helloworld;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("Hello World!");
setContentView(tv);
}
}
I've tried adding import.android.R;, cleaning and restarting. I've also deleted import.android.R and included import.com.example.helloworld.R; (my package name). Keep getting the same error.
You've imported the Android's "R" class. You need to import your own.
Replace
import android.R;
With:
import com.example.helloworld.R;
If your R file doesn't resolve, then you have a problem in one of the files in your res folder. Check that all file names are lower case, have no illegal chars and that any XML files don't have syntax problems.
check your error logs or lint messages. I was facing this problem a couple of times.
Last time it was an improperly generated menu.xml file. Removing helped
Related
I have never used Android Studio before and have always been coding using NetBeans.
I need to create an app for Android, so I decided to use NetBeans. I created new project and it is 'empty' for now, however I have an error:
package Fridge.Freezer;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
The error is in setContentView(R.layout.main); - cannot find package. Can anyone tell what I am doing wrong? Clean and build does not work.
erase your package name at the top and try to add it to getting the suggestion package import by shortcut Control + Enter beside "R"
Try checking following component in your project directory
R.java file
folder named layout
layout xml file named main.xmlmain
PS Since your are starting a fresh project, I would suggest you to start at Android Studio.
I tried cleaning the project and rebuilding it again. It did not work. The solution was to restart IDE.
So when i create a new android application project with ANDROID 6.0 library i instantly get the a lot of errors in the MainActivity code.
Starting with The import android.support.v7.app cannot be resolved
To The method onCreate(Bundle) of type MainActivity must override or implement a supertype method
When i add the appcompat.v7 library obviously Some of the errors related are gone but other are coming instead. Like:
The type android.support.v4.widget.DrawerLayout$DrawerListener cannot be resolved. It is indirectly referenced from required .class files on the package line.
And still The method onCreateOptionsMenu(Menu) of type MainActivity must override or implement a supertype method.
Ok. So add the v4 jar too. Ok. I added it too to the project properties and no errors!
Wait,but when i run the app now I get a ClassNotFoundException.
So what the hell android 6.0???
Thanks for the help.
EDIT
MainActivity.java:
package com.minyan.get.dl;
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);
}
}
The main reason is that Android Studio is the main IDE now.
Eclipse is no longer supported.
I suggest switching to Android Studio.
This question has been up a couple of times but still I haven't found an answer that helped me.
1:st one:
I cannot find any of my R.Layout.activity_article_detail and other layout xml files through reference. It stopped working after did a "clean project" in Eclipse and tried to build it up again.
I've tried removing and adding and removing the .R import but right now it wont find any of them. My layout files doesn't include a Capital letter, are spelled correcly.
code:
package martin.larsson.kopingsrssreader;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import martin.larsson.kopingsrssreader.R;
public class ArticleDetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_detail);
Second problem is it cant find my res.menu either. I got 2 files in it. detailmenu.xml and refreshmenu.xml. In both i cant find any of the id tags
<item android:id="#+id/actionbar_markunread"
<item android:id="#+id/actionbar_saveoffline"
<item android:id="#+id/actionbar_refresh"
Does anyone got a clue?
SOLUTION: Uncheck "build automatically" and do a "clean project", and a Eclipse restart.
Clean your project or restart the eclipse.
Look to the imports, the R imported maybe is not correct.
If anything is wrong in your layouts, new updates aren't added to the R file. Look at the errors panel, and possibly to a clean rebuild to see what errors are popping up.
if u have an error in any of the resource files R will show up red unless all Errors are fixed and the project is built
I'm trying to set up a splash screen for PhoneGap in Intellij Community edition 11.12.
The problem is that Intellij gives me the error 'cannot resolve symbol splash' - for some reason it's not picking up R.drawable.splash - I've rebuilt the project and deleted the generated files, but that doesn't help.
I have the following folder structure (looks correct from http://developer.android.com/guide/topics/resources/providing-resources.html)
-res
--drawable
----splash.png
--drawable-hdpi
----splash.png
--drawable-mdpi
----splash.png
--drawable-ldpi
----splash.png
--drawable-xhdpi
----splash.png
And I'm trying to set the splash screen with:
package com.illbeintouch.mobile;
import android.R;
import android.os.Bundle;
import org.apache.cordova.DroidGap;
public class MyActivity extends DroidGap
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html", 5000);
}
}
I don't know if it makes a difference, but I'm using 1.7 Java SDK, and android 2.3.3 as the target platform.
If you can help out I'd be super grateful - I've spent all morning trying to sort this thing out!!!
You try to find splash in default R class:
import android.R;
Remove this import and import appropriate autogenerated R.
package com.ustr.eMIRnew;
import java.util.ArrayList;
import java.util.HashMap;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class eMIRnew extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}`
This is my code. But it is showing the error R cannot be resolved.
I have tried many methods like clean-and-build, closed-and-open the project, changed import android.R to import your.package.name.R etc. But nothing helped. Can anybody help me, please?
You are Importing android.R package, which is default one provided by Android.
If you want to access your own Layout, assets, String. . . then
Remove the import android.R statement.
No need to import your Package.R, By default, R file is generated during built.
You are using the android R file you have to use you package R file if you want to access the main layout for you app. import you_package.R
Remove the import statement:
import android.R;
In the onCreate method where you set the content view to R.layout.main, your project should be using this file:
com.ustr.eMIRnew.R
This file is generated when you build your project. Are you using Eclipse for building? Then this should not be a problem.