Layout can't be found - android

When I use the following line :
LinearLayout layoutRoot = (LinearLayout) findViewById (R.id.layoutRoot);
it gives an error saying that R.id.layoutRoot cannot be resolved.
what does it mean?

Verify that in your imports, you have something like:
import com.myproject.R
and not
import android.R

Check your R.java file (which is in the gen directory) and you will see that the layouts identifiants don't belong to the id class but the layout class.
public static final class id {
}
public static final class layout {
// Layouts are here...
}
So it should be: R.layout.layoutRoot.

Save the project
Exclude bin, gen from svn (if you use it)
Commit, update (if you use svn)
Clean
try to build again. If won't work:
Delete gen, bin
Save
Build.

Related

Why does my BuildConfig class keeps getting duplicated every time I run my project

I migrated my project to Androidx and every time I run my project I keep getting this error that says class BuildConfig is public, should be declared in a file named BuildConfig.java. I know that the cause of my is that every time I build my project my java BuildConfig class keeps getting duplicated. Which gives it a name such as BuildConfig1, BuildConfig2 etc.. I constantly have to keep deleting it and rerunning my project in order to compile this is really annoying does anyone know the cause of this and perhaps a fix? This is my BuildConfig class
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.examp.smartshop";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
}
Try to clean project(Build->Clean Project) and rebuild Project(Build->Rebuild Project).
or
Try deleting .gradle folder , build folders, .iml file.( You can do thing by going to Project window and choose 'project' option. And do the rebuild project again.
or
last resort File->Invalidate Cashes/Restart.
Vote If it works.
Happy coding.
Thanks.
Under the project search for a folder named ".gradle" and while at it search for another under the 'app' folder named "build". Delete the two folders and run your app. It will solve the issue. See this
The error arises as a result of duplication of files hence Android Studio is unable to determine which files to use.
I had this same issue after doing some package restructuring.
I tried deleting the build and .gradle directories and restarting Android Studio with invalidating caches with no luck.
After the package restructuring, not only did my androidTest files end up in my main directory, eventually I noticed I had ended up with a BuildConfig.java in my source directory. I really should have been more careful with the refactoring preview.
Deleting the BuildConfig.java from my main/java/package directory resolved this issue for me.

CS0103 The name 'Resource' does not exist in the current context

While I create android app in Visual Studio with Xamarin I got strange error - CS0103 The name 'Resource' does not exist in the current context. When I do Clean and then Build, there are no any problems, build is success. But when I changes something in code, Error appears again.
I tried to make changes according this post but all file names in Resources folder are ok. Can anybody help me with this problem?
short:
make sure your Namespace is same across all these files that is mentioned below ;
Resource.Designer.cs
every Activity that procudes error:"cs103 resource doesnt exist .."
yourproject.csproj (edit vith notepad)(verify "RootNamespace" ,"AssemblyName" is same with your namespace )
after you make them all same Namespace clean+build should work
long:
if you have changed namespace in anywhere in the project,
you gotta Find And Replace oldNameSpace to NewNameSpace inside yourProjectName.csproj file
(open with text editor outside of vs2015 ide)
why ??
when you compile project namespace in the Resource.Designer.cs is overwritten with this values from csproj file
Any Activity:
namespace yourNew_NameSpace // <-- Attention here !!!!!!
{
[Activity(Label = "asdf", MainLauncher = true )]
public class act_main : Activity
{
TextView tv_camResolution;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ly_main);
tv_camResolution = FindViewById<TextView>(Resource.Id.textView1);
}
......
.csproj file :
....
<RootNamespace>OldNameSpace</RootNamespace>
<AssemblyName>OldNameSpace</AssemblyName>
....
Resource.Designer.cs:
//▼▼▼below see oldnamespace
[assembly:global::Android.Runtime.ResourceDesignerAttribute("OldNameSpace.Resource", IsApplication=true)]
namespace OldNameSpace // <--- and here see oldnamespace
{
....
I have the same error, but my code compile. so its maybe related to the wrong import or ambiguous Resources object. Note that there is Resources and Resource i believe Resources is coming from android sdk.

R cannot be resolved to a variable, rebuilding doesn't solve

Can someone help me?
public class Maths4to5Home extends Activity implements OnClickListener
{
private Button button_4to5PlayGame, button_mathsInstructions, button_4to5Scores;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maths4to5home); //error
button_4to5PlayGame = (Button)findViewById(R.id.button_4to5PlayGame); //error
button_mathsInstructions = (Button)findViewById(R.id.button_mathsInstructions); //error
button_4to5Scores = (Button)findViewById(R.id.button_4to5Scores); //error
I have included a sample of code above and highlighted where my errors are. I am being constantly told that R cannot be resolved to a variable. I have tried cleaning and rebuilding. Also I have tried importing R (as the quick fix suggests) but then I am told that for example in my setContentView() that
activity_maths4to5home cannot be resolved or is not a field
Any help would be greatly appreciated!
You need to import the correct R — specifically, the one for your project. This is based on what's in your project's manifest. If your manifest declares the project's package to be com.example.my_project then you need to import com.example.my_project.R;.
If your activity is already in the project's package, then you do not need to import R. However, you specifically need to not import android.R.
The error message may also be caused by a missing R.java. This can happen if errors in your resources prevents R.java from being generated. You should check that there are no errors in any of your resource files.

R.java problems with generating

I tryed to create new project and in my R.java there was next code, for example:
public static final class layout {
public static int activity_main=0x7f030000;
instead of
public static final class layout {
public static final int activity_main=0x7f030000;
So I can't build project normally, because it generates public static int without final.
What should I do?
Right Click on project --> Android Tools -->fix project properties
before doing this remove import statement like
import android.R.*..
or try clean project from
project --> clean
This could be because of many things.
1. Check your xml files have no errors in them as this can stop R.Java from being automatically generated.
2. Clean your project. This will rebuild your project.
3. Delete any android.R import statements.
4. Close and re-open eclipse.
I also wouldn't recommended fiddling with the R.Java file dirctley.

How to fix NoClassFoundError in android

I have two classes in a different projects.
I am importing the project that has the class that I need.
Now, I have no errors.
The project know's that class and I even made an object from that class, but when I'm trying to run it it crushes with the "NoClassFoundError".
How can i fix that?
import com.dmg.pixelservice.core.*;
import android.app.Activity;
import android.os.Bundle;
public class show extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Pixel pixel = new Pixel();
pixel.doIt(this,"fd7ccf36-3f85-11e1-8671-40409e0f44a1",true);
setContentView(R.layout.main);
}
}
When I debug, I can see that it crashes when I'm trying to do Pixel pixel = new Pixel();
Please help.
create new libs folder in your application .name must be same.and put your jar files in this folder.and
go to
java Build Path -> Configure Build Path -> Add jars
Looks like the jar file containing the Pixel class is not packaged into the APK.
To make that happen, it seems you need to copy the jar into the libs folder of your Android project.
See this question:
Approach for fixing NoClassDefFoundError?
Android Developer Guide:
http://developer.android.com/guide/developing/projects
it is easy to find the error if you send the src code of your Pixel class.

Categories

Resources