Quite new to Android dev so there is a good chance I made a dumb mistake. But for some reason I cant reference a ImageView I created in a second Activity using the findViewById method. When I went and checked the 'R' file I could see that an Id was not created for all 3 ImageViews I am trying to reference that is in the same activity.
What could the possible reason be for this? Could it be something to do with data I put in the Manifest file? My java class? Because my syntax for the containing activity is 100%....
I'm suspecting is has something to do with the new activity/manifest. How can all the ImageViews in the same Activity not be referenced...?
Clean the project and try again. To clean you project Project -> Clean select your project and clean. Some time it happens while developing the project. I've faced many times.
Related
I'm trying to create my first app with Android but I've encountered some problems.
Following a tutorial and making:
new android application -> blank activity
The default result should be:
With MainActivity.java and R.Java inside it.
But instead I've got this:
There are a lot of errors and the R.Java file is missing.
I can obtain a result similar to the tutorial one (with no errors), creating a new android application and unchecking create activity, but this time MainActivity.java under src is totally missing.
Maybe I could copy the MainActivity.java of the first project and put into the second one, but I prefer understand why it doesn't work.
Thanks for the help.
Best regards.
its import library problem
Right click on you application----->Android
under library view ,
check if you have this red error then remove the library and add it again
I wanted to create a Fragment in Android Studio, so I did the following steps:
Right click on package
New -> Android (Other) -> Fragment (List)
Checked both include fragment factory methods? and switch to grid view on large screens
The description said "Creates a new fragment containing a list that can optionally change to grid when on large screens."
Here's the funny part: As I was reading the code to understand how this switching is done, I could not find any code that switched XML files or anything. The only odd thing that I found is that the XML used for the Adapter was called R.layout.fragment_item, but there was no XML called like that in my layout folder.
There were 2 XML called R.layout.fragment_item_list and R.layout.fragment_item_grid. So I though "hey, maybe internally the framework auto decide if going for _grid or _list and that's why it doesn't mark R.layout.fragment_item it as non-existent"
When I was done reading the code, I deleted both R.layout.fragment_item_list and R.layout.fragment_item_gridfrom my layout folder and guess what? R.layout.fragment_item is NOT marked as non-existent, and there is no such file inside my Layout folder.
I already went Build > Clean and Build > Rebuild Project and it still marks it as existent. Am I missing something here? sounds like there is some basic stuff I haven't taken into account.
Just found what was going on. Inside values/refs.xml there was <item name="fragment_item" type="layout">#layout/fragment_item_grid</item> which should be the cause of it.
I had the same issue, I didn't understand your solution as I couldn't see refs.xml in the values folder.
For anybody out there, this is what worked for me:
In Android Studio go to File -> Invalidate Caches / Restart...
I have just added an activity to my project (from the options) in eclipse called Dlg as a result a Dlg.java file was created along with a activity_dlg.xml. Also added the activity in the AndroidManifest.xml however eclipse has placed a small cross next to the Dlg.java file and activity_dlg has been underlined in
setContentView(R.layout.activity_dlg);
and it states activity_dlg cannot be resolved or is not a field. Why is this happening all of a sudden. I Added activites a while ago and they were fine.
Any suggestions ?
The "cannot be resolved or is not a field" error on the R class indicates that the changes you made (addition of the layout xml) is not reflected in the generated file. You can clean the project using menu Project -> Clean... to force re-generation of the R class.
It probably means that for whatever reason the R.java file, which is automatically generated, isn't generated or incomplete.
This usually means you have an error in your xml file.
Assuming that activity_dlg.xml actually exists, you probably have an error in there.
Maybe its referencing to an image or string which doesn't exists?
Open it up and check it carefully, not always does the xml reader underline errors.
If you can't find anything you can always try to clean your projects or restart eclipse, but that probably won't help.
I think the newly added activity_dlg.xml is not associated with an id in R.java file.
Confirm this by going to R.java. Under this you have something like:
public static final class layout {
public static final int activity_dlg=0x7f030000;//This is some id to your layout
}
If this is not present then u can right click on the project directory from eclipse and refresh. or Project->Clean to regenerate the R.java file.
I'm attempting to create an Activity and unfortunately every time I want to grab one of my XML components it gives me a RunTimeException (NullPointer).
Anytime I use code such as:
TextView tv = (TextView) findViewById(R.id.myView); //I get the exception
The same happens for any components I attempt to find with that method. I can't quite figure out why. I know it isn't due to the Activity not being in the Manifest because it's the only Activity in the test app I made. (The one set up by default).
Oddly I can still use setContentView(R.id.myView). It just doesn't seem to want to find anything when using the findViewById method.
Info that might be of use:
I am currently using NetBeans as my IDE.
I have done multiple 'clean and builds' as was suggested in another question. Android -findViewById question
Has anyone run into this issue before? If so, what was the solution?
If need be, I can provide sample code of when this is happening.
Don't pass in a view ID to setContentView, pass in a layout resource ID:
setContentView(R.layout.layout_name);
If you still have problems, post your layout file.
It is very sure that you R.java is not properly generated.
Delete R.Java in netbeans IDE and Re-build the project.
Hope it resolves your query.
How do I fix this? I already tried removing the R.java and cleaning the project via eclipse, but it doesn't help.
FYI I am trying to get PhotoStream from here: http://code.google.com/p/apps-for-android/, but so far it has been very difficult to get things work.
Okay..... 5 mins later google tells me the correct answer...
http://www.fairtec.at/en/it-blog-mainmenu-16/168-the-type-r-is-already-defined
I just didnt search hard enough.
"The type R is already defined"
That's the message you get in Eclipse if you try to build the Funambol Android Sync Client.
Reason is that you have checked two Builders that try to generate the same class.
You just have to uncheck the Java-Builder from Project->Properties->Builders.
Then the application even works fine in the Emulator.
Delete the R.java from the src folder and rebuild the project. This file will be automatically rebuit during this process.
http://www.fairtec.at/en/it-blog-mainmenu-16/168-the-type-r-is-already-defined
click right to project click properties
Project->Properties->Builders.
unckeck java Builder
delete file R.java
You may want to change your package names. It looks like you are using a 'PhotoStream'.jar which has it's R.class defined at the same package structure as you.
Here is a link to the R.java from the project on Google Code. Notice you are using the same package:
http://code.google.com/p/apps-for-android/source/browse/trunk/Photostream/src/com/google/android/photostream/R.java?r=83
I had the same issue when I imported a project from work. Turning off the Java builder as suggested in the article you found fixed my problem, but when I made code updates they were not reflected in the running app. In my case there was an R.java in my source which I deleted and that fixed my problem.
In my case,
as i m not using any IDE for programming but using command line Android..
i had two xml files, one in layout and other in layout-land. i was using same id "XXX" for both but while declaring i made small mistake
android:id="#+id/XXX" (in layout xml)
android:id="#+id/XXX " (in layout-land xml)
please observe extra space in second id declaration, so while creating R.java they were different and in R.java i had
public static final int XXX=0x7f040046;
public static final int XXX =0x7f040045;
which are same, so please be aware of extra spaces. Thank you