I downloaded a ViewPager example from GitLib and, unfortunately, it had a "Sherlock theme" packaged with it that got all sorts of errors. I was advised, on another post to remove the theme, one step of that was to change the code below to extend from FragmentActivity instead of SherlockFragmentActivity. But when I do that I get errors.
setContentView(R.layout.main) errors saying that 'main' is not a field
the R.id.pager errors saying 'pager' is not a field
Since I don't understand how this code works yet, I don't know how to fix the errors.
Any insight would be appreciated.
thanks, Gary
public class ViewPagerFragmentDemoActivity extends SherlockFragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewPager pager=(ViewPager)findViewById(R.id.pager);
pager.setAdapter(new SampleAdapter(getSupportFragmentManager()));
}
}
It sounds like your R.java file has not been rebuilt (or you are pointing to the wrong one now).
Look in your imports for an android R file (IIRC it is com.android.R)... if it is there delete it. Then clean your project and Eclipse should then rebuild yours and import it.
Another possibility is that you have an error in an xml file. If the compiler cannot parse the xml file, it will fail to generate a new R.java file. Usually there is a little red flag on the problematic child.
Related
I'm creating an Android Library Project, and I have a class in which the programmer has to pass an R.layout.some_layout, to the instance, in which some view should have an #+id/text
So the idea is that I create an XML layout file, I add a view with an "text" as its ID, and after I instantiate the class I use object.setLayout(R.layout.some_layout);
In the definition of the class there is something like this:
public void setLayout(int layoutId) {
View view = findViewById(R.id.text); // error here cause of the R.id
}
The problem is that since it's a library project, my R.java doesn't have an R.id, so it marks an error on that line. Any ideas of how to do this?
There must be an error in one of your xml file
Kindly check your xml may be some syntax error remain with the xml file which is preventing to create complete R.java
Then
Clean & Rebuild your project and also you can Restart IDE if problem remains
Did you check if your R.java file was generated correctly? If not, please clean build your library project and check again.
I am brand new to android development so please bear with me. I have encountered a problem when I create a new activity. In the newly created activity there is the auto-generated overridden method 'onCreate':
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_contact);
// Show the Up button in the action bar.
setupActionBar();
}
On the line setContentView(R.layout.activity_find_contact);, I get the error: activity_find_contact cannot be resolved or is not a field.
I have looked up other questions on this topic. Some say to just restart eclipse - this didn't work. Others said to try cleaning the project - this didn't work either. The most promising answer said to delete the line:
import android.R;
I tried this, and of course then R gets the error: R cannot be resolved to a variable. So I followed the advice to import your.application.package.R; - which in my context translates to import com.example.databasetest.R;. However this just brings up the error the import com.example.databasetest.R; cannot be resolved to a type.
So now I'm stumped and back to square one. Any ideas?
R files are automatically generated.. so you don't need to import it or do anything. The error is because there is something wrong in your xml file or files.Got through your xml files and see if you can find any errors.
These errors might even be typos.So make sure all of it is correct and cleaning and running the project again should get your code working
Do not use the following import
import android.R;
This errors comes because when we create new app we delete hello world textview but that is referenced in strings.xml in res > values > strings
go to that and remove hello world , clean project and errors should be gone ...
Iam using Jellybean. When i was putting layout name, this layout name is automatically created same as menu name. Then i access layout for main activity, i can't access id. I got error with main cannot resolved variable.
Code:
#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;
}
I just recently ran into this problem as well.
It turned out that the generated name "activity_main" was not accepted. I removed the underscore and it worked. After I removed the _ underscore it was able to generate the R file.
Incidentally this was an error right from the originally generated code.
I had never seen this problem before.
Try cleaning your project!
Project - > Clean
Select "Build Automatically" so that every time you clean the project, it will be rebuilt. And yeah, make sure you are saving the file before cleaning it (I know it is weird but sometimes the errors refuse to go away until you save it and then Clean it). Make sure that there is no error in the files in the layout folder, as it sometimes prevents the auto-generation of the code in R.java
Also, make sure that your code in MainActivity.java does not have import android.R. Eclipse tends to add that when you are managing the imports.
I recently activated ProGuard for my Eclipse Android project. After adding external libs and dynamically referenced classes to the proguard.cfg, I don't get any errors when building the apk. I get however a NoSuchMethodError when I try to start the installed app.
I narrowed it down to a specific method called in the onCreate method of the main activity. To simplify things, here's what the class and method look like (I left out a lot of code, but I think this should illustrate it):
public class TestMain extends TabActivity implements OnSharedPreferenceChangeListener{
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
testMethod();
}
}
testMethod() is defined as follows:
private void testMethod() {
int charsLeft = maxPostMessageLength - someEditText.length();
...
}
When I remove the "someEditText.length()" part, the app starts. So, the way I see it, the method that can't be found is the EditText.length() method. Strangely, though, the app also starts when I remove "someEditText.length()" from the testMethod and put it directly into the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test = someEditText.length();
testMethod();
}
Does anyone know how I can get rid of this error and why I can call someEditText.length() directly in the onCreate method but not in a method called by the onCreate method?
Without using Proguard the app works fine, of course.
Edit:
I tried the -dontshrink, -dontobfuscate and the -dontoptimzie options in the proguard.cfg. With -dontoptimize the app starts without errors.
Still, it would be interesting what exactly causes this specific error.
The Proguard documentation proudly states: "The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes".
Well, I gave up with the 'shrinking' part of it after getting runtime errors like you describe. I added the line
-dontshrink
to the proguard.cfg
You can see which routines have been removed from your code by inspecting the file usage.txt.
I'm happy to say that in my projects it's always missing, meaning that the code is obfuscated but nothing has been removed. I don't get any runtime errors now.
I accidentally stumbled upon a possible solution. Well, it totally works in my case, so this IS a solution to the original problem:
Today, I implemented some code with #Override annotations, which didn't work, at first. Luckily, someone else already had the same problem and an easy Eclipse-related solution:
'Must Override a Superclass Method' Errors after importing a project into Eclipse
Now, I thought, well, if I was always using Java level 1.5 before, why not try ProGuard again, without the -dontoptimize option, now that I set it to 1.6. Can't hurt...
And what can I say, now the app starts and I don't get the strange error when EditText.length() is called in a method.
The optimizer may remove the method invocation and the method if it comes to the conclusion that the method doesn't have any side-effects. It should never create inconsistent code though, and I'm not aware of a problem like this. You should check if it persists with the latest version of ProGuard. Otherwise, you should file a bug report on the ProGuard site, preferably with a small example that illustrates the problem.
I had a similar problem to the OP and it ended up being a proguard config option I set -allowaccessmodification, removing this solved the issue.
I just started playing around with android development, and already with just an attempt at making a button, I have encountered a problem.
The error I'm given in the following code is right on "R.id.button1".
It says id cannot be resolved or is not a field.
Do I need to manually reference every single object I make in the layout xml file? I found that this did work, but it does seem to be a bit much for every button I want to make...
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
private Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}
I've been wasting a lot of time (two weeks) because of the same problem until I discovered the problem wasn't mine but Eclipse's.
I guess there's a lot of people with the same problem.
Just try this: Save your project, close Eclipse and then open it again. So simple.
Do I need to manually reference every single object I make in the layout xml file
Yes, otherwise you won't be able to do anything with those views. It's not that bad actually. So, each time you create a view in your XML, and you want to reference it, put an ID:
<View
android:id="#+id/the_id"/>
And then, from your code you can reference it using the R class. You can type, in the example, R.id.the_id and then Ctrl+Shift+O to make Eclipse auto import the needed files.
You can speed up your productivity by using frameworks like Roboguice; I think it's for lazy people, though.
This answer is not applicable to this question (looking at code you have provided). Just adding it if someone else stumbles here and above mentioned answers do not help.
If cleaning (Project --> clean) doesn't helps or saving and restarting eclipse doesn't help either, check for the following incorrect import.
import android.R;
Which Eclipse sometimes add by mistake on auto-import (Ctrl+Shift+O).
Remove that line (import) and it's done :D
Following this EXCELLENT tutorial , I encountered the same problem. After reading Carmello's answer (Sept 17, 2011. 07:23) I simply clicked File->Save All, and voila, 'button0' was automagically defined, and even syntax highlighted.
If "R.id.button1" is not defined, then you'll get a compile error, just as you saw. If you don't define this in the layout, then it won't be defined.
You don't have to specify every object you create in the layout, but you do if you try to reference it from "R.*". You can manually create buttons and other objects that are not specified in the layout.
I ran through the same issues for time being. Plz, do not forget to define as follows:
<View
android:id="#+id/button1" />
if you are using the id in your .java class.
Button b =(Button) findViewById(R.id.button1);
Being said that, the id defined in xml file must match with the id in findViewById().
Go to the 'R.java' file under the 'gen' folder and check whether your 'button1' is present under the class 'id'.If not,then this could be the reason you got that error.When you use the statement " R.id. " make sure that the is present under the appropriate class,in this case under the 'id' class.
R.id is a generated object that assigns int numbers to resources. Try this go to your gen/mypackage/R.java and delete the file. As you can see it is re-generated. This file provides static references where as the context is more of the dynamic state of your app. If you have syntax errors that will prevent automatic re-generation of that R.java file so you will get lots or R. errors. As everyone else has said you can click save all icon or ctl+shift+s on windows. You can clean the project project/clean and that will clean up 95% of those exceptions. Yes eclipse is buggy that way but netbeans does not support android that well. this link may help
Good luck
Do these things,anyone of this will help you
Project -> Clean,
Right click -> Fix Project Properties
Restart Eclipse
make some fake modification in manifest and save
check your console for any error message
check your drawable folder, check the image names satisfy the rules