android Project add new activity error ? - android

I am having trouble adding new activity to my android Project because of this error
error MSG:
R cannot be resolved to a variable testActivity.java
I really want to know what is correct way to add new activity to my project and how to solve this error?

How to add a new activity:
Right click in your project
New > Class
Enter the name of your Activity.
In super class field: android.app.Activity
Also, make sure you clean your project, close Eclipse and launch it again.
Finally, double check that you included your new activity in your manifest file.

You need to clean your project a few times. Save the code and clean the project and make sure you have not used import andorid.R;

Related

Trying to use code from another project by including it in my build path

I'm trying to figure out a way to share code across projets. this is what i did
created a project called sharecode and made the following class
package codelib.com;
public class cShape {
}
made a android project called testsharecode
whent to properties->java build path and clicked on projects at the top.
clicked add, and added sharecode
created a class to try include the cShape class from the sharecode project
package com.example.testsharecode;
import codelib.com
public class parent {
cShape test;
}
On my import i get a error saying
"only a type can be imported, cadelib resolves to a package"
on cShape i get a error saying the def does not exist.
How can project testsharecode use the code in projetc sharecode?
have you tried the following method :
right click on the distination projet, Build Path -> configure build path
chose project then select testsharecode
back in your distiation project now you should be able to use your public classes.
hope this helps

Adding a new class in android project must import R.java file

I just added a new class to my Android project, and I have an error that says that I have to import R... I don't understand because R is not supposed to be imported... I don't know why it is not recognized in this class and it is recognized in the others ?
I had several packages of an existing project and I created a new package and created a new class in that extends Activity. I also added the activity in the Manifest. The class contains only the basic onCreate method and the problem is on the line setContentView(R.layout.activity_manage_card) that gives me the error

ActionBarActivity and FragmentActivity... "Inconvertible types"?

I'm currently having a curious problem, trying to use simultaneously v4 and v7 (actionbar) compatibility libs.
Let's say I've got my own Activity class, that extends ActionBarActivity on one hand and, on the other hand a Fragment, inside of which I call "(MyActivity)getActivity()".
I checked the imports and my Fragment is a v4 Fragment.
In a v4 Fragment, calling "getActivity()" returns a FragmentActivity.
I also checked: ActionBarActivity extends FragmentActivity.
Now here is my problem:
I don't have any error in the editor (no red line).
I'm using Android Studio.
When I try to "Make" the project, the compiler returns an error for each time I call "getActivity" inside my Fragment.
It tells me he has a FragmentActivity but a ActionBarActivity is expected and tells me those classes are incompatible.
It interrupts and I can't even test my app.
It may be a problem with the gradle configuration, but I added "compile "compile "com.android.support:appcompat-v7:18.0.+"" in the "dependencies" part of my build.gradle file, as said on this page:"http://developer.android.com/tools/support-library/setup.html", and it doesn't change anything...
Any idea?
EDIT :
As an adddition to my yesterday's post, even if everything is explain before, here are some code parts and the error message Android-Studio gives me:
First of all, my activity:
...
import android.support.v7.app.ActionBarActivity;
...
public abstract class MyAbstractActivity extends ActionBarActivity{
//Do some stuff here
}
Every activity of my application extends this class.
Now here is my fragment:
...
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
...
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Do some stuff...
((MyAbstractActivity)getActivity()).getSupportActionBar().setTitle(R.string.app_name);
}
//Do some stuff...
}
No error is shown in the editor.
But when I "Make" the project, I get this message in the "Event Log":
Compilation completed with 5 errors and 0 warnings in 4 sec
In the "Messages Make" part, I can see this for each time I try to cast "getActvity()" in "MyAbstractActivity":
java: inconvertible types
required: com.myapps.abstracts.MyAbstractActivity
found: android.support.v4.app.FragmentActivity
Well, I'm sad to say that, but I finally downloaded Eclipse, imported my project, and solved my problem, using the error messages of Eclipse.
It told me to "Fix Project Properties", I clicked, it did everything on his side, I tryed to launch the app, it worked.
Now that the problem is fixed, when I come back to Android Studio, it works too... But, for the moment, I think I'm gonna stay with Eclipse... Too much time lost for nothing. :(
Android Eclipse plugin gets flaky. Try to clean your project, then rebuild. Also, try restarting Eclipse. Also verify your project build path library order. Also, try to rebuild from the command line in the project with 'ant clean; ant debug', but do it while Eclipse is closed.

Android Nullpointerexception thrown when R.id value not found

When I add a new activity (.java and .XML files) in my Android Project some R.id values that used to work get lost and causes my App to throw a NullPointerException, but if I use the hex value it works again:
R.java: public static final int editTextTotal=0x7f040064;
findViewById(R.id.editTextTotal); //Throws nullpointerex after I add a new activity.
findViewById(0x7f040067) //Works
I got tired of Eclipse so I'm working with commands only, is there anything else that should be done other than add the files, change the manifest and run the "ant debug" command to add new activities?
What is the import your are using? You have to use
your_package_name.R
There is something wrong with your ADT. Try to update to the latest version.
If the problem still exists, try to clean the project whenever you add a new xml.

Could not find class XXX referenced from method XXX.<YYY>

I'm working on a libGDX project and I have a class called CheerVArachnids that has another inline class which is an event listener. When I run this project on the desktop it works fine. BUT when I run on my Android device, it can't find that inline class and I get the following error:
Could not find class 'com.bbj.cva.CheerVArachnids$PlaceUnitListener', referenced from method com.bbj.cva.CheerVArachnids.<init>
Here are the important parts of my class:
package com.bbj.cva;
public class CheerVArachnids implements ApplicationListener {
class PlaceUnitListener implements EventSubscriber<PlaceUnitEvent> {
#Override
public void onEvent(PlaceUnitEvent event)
{
//
}
}
public CheerVArachnids() {
EventBus.subscribe(PlaceUnitEvent.class, new PlaceUnitListener());
EventBus.subscribe(RemoveScreenObjectEvent.class,
new RemoveScreenObjectListener());
}
}
Any ideas why on Android, at runtime it can't find that inline class?
Since some ADT-Version you have to set which libraries / projects should be exported too.
Project-Propiertes -> Java Build Path -> Order and Export -> Check your Sources and other Libraries you are using.
Do these Export-Settings for your Core- and Android-Project.
Then it should work fine on Android.
In my case, everything worked fine until I installed the new updates for the SDK and Eclipse.
I got an error: "Could not find class..."
I found solution in another stackoverflow site.
I have a similar problem when using external jar (in my case openCSV). The reason I had a problem was due to a change in ADT 17 (or above). What I needed to do to resolve the problem was
In Eclipse go to Properties -> Java build path -> Order and export.
Mark my jar.
Move jar to top of the list.
The solution was found in the following page which reference to a very good article.
First you should: import XXX(Class).java,
If you added external library jar file import them to the LIB folder.
After that:
Right Click to your Project -> Properties -> Java Build Path -> Order and Export(tab) -> select All -> press OK -> Clean your Project.
Hope this solve this issue

Categories

Resources