Main layout not recognized, immediately after starting new Android project - java

The below code was automatically generated when I started a new android project.
I'm getting an error on the "R.layout.main" saying it does not exist.
I do in fact have a main.xml and I can see the layout change as I edit it in the Graphical Layout tab.
How can I fix this so I can run my application?
public class ComplimentGeneratorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

The package name of the R file and the package name of your source code may not match. Make sure the package your code is in is the same as the package defined in the manifest file. Otherwise you will need to import the R file with the full package name (ex: com.example.R.layout.main).
If they match, for some reason your R file was not generated properly. Try cleaning your project.
Also, start accepting some answers. I almost didn't answer this because of your horrible acceptance rate.

This may be a very simple answer, but it has happened to me before. Try to restart eclipse. File -> Restart

Try to import the R.java file with the full path inside the main activity class file...
import com.example.packagename.R;
Hope, this will solve your query.

Related

android: cannot find symbol TaskDescription

I am trying to set the color of the ActionBar while my app is "minimized" using the TaskDescription as suggested by the answer to this post: Setting header color of app in overview (recent apps) screen
Here is the relevant code:
...
import android.app.ActivityManager;
...
public class MainActivity extends SherlockFragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
...
int c = Color.parseColor("#0A782F");
ActivityManager.TaskDescription td = new ActivityManager.TaskDescription(R.string.app_name, R.drawable.launcher, c);
((SherlockFragmentActivity) this).setTaskDescription(td);
}
...
}
Although I have not posted the xml resource files for this project, you may assume that R.string.app_name and R.drawable.launcher are defined in xml files.
Here's the problem: when I compile this code, I get the following error:
error: cannot find symbol
...
symbol: class TaskDescription
location: class MainActivity
Now, from what I understand, Java throws the cannot find symbol error when you refer to a class or variable name that does not exist. In the case of classes, it is usually caused by forgetting to import the class you need. However, that cannot be the cause in this case, as I have clearly imported ActivityManager at the top of my class file.
Here are some other examples where people have done exactly the same things, just to show that I have done my research: http://www.programcreek.com/java-api-examples/index.php?class=android.app.ActivityManager&method=TaskDescription
Finally, I thought the problem might have been that you can only instantiate the TaskDescription class in an Activity context. However, I am using SherlockFragmentActivity instead of the standard AppCompatActivity. Unfortunately, I tried changing this and I still get the same error. Any ideas?
Thanks in advance for your help!
Found the solution! I didn't mention in the problem that I had built my project using gradle. The issue was that I was using an old Sdk version (in particular, I was using a version of the android Sdk which had not yet defined the TaskDescription class). Therefore, to fix the problem, all I had to do was add the following line to the appropriate place in my build.gradle file:
compileSdkVersion 21
Hope this helps someone else who is feeling lost for the same reason.

Android Unable to Import Library project

I read almost every answer obout this topi c but i cannot find a working solution for my case or maybe I'm missing somthing.
I use eclipse as IDE and I would like to use an external library for example I'm trying to add this Library to my project but I don't understand what i'm doing wrong.
I tried download the full project and import it into the workspace, mark it as library and then under my project and add it as reference.
If everything is ok how should I call an activity from the linke library?
I tried to defind the linked activity into my manifest file but without success.
Could you please point me in the right way?
Thank you
You need to write code which will call the library activity inside onCreate() or any other method which you choose like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//Here you need to call directory chooser activity.
final Intent chooserIntent = new Intent(this, DirectoryChooserActivity.class);
final DirectoryChooserConfig config = DirectoryChooserConfig.builder()
.newDirectoryName("DirChooserSample")
.allowReadOnlyDirectory(true)
.allowNewDirectoryNameModification(true)
.build();
chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config);
startActivityForResult(chooserIntent, REQUEST_DIRECTORY);
}
Import DirectoryChooserActivity and DirectoryChooserConfig class in your activity; If you are not able to import the mentioned class then you are not added library into the project correctly.

Just update ADT and Unable to resolve superclass errer appear

The error show after I update my ADT, the project is created before the update.
In Login Activity,
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Do something here... if login success, Start another activity
Intent i = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(i);
}
.......
}
In MainActivity,
public class MainActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
//......
}
}
I try to remove all the code inside and back to a blank activity. The error is not happen because of the code inside the activity. I think is abut the extend FragmentActivity
Here is the error trace:
Unable to resolve superclass of Lhk/iactive/imoodle/activity/MainActivity; (15)
Link of class 'Lhk/iactive/imoodle/activity/MainActivity;' failed
I get this error after I update my ADT.
I do some research on this topic. I try to import the android-support-v4.jar again and this do not fix the problem.
I also read some blog like THIS ONE can not help me too :(
I have solve the problem by myself. I am going to share the solution hope there is other people who suffer from the same error can save some time :D
I try many method for example go to build Path Config -> Order and Exprot, All the library is already checked. And the NoClassPathFound error is still showing.
I have no external Jar for the project and The error is from FragmentActivity from Android Support Library.
Then I delete the android-support-v4.jar complete and add it again by right clicked the project -> Android Tools -> Add support library...
After the new support library added. All the error fix. :D
This worked for me:
For all projects (app and library):
Right click the project, and choose Properties
Go to Java Build Path-Order and Export.
Tick 'Android Private Libraries'

Insert } to Complete ClassBody error even with matched braces

Yes. I know this is trivial. But this is getting silly.
Image proving the error:
The code:
package apack.age;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class DisplayContactActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display_contact_layout);
}
public void openSubreddit(View view)
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com")));
}
public void openTwitter(View view)
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com")));
}
public void openGmail(View view)
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("www.gmail.com")));
}
}
Seriously. There's 5 open and 5 close braces. This is my first attempt at an android app but I have 3 years of Java experience so this is pretty frustrating.
Run errors out. Refresh does nothing. Project clean does nothing. Auto code format in eclipse (CTRL + SHIFT + F) does nothing.
EDIT 1: Copying the code to a new class gets it to compile. I'm running the new version of Eclipse: Juno. I've done source -> cleanup and it just says "nothing to change" every time!
Edit: Please upgrade to ADT 20.0.1, where this has been fixed.
Switch to the Lint view, use the Clean button to remove all lint errors. There is a bug with lint in ADT 20, that it sometimes takes over the Java compiler bugs.
You can verify if this is really the problem by looking at the "Origin" column of this bug in your Problems view. Normally that should be "Java problem" for this error, but it will be "Lint" instead.
Try to save the file with Ctrl+S, it happens to me sometimes. Also, it seems that you forget to insert the package name at the beginning of the code:
package com.example.my_application;
There's gotta be a snapin in your eclipse with the issue. Have you tried a virgin eclipse install?
Here are some tips for Eclipse users who are experiencing this issue. Please try the following, which solved this problem for me (I was using this download of the IDE: eclipse-SDK-4.2-macosx-cocoa-x86_64.tar):
1) mv the offending source .java file to a new file.
2) Refresh eclipse (the project via right-clicking the project name).
3) mv the new file back to the original name.
4) Refresh eclipse again.
This solved the issue for me. It was nothing about the code, it happened for me, intermittently throughout my coding session, even in a very small file in which it was not easy to misplace any closing braces :-)
I tried some of the solutions suggested here, no luck, then tried the below, seems good.
I moved the Java file out of the source tree. Of course, compile errors lit up everywhere else in the tree.
I created a new Java file for the class. I right-clicked on an error to generate the file. I set the parent class in the dialog.
I fixed the errors in the file - constructor required because of parent class - with temporary code.
With the old code open in a text editor (Notepad++), I chunk-by-chunk copy-pasted the old code in, fixing imports as required by right-clicking on the errors.
It compiles happily now.
Grumble. Flaky tools. Grumble.

android app test error

When I run my first app, I got error.
Code of the activity:
package com.pradeep.ulc;
import android.app.Activity;
import android.os.Bundle;
public class New1Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Error:
R cannot be resolved to a variable New1Activity.java /new1/src/com/pradeep/ulc line 11 Java Problem
How can I fix it?
1) Make sure the package name is correctly set everywhere
2) Try doing a clean build to regenerate R.java.
If you must use a different package, import the R.java from the package specified in the manifest.
It means that you have problem in your resources, so R file cannot be generated.
What do you have in res/layout/main.xml file?

Categories