Insert } to Complete ClassBody error even with matched braces - java

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.

Related

Cannot find symbol class Intent

I am new in android development and coding java and xml.
but I was following this tutorial:
http://www.steventrigg.com/activities-and-the-action-bar-create-an-alarm-clock-in-android-tutorial-part-1/#comment-296
then I had this error when using Intent. The word "Intent" under switch became red and there is an error "cannot find symbol class Intent"
Can someone explain to me what is going on and how to solve this?
This is the last part of my code under AlarmListActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.action_add_new_alarm: {
Intent intent = new Intent(this,
AlarmDetailsActivity.class);
startActivity(intent);
break;
}
}
return super.onOptionsItemSelected(item);
}
Look at your AlarmListActivity again and check the import statements at the top and make sure it includes the line:
import android.content.Intent;
If you intend to use any pre-existing classes that aren't part of the java.lang package, you generally have to import those classes. An Android Intent, for example, is a pre-built class, written by the Android development team, that allows for notification of other apps/activities. If you want to use Intent, you'd then have to import the package containing Intent.
When you write new Intent(), the compiler sees that you're requesting the construction of a new object, but because that object is not found in the java.lang package, it needs to know where to look for a blueprint to build that object. The import statement is the location of that blueprint.
I took a look at the tutorial and in the manner of experienced programmers, the author seems to have glossed over a few basic, but nonetheless, important things, such as the import statements that make his sample code work.
I had a same problem which I just resolved, by understanding how Android Studio indexes files, As you know Building an Android App is quite complicated process. So Android studio has some internal references which it keeps getting updated on change of every file that you have created.
I arrived at this post while searching for the solution,
This is how I got this problem
I usually wont create an activity under the main project package, I create sub packages to organize files as per the design pattern that I use, for eg If my APP name is com.example.testingaravind then inside that I usually create packages such as activites, services, models, managers etc ... So today I just created an activity first and then moved that activity into activites package via Android Studio, I started facing the same issue what you have described, Below was my source code
public class BootstrapActivity extends ActionBarActivity {
private static final String TAG = "BootstrapActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bootstrap);
}
public void startServiceOnClickHandler(View view) {
Intent intent = new Intent(BootstrapActivity.this , AnalyzerService.class);
startService(intent);
}
}
In the method startServiceOnClickHandler it was showing an error saying,
"Cannot resolve constructor Intent" I searched a lot in google and found that
When I move a file from one package to other package, my manifest file wont get updated, in the manifest we mention the activity name and its package path in my case it should be
android:name=".activities.BootstrapActivity"
But it was
android:name=".BootstrapActivity"
Because of this, Android studio was unaware that a class called BootstrapActivity exists inside the activities folder,
This seems to be a bug in the way how android studio works. Android Studio has to update manifestfile when I move the activity class file from one package to another package.
I am posting this to help others who might arrive at this post with the similar usecase.
Check name in manifest file in activity tag specify correct package for example your
<activity
android:name="Your-Package.MainActivity"
android:exported="true" />
In second case If you are using kotlin class inside java class, after configuring kotlin in project can solve the project...

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'

Main layout not recognized, immediately after starting new Android project

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.

setContentView(R.layout.main); error

package com.elfapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button btn_Login;
private EditText et_UserName;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_Login = (Button) findViewById(R.id.button_login);
btn_Login.setOnClickListener(this);
et_UserName = (EditText) findViewById(R.id.editText_userName);
}
public void onClick(View v) {
if (v.equals(btn_Login)) {
// skriver ut en toast när man klickar på knappen
Toast.makeText(MainActivity.this, "Ansluter till server...", Toast.LENGTH_SHORT).show();
// används i debuggern för att påvisa att programmet exekverat hit
Log.v("ThisApp", "onClick Successful");
// TODO skickar det som står i et_UserName till controller (genom TCP/IP), som ska kolla om användaren finns
// send et_UserName.getText().toString() to controller
// if(username exists)
Intent intent = new Intent(this, RoomActivity.class);
this.startActivity(intent);
}
}
}
I'm getting an error on the line containing setContentView(R.layout.main);
Not sure about what the error/exception is because I'm not used to working in Eclipse..
This just happend to me a minute ago, but after researching a while, and read this post I notice this.
There is a custom R class with you app name, so when you try to import the missing class (in Eclipse, press Ctrl + Shift + O to import missing classes (Cmd + Shift + O on Mac)), you should see two posible classes the normal:
import android.R;
And a custom class with your project namespace:
import com.yourname.yourapp.R;
If you choose the custom class, problem solved!
Just take 2 steps and problem would be more likely to get solved:
Step 1:
Clean your project by clicking Project -> Clean.
Step 2:
Rebuild your project by clicking Project -> Build All.
Also make sure that your layout xml files are syntax error free and you don't have any image which has non-acceptable names (such as a "-" between image name).
Also I request you to have a look at problems window and let me know what errors are being shown there.
Using NetBeans 7.0:
If you fix imports before R.java has been generated for your project (before building it the first time) it will add the line:
import android.R;
which will override the local R.java that you are trying to reference.
Deleting that line resolved the errors for me.
Step 1 :
import android.*;
Step 2 :
clean your project
Step 3 :
Enjoy !!!
if you have multiple packages with different classes then it will be confusing: try this:
import package_name_from_AndroidManifest.R;
is this already solved?
i also had this problem. I solved it just by cleaning the project.
Project>Clean>Clean projects selected below>Check [your project's name]
This problem usually happen if eclipse accidentally compile the main.xml incorrectly.
The easiest solution is to delete R.java inside gen directory.
Once we delete, than eclipse will generate the new R.java base on the latest main.xml
Simply:
Right click on your project.
Go to properties.
Select android (second option in the Left panel).
Click "add..." (in library), select your project.
Click ok.
And finally, clean your project.
If this doesn't work, make sure that "android-support-v7-appcompat" is in your Project Explorer.
If it isn't there, you can add it by importing a simple project from: C:/android-sdks\extras\android\support\v7\appcompat
use code : setContentView(R.layout.activity_main); instead ofsetContentView(R.layout.main);

Trying to make a Adapter class to choose .Java file depending on firmware version

What I did was create two .java files. One that can compile and run on a 1.5 phone (SDK3) and then one that works on 2.0(SDK5) So for this example i'll call the 1.5 file ExampleOld and the new one Example. I was wondering if i just made activity like this if it would work sort of like a "portal" and pick the activity to load depending on the SDK so there is no crash or compile errors. Are there any changes I should make to my code? Maybe anyone out there that's had to do this before. thanks!
package com.my.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
public class ExamplePortal extends Activity {
int sdk=new Integer(Build.VERSION.SDK).intValue();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (sdk<5) {
Intent v = new Intent(this, ExampleOld.class);
startActivity(v);
}
else {
Intent v = new Intent(this, Example.class);
startActivity(v);
}
}
}
What you're doing (correct me if I'm wrong) is trying to maintain backwards compatibility while making use of new APIs if the user is running a newer android version. The best way to do this is to follow the tutorial Google posted here. This avoids any verification issues and is really the best way to do stuff imho.
I would put this decision in a Factory Class to avoid having these if-else statements all over the codebase.

Categories