Java Android Eclipse: R Classes disappear [duplicate] - java

http://developer.android.com/training/basics/firstapp/building-ui.html
I have been following this tutorial, but I have two errors, both "R cannot be resolved to a variable". I have made android apps (easy ones) in the past, and I remember this problem being fixed by checking whether or not I have an import R statement (which I don't) and whether or not the project has been cleaned before being built again (I cleaned it and I still get the error). I am at a loss as to what to do. Thanks!
*I also want to mention I did see the thread of the same title with 170 hits, and the solution of "delete the import R statement" does not apply to my problem (I don't think)
Thanks again
All code is straight from the link above, but here it is for convenience
activity_my_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
MyFirstActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MyFirstActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_first);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_my_first, menu);
return true;
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="menu_settings">Menu Settings</string>
</resources>
edit: When I clean the project, I get this message in the Console:
[2012-06-29 11:12:38 - MyFirstApp] W/ResourceType( 6140): Bad XML
block: header size 91 or total size 0 is larger than data size 0
[2012-06-29 11:12:38 - MyFirstApp]
C:\Users\zhong\workspace\eclipse\MyFirstApp\res\menu\activity_my_first.xml:2:
error: Error: No resource found that matches the given name (at
'title' with value '#string/menu_settings').
edit: added a line to layout xml file <string name="menu_settings">Menu Settings</string>
Fixed cleaning project errors, but I still can't run the project from the two R errors.

I'm working my way through the same example, and had the same (or very similar) problem.
Finally I noticed that there was a tiny little red x on the manifest.xml.
Sure enough, it was complaining about this:
android:label="#string/title_activity_hello_world" >
So I added:
<string name="title_activity_hello_world">Hello World</string>
to strings.xml and now it works.

I had the same issue while following the tutorial.
Cleaning the project or cleaning the imports did not solve the problem.
How the problem was (simply) solved : I quit Eclipse and relaunched it.

2 possible things
The package name specified in the Android Manifest isn't the same as the in the Java files.
Or, your IDE hasn't generated the R.java file in the gen/ folder. Try building again (despite the error) and it will usually clear it up.
Basically the R.java file is generated for you and in the same package so you can refer to it as simply R.

I had android:text="#string/button_send"
Which gave the error: No resource found that matches the given name (at 'text' with value '#string/button_send').
I tried to solve the error using the tuturial. But the tutorial fails to tell me to write all the lines required one by one. So I miss to write "<string name="button_send">Send</string>" in the strings.xml.
After adding the previous line the error disappear! Which I believe is something similar to the original problem in this thread.
So if I had copied the whole lines from the given tutorial it may had not happened, but by typing our-self is how I think is the best way of learning.

I found the solution:
go to your project->res->menu than open the XML file there and delete this line:
android:title="#string/menu_settings"
I deleted it and it start to work,after 3 hours of the same warning in the problem section.
It even make sense when you read it, "at title with value #string/menu_settings"

Pleas confirm there is no error in
1- Project folder
2- Problem View of eclipse
3- Console
4- error log as in image

You have to make sure to
Clean your project Project-->Clean
The /res directory doesn't contain errors (some files will show up
with a red icon on the file explorer)
Your imports doesn't contain this line import android.R;
Check that in AndroidManifest.xml, the attribute package has the
correct value. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="your.correct.package.name"

This fixed it for me:
https://stackoverflow.com/a/3259974/1538785
Seems like eclipse likes to add an import line to java files for no reason. Go onto your java file, hit ctrl+shift+o and all the import statements will appear. Delete the line that says import andriod.R

Right click on the Project, select "Android Tools" > "Add Support Library" > "Android Support Library, revision 11" > "Install"

In my case, the problem was that I was on 64-bit Linux, and the required 32-bit libraries were not installed, meaning that the build tools could not generate R.java.
Running this command, then cleaning the project, fixed the problem:
sudo apt-get install ia32-libs

Make sure the executables (aapt etc.) in sdk/build-tools/android-xyz/* are executable if you are running the default download on your Linux box...

I had the same error trying to do the first tutorial. To get the app to run I edited MainActivity.java so it looked like this
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Related

Android Studio causes Binding problem in layout resource file after updating to latest version

My Android Project was perfectly building and running. After updating Android Studio to v3.5.2 and also to latest gradle build version the build is failing without having done any changes to the code.
The problem seems to be related to Binding. The following error pops up:
> Task :app:compileDebugJavaWithJavac
C:\App\MyApp\app\build\generated\data_binding_base_class_source_out\debug\dataBindingGenBaseClassesDebug\out\com\project\databinding\LayoutBinding.java:13: error: cannot find symbol
import com.project.ViewModel;
^
symbol: class ViewModel
location: package com.hometech
C:\App\MyApp\app\build\generated\data_binding_base_class_source_out\debug\dataBindingGenBaseClassesDebug\out\com\project\databinding\LayoutBinding.java:19: error: package ViewModel does not exist
protected ViewModel.My_ViewModel mViewModel;
^
C:\App\MyApp\app\build\generated\data_binding_base_class_source_out\debug\dataBindingGenBaseClassesDebug\out\com\project\databinding\LayoutBinding.java:25: error: package ViewModel does not exist
public abstract void setViewModel(#Nullable ViewModel.My_ViewModel ViewModel);
^
C:\App\MyApp\app\build\generated\data_binding_base_class_source_out\debug\dataBindingGenBaseClassesDebug\out\com\project\databinding\LayoutBinding.java:28: error: package ViewModel does not exist
public ViewModel.My_ViewModel getViewModel() {
The related XML layout resource file that causes the problem looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="ViewModel"
type="com.project.ViewModel.My_ViewModel" />
</data>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</layout>
The entire build runs successful if the layout resource file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</layout>
But as soon as I add the <variable ... /> part the build fails with the mentioned error messages.
(The 'ViewModel' package and the related ViewModel of course exists!)
I am super desperate about this. All the Android Binding stuff has often problems especially after updating but this time I simply do not find and fix and I tried a lot!
I appreciate any help, thanks!
I found the solution myself.
One of the error messages sais "package ViewModel does not exist".
Well, the package definitly exists and its also possible to access it from within classes.
As mentioned in my post I was able to build exactly the same project before updating Android Studio and gradle build to version 3.5.2.
I changed the naming of the package "ViewModel" to "viewmodel" and the build is finally successful again.
Also the expression "Viewmodel" was working, but only twice, the third time I tried to build the project like that failed again. A really strange behaviour of Android Studio!
So obviously something was changed in the newest Android Studio version that prevents using such a wording!
By the way, all other packages are also in camelcase like it was for "ViewModel" and they are not causing any trouble.

How do I begin to use this Java library?

Teaching myself Java, still very new. Last night I learned how to import this library into my project in Android Studio. But now I'm confused as to how to actually start using it.
I know that Java works with classes and that supposedly a library is just a collection of classes (and probably others things too...) that you can begin using once you import it. But the author of this library told me to just use this:
BackgroundMail bm = new BackgroundMail(context);
bm.setGmailUserName("sendername#gmail.com");
bm.setGmailPassword("sender_email_password");
bm.setMailTo("receiver#gmail.com");
bm.setFormSubject("Subject");
bm.setFormBody("Body");
bm.send();
But when I try to put that into another one of my classes, I get red errors all over the place. So then I tried to create a Java class within my app's files and I still got red errors. Could someone kindly help me, a total beginner, get started here? I'd like to use this library to send an email in the background of my app.
To import the library:
I followed this answer: https://stackoverflow.com/a/35369267/5241266 and used Approach 2.
MainActivity.Java: this is where I put the import code.
package moviehelper.moviesfree;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
For now, I have not added the block of code that the author told me to use (see above).
The Gradle Console error:
I get the following error when doing Build -> Make Project:
error: package com.creativityapps.gmailbackgroundlibrary does not exist
import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;
My project tree: I think there might be some problem with this structure. It looks like the library was added as its own project? Although I'm not sure.
If you don't want to use the JitPack approach as documented in the instructions, then look at the settings.gradle file in the Github example.
It includes both modules (app and the library).
Then, once that is setup, you can compile project(:libraryName) in the app/build.gradle file's dependencies section. Again, see the Github sample for the syntax.
With those two steps (plus one to download the library), it should be importable within the app code.
Assuming you followed along with the other steps on the readme, at the top of your code you need to tell the source file to import the classes:
import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;
public class MainActivity extends AppCompatActivity {
[your code]
}
I would also recommend looking through the sample code included in the github project
You are doing some wrong steps friend. You can follow this
Find your gradle file with name buid.gradle (Project: [ YOUR_PROJECT_NAME ] )
Then find and add this line
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
You are done 50% now. Now press SHIFT+CTRL+ALT+S and wait for a window.
On the Window, select DEPENDENCY TAB on top.
Click on + BUTTON at top right. Click on Library Dependency.
Now paste com.github.yesidlazaro:GmailBackground:1.2.0 there.
Click on OK and exit the window. Gradle recompiles.
Now simply put your code, If RED syntax error showing, Click at the code where error is showing and when a BLUE pop comes, Press ALT+ENTER.
Everything done..!

The Preview Doesn't Displayed At The Launching (Android Studio)

I started learning the Android SDK and I have a problem.
I launched the studio and the android screen didn't show up (YES, I know where the preview tool bar and how to switch).
Quote:
"Rendering Problems The following classes could not be found:
- android.support.v7.internal.widget.ActionBarOverlayLayout (Fix Build Path, Edit XML, Create Class)
Tip: Try to build the project."
*Sometimes there is another rendering problem and this is showed :
"Rendering Problems The following classes could not be instantiated: - android.support.v7.internal.widget.ActionBarOverlayLayout (Open Class, Show Exception, Clear Cache)"
I'm pretty sure its the "appcompat_v7", which is automatically generated in eclipse when setting up an android project. It imports unnecessary features and causes a bunch of build errors.
Delete "appcompat_v7" folder
go to your MainActivity
Delete the import "android.support.v7.app.ActionBarActivity";
change "MainActivity extends ActionBarActivity" to "MainActivity extends Activity"
add import android.app.Activity;
add import android.os.Bundle;
add import android.view.Menu;
add import android.view.MenuItem;
go to res folder and then go to your values folder...delete styles.xml
delete values-v11 folder
delete values-v14 folder
go to menu folder, and click on main.xml...go to "item" tag and delete app:showAsAction="never"
go to AndroidManifest.xml...go to "application" tag...delete android:theme="#style/AppTheme"
go to project at the top...clean and build...you should have no more errors.

Android Is Remembering My Old Package Name?

When I first created my test project I told it that my package was com.example.test. Now, i've renamed the source package to com.imcool.android, and I also updated the package in my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.imcool.android"
android:versionCode="1"
android:versionName="1.0" >
After a full clean, Android asks me if I want to update my launch configurations, I click yes. I try and run an android unit test and I get this error:
Test run failed: Unable to find instrumentation info for: ComponentInfo{com.imcool.android/android.test.InstrumentationTestRunner}
If I change the package in my manifest back to com.example.test it works, even though my actual source package name is still com.imcool.android.
Whats the deal?
it's not android that has asked you . it's eclipse . :)
anyway, just go to the run-configuarations and delete all of the items that aren't needed anymore , and do the run again . this way it will forget about the previous configuration and will use only those that you ask of it.

R.id cannot be resolved [duplicate]

This question already has answers here:
R cannot be resolved - Android error
(108 answers)
Closed 10 years ago.
So I copied this tutorial example thing right from Google's android site and I ma getting an error that R.id cannot be resolved.
Here is my Java file
package com.TestApp.HelloWebView;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class HelloWebView extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
}
Here is my res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
You have to import your R class
import com.TestApp.HelloWebView.R;
Also as Demand posted you have to use a namespace for your layout.
?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
I had the same problem when I updated my Android SDK. Suddenly new projects could not generate the R.java file.
importing my R.java didn't help because it wasn't generated
cleaning and building the project didn't help either -- the /gen folder was still empty.
I found that after the upgrade, the adb server didn't start up properly. So a manually started it:
$ cd $ANDROID_SDK/platform-tools
$ adb start-server
Then I restarted Eclipse and did a clean/build on the project, which generated the R.java file.
If using Eclipse, run a clean build (Project/Clean...); this can often clear issues with the resources not being compiled correctly.
Did you try any of the solutions listed here?
R cannot be resolved - Android error
Also, look for your R.java file. Are there any errors with it?
I had this problem too. I can't say a solution in general, but you can make a new workplace and copy the hardcode. In the future never click on clean or build, instead save or start eclipse again.
Go in your main.xml file, delete a char and rewrite it. That will generate the R file again.
On trying an Android beginner's tute (http://mobile.tutsplus.com/tutorials/android/android-sdk-development/) the "R.id cannot be resolved" error had me stumped too. In my case it was an android:onClick in /res/layout/main.xml. It seems onClick is not supported in AVD platform 1.5 API level 3.
A change to a new AVD with platform 1.6 API, level 6 removed the error - also started the project from scratch with the new api and level settings. For absolute Eclipse newbies (like myself) create a new AVD with:
Window -> Android SDK and AVD Manager -> New. Select platform, level number from "Target" drop down list.
Also try deleting the ~.android\debug.keystore (Happened to me, thought it was due to R.id issue). Real problem was debug.keystore was older than a year!
I ran into this problem when I tried to follow Lars Vogel's Android development tutorial...it turns out my problem is simply because I didn't save the layout file. Once I save the file, it works fine.
make sure you do not have any xml files with capitals in the name.
I had the same problem.
all the file names in res (and all its sub folders including layout) must be lower case only. This includes image files, xml files, etc (even if they aren't referenced anywhere within the project.)
if you posted full your main.xml file, then it's not right. You must write a namespace of xml:
xmlns:android="http://schemas.android.com/apk/res/android"
for example:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="78dip"
android:layout_height="65dip"
android:paddingTop="4dip"
android:textSize="12dip"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#color/bright_text_dark_focused"
android:gravity="center_horizontal|center_vertical" />
Yeah another fix
I started the emulator. By running a helloandroid program, that did compile.
And got out of eclispe and got back in don't know which one fixed the problem..
Cleaning the project etc didn't fix it - can i have 2 hrs of my life back!
import com.TestApp.R;
isn't accepted once R is lost.
My Eclipse now auto creates a new "res" folder everytime I "clean". Yes, I have two folders named "res" in parallel directly under my project folder. The auto created one is empty. String values are still found. No other errors than "R cannot be resolved" are reported by Eclipse.
Eureka! I've managed to make my first android project import to Eclipse!
Earlier I always got "Unable to find system library" under the project's Properties - Java build path - Libraries tab and it was impossible to correct it once it occured. The consequence was that no import statements could be resolved by anything imported.
In this case, I changed the JDK compliance level from 1.6 to 1.5 BEFORE I imported (afterwards its too late and cannot be mended). It must be done from Window - Preferences - Java - Compiler (earlier I thought this option was available only by right-clicking on already imported project). Then right-click on each project and set Properties - Android to a suitable Target (with the errors above, eclipse ignores any selection here). Then, I could even change back to JDK compliance level 1.6.
So, in general, changing the JDK compliance level before importing, might help.
And when deleting a project ALWAYS check the "Delete project content on disk (cannot be undone)" checkbox in the "Are you sure" dialog. Otherwise it is impossible to re-import the project, o if you've imported it with the wrong JDK compliance level the first time, it is impossible to correct the mistake, other than to try in another workspace.
These are the experiences of a very frustrated Eclipse user.
Make sure there is not a hidden error in one of your .xml files.
Sometimes you can delete a statement in the strings.xml file that is important.
For example, in the Vogella example, I got the error:
Error: No resource found that matches the given name (at 'title' with value '#string/menu_settings').
I had deleted the line:
<string name="menu_settings">Settings</string>
Putting this back in or deleting res/menu/main.xml would solve the error.
I had the same problem but got fixed
i dont know exactly how this problem gonna fix
Just try, It might be helpful
add this code at the top of ur code
import com.MyFirstApp.activities.R;
here com.MyFirstApp.activties is my component name replace it with your component name

Categories

Resources