I noticed that even though I made a new xml named drink, there is no ".java" file with it. Do I need to make one or do i add it on to MainActivity.java?
This code is in my MainActivity.java and the Intent line is saying that drink cannot be resolved to a type.
public void calculate(View view){
Log.i("clicks","You Clicked B1");
Intent i=new Intent(MainActivity.this, drink.class);
startActivity(i);
}
This is in my manifest:
<activity
android:name=".drink">
</activity>
This is a post before I knew android. I didn't realize you have to make the java file and xml file.
This code is in my MainActivity.java and the Intent line is saying
that drink cannot be resolved to a type.
This means that you don't have a "drink" class. you have to create a new class called "drink" that extends Activityin the application package.
Without any further code to go on (including knowing what your drink class and your manifest.xml looks like), I would check to make sure that the package name specified in the manifest tag of your manifest file matches the package name of your drink class.
I am not sure I understand you correctly, but to go to a different 'page', which is in your case an Activity, you have have to create one yourself.
If you do not see a .java file representing your Activity you should make one like this (note the uppercase Drink for naming conventions):
public class Drink extends Activity{
protected void onCreate(Bundle savedInstance){
this.setContentView(R.layout.drink.xml)
}
}
I am assuming you already made a xml file for the layout of the Activity. Please read the helpfull documentation of Android.
This means that you did not create the class yet. Right - click on your package name in Package Explorer and select Class. Then, create your activity with your desired class name. Also, check if you declared your android:onClick = calculate in your XML file if you get a yellow attention sign that says that your method is not used.
Related
I ran the HelloWorld android app and now I moved on to making buttons and stuff like that. I am able to create the buttons in the layout xml and all that, but I ran into some confusion over Eclipse not recognizing my Intent declarations.
Here is a snippet of code:
addProblemButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
The CurrentActivity and NextActivity classes do not seem to be recognized and Eclipse and it doesn't give me the option to automatically create the import statements for it.
What is the package that these classes are in? Is it an issue of some things not recognized? Or some package that needs to be installed/downloaded? Whats the best practice way to handle such a situation?
Also, do I need to add listeners if I already added the buttons to the layout?
Thanks!
I believe CurrentActivity and NextActivity are just being used as example names for classes for launching an activity in whatever code snippet you were looking at.
CurrentActivity should be the name of whatever the Activity class is that you're launching the new activity from, and NextActivity would be the name of some new Activity class that you want to navigate to next.
It seems you are trying a tutorial. In your project, you should create your own classes extends Activity, named CurrentActivityand NextActivity, so Eclipse will know what they are.
2.If you just declare a button in the layout xml file, the app only show it, but doesn't know how to handle the click event on it, so you still have to register the listener for it. You can:
a. Set the android:onClick attribute for the button in the layout file, and then implement the method to handle the click event. I.e. android:onClick="click" in the xml, and add a function with that name in your code:
public void click(View v){
//Process click event here
}
b. register the listener fully in code:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Process click event here
}
});
Ubuntu 11.10 ADT 16.01 Java 6 Eclipse Java EE Indigo SR1 - All latest release following android dev requirements.
I am just starting to learn coding for android and got to a tutorial on switching Activities. Using the New project android template, I made another test activity class identical to the first except for name.
package com.test.SwitchActivity;
import android.app.Activity;
import android.os.Bundle;
public class SwitchActivityActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
The problem is that when I use the Menifest editor edit the AndroidManifest.xml file - add button for the Application Node, the listbox is empty. All the tutorials I have found are populated with xml tag choices. Now, I can add the xml data manually - it is no problem. What is driving me nuts is not being able to figure out why it does not work for me as it is supposed to work. I don't find other people having the issue and I have re-installed everything at least once before coming here to post another question.
I just want to finish the tutorial and switch between two activities.
Here is a screenshot of what I describe. Anyone have any ideas as to why it does not work?
http://i.stack.imgur.com/Fwfbz.png
To create another activity
Create a new class "myactivity.java" that extends the Activity
Create android xml file with your UI items in it lets call it exampleactivity.xml (it should be in small letters with .xml extension and should be saved in res/layout folder).
In myactivity.java add the following method (same as above except exampleactivity replaced main)
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.exampleactivity);
}
}
create a button changeactivity in SwitchActivityActivity and initialize it in onCreate method of SwitchActivityActivity via
changeactivity = (Button) findViewById(R.id.newActivityButton);
create the following in onCreate method of SwitchActivityActivity
changeactivity.setOnClickListener(new OnClickListener() {
Intent i;
public void onClick(View v) {
i = new Intent(Intent.ACTION_DEFAULT);
i.setClass(SwitchActivityActivity.this, myactivity.class);
startActivity(i);
}
});
In the main.xml create a button and give the button id by
android:id="#+id/newActivityButton"
Finally add the following in AndroidManifest file after the SwitchActivityActivity activity tag
compile and run it.
I have a problem that I can't seem to find the solution to.
I have an app that loads the main.xml file on startup, of course. In it are several buttons, and I want the buttons to take me to a different XML file. I just used setContentView(R.layout.newlayout.xml) method for that, and it works great.
The problem comes in after that. If I reference any of the buttons or other objects in the new layout, the app won't even finish loading before it errors out and closes on the emulator. However, if I take all references to objects out, the app runs fine.
I can navigate TO the new layouts, but their buttons can't do anything. Do I need to create a separate Java file for each layout? Or am I doing it all wrong? I'm trying to be as specific as I can. I suppose you could say I need to have different "pages" in my app as a website would.
I think what you are trying to do is best solved using multiple java files, each one defining it's own android Activity.
While it is possible to have multiple layouts/views in a single activity, this will generally make the code more complex and harder to read/debug in the future. By having each 'screen' in its own file, it will be a bit easier to manage all the different views you need to juggle.
The buttons and views only can refer to those mentioned in the current SetContentView() file..
u can test this by creating a button and initialising to an R.id... without setting the content view.. U will get a force close..
so if u change the XML file u shud initialise stuff again....
Ok, for anyone out there with the same problem and haven't figured out how to do it, as I said in my comment on ylebre, my Coworker and I have finally discovered how to do it. First off, we added
implements OnClickListener
to the class, after
extends Activity
then, we created a new java file, and at the beginning of the file it called
setContentView(R.layout.newlayout);
instead of main. Then, we made a button as follows:
Button button1 = (Button) findViewById(R.id.button01;
button1.setOnClickListener(this);
then later in the code:
public void onClick(View v) {
switch(v.getId()) {
case R.id.button01:
startActivity(new Intent(this, NEWJAVAFILE.class));
break;
}
}
And that's it! We just copied and pasted that code into NEWJAVAFILE, changed the names and such, and we were able to navigate freely back and forth. As ylebre said, all of the code for the new activity is in the NEWJAVAFILE.java. OH and don't forget to add the name of the java file to the manifest inside the tags:
<activity android:name=".NEWJAVAFILE">
</activity>
it all seems so simple now!
I am getting this exception while I am trying to call an activity from another one. The complete exception is
android.content.ActivityNotFoundException:Unable to find explicit activity class {com.x.y/com.x.y.class};
I am doing an intent.setClass("com.x.y","com.x.y.className") where className is the name of my activity class and com.x.y is the package it resides in.
My AndroidManifest.xml has the following content:
<activity android:name="com.x.y.className" android:label="#string/app_name">
Am I missing anything?
Maybe you need to check that you added the new activity to the manifest.xml file
Example:
<activity
android:name=".className"
android:label="#string/app_name" >
</activity>
If other people are encountering something similar and arrive at this post, an issue I had may save you some time. May not be related to the OP's problem but def related to the ActivityNotFound exception.
I was trying to load an activity by using:
Intent intent = new Intent( this, class );
However I continuously kept getting the ActivityNotFoundException even though I had checked and rechecked the code multiple times.
This exception I was getting wasn't actually being caused by the intent but some code I was running inside the loaded activity throwing a RuntimeException. (my issue was caused by Typeface.createFromAsset())
It is possible you are running into a similar RuntimeException in your activity.
To see if this is the case, put your intent code in try catch blocks. Like so:
try {
/* your code */
...
} catch ( ActivityNotFoundException e) {
e.printStackTrace();
}
Run your app again, and check your LogCat, if it's the same issue, you'll get a RuntimeException with a "Caused By:" entry pointing to your actual issue.
I spent a good hour trying to figure this out. Hopefully this may save someone some time.
The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too.
Delete your activity from the manifest and then add it again. This type do not write type the XML directly. Instead, go to Application > Application nodes > add, choose the Activity, and then browse for the file source.
This worked for me.
intent.setClass takes parameters as "Package Context" and "Class".
an example would be:
intent.setClass(CurrentActivity.this, TargetActivity.class);
also you need to check if the activity is registered in manifest file.
Added a new activity and defined it in manifest.xml, but I was still getting "Unable to find explicit activity class" error. I am using Eclipse. Solution for my problem was "cleaning" the project. From the main menu in Eclipse: Project|Clean.... Then you select your project and clean it.
Hey, you need to use another form of Intent constructor. This will surely solve your issue within a second:
Example:
Intent inte=new Intent(getBaseContext(),"your class name with .class extension ");
startActivity(inte);
This works perfectly and I checked this code, its working properly.
I had an ActivityNotFoundException when I implemented the Activity inside another class (as an inner class):
//... inside the utility class Pref
public static class Activity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
//...
Declared as the following inside the manifest:
<activity android:name=".Pref.Activity"
...
After declaring this as a normal class (public class PrefActicity) and changing manifest accordingly, it worked as usual.
I was using getActivityContext() (instead of Activity.this) for the menu code to save some work, and copy-and-paste it to each activity without editing each time.
I replaced them with Activity.this, and the issue is gone.
I have a feeling a smarter Android guy could work-around not having to do that. Would like to hear what it would be.
Looking at the documentation here what you want is:
intent.setClassName("com.x.y", "className");
Restart the Eclipse and check your Manifestfile again. If you find missing the respective Activity, then add it and try again. It solved my similar issue.
In addition to Mina's answer.
When you declare activity as inner static class then you should write your activity into manifest like ...
<activity android:name=".app.FragmentLayoutSupport$DetailsActivity" />
here .app comes from your package name , it can be .helpers.afdfa$afda
My solution to this error was to add a package name in front of the name in manifest.
I had the following activities:
id.scanner.main.A1
id.scanner.main.gallery.A2
My manifest contained the following:
<activity android:name=".A1" ....></activity>
<activity android:name=".A2" ....></activity>
This solved the problem:
<activity android:name=".A1" ....></activity>
<activity android:name="gallery.A2" ....></activity>
Yeah I got this problem too. I refreshed the project. And then, everything works fine.
when i have same issue. if you are using library class files and writing it into android manifest files write it like and then remove the library projects manifest files this portion>>
then it will work absolutely..
This exception also occurs if you include a library in your app and if the library is calling an activity defined in the library project. In this case we need to merge library's manifest with calling app's manifest.
With ADT version 20, we can do this by adding the below statement in project.properties of calling app.
manifestmerger.enabled=true
Check out the content of the Android Manifest File in the bin folder of the project. When your app is compiled and packaged the Manifest File is copied to the bin folder. In my case the Manifest in the bin folder did not agree with the original Manifest. This is probably a mistake of Eclipse. I manually copied the Manifest to the bin folder and it worked.
you can add this code in manifiest.xml file
action android:name="com.kaushalam.activity101activity.SecondActivity"
category android:name="android.intent.category.DEFAULT"
I got the same case too. After reading thepearson's answer, I revised my Activity and found out that I wrote
public void onCreate(Bundle s)
But in fact it should be
protected void onCreate(Bundle s)
And it works now!
This works if you have an Activity object (which you need to launch):
intent.setClassName(CallingActivity.this, activityToLaunch.getComponentName().getClassName());
Activity you're calling sholdn't contain "sheme" and contain intent-filter:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.sj.myapplication.SecondActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
so in calling code:
Intent intent=new Intent("com.example.sj.myapplication.SecondActivity");
startActivity(intent);
Try using the following:
intent.setClassName("com.x.y", "com.x.y.className");
This works for me
I also ran into ActivityNotFoundException by passing the wrong view into setContentView(), each activity's class file must correspond with the layout xml file this way.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wrongView);
}
as opposed to
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.correctView);
}
I had the same issue. I tried everything but the error, which I sorted out later, was that there was a space left between double quotes and my class name. It has to be:
intent.setClassName("com.x.y","com.x.y.className")
not
intent.setClassName("com.x.y"," com.x.y.className")
This question already has answers here:
Best way to add Activity to an Android project in Eclipse?
(8 answers)
Closed 9 years ago.
Easy one.
I've gone through a few guides and tutorials, and they're quite clear on how to start an activity (with intent).
However, how do I create a new activity in Eclipse? I can probably do this by hand, but then I have to modify the R file, which is auto-generated, and add a new xml layout.
Ok. Being a newbie myself I think the above two answers are thinking too much. He's asking very simply how to create a new activity in Eclipse.. I think this is what he wants:
A new Activity in Eclipse is actually a Class.
You would doubleclick 'src' on the left side in the Package Explorer, then highlight your 'com.' name, right click, select 'New' and then select 'Class'. Enter the Name as NewActivity and set the Superclass to android.app.Activity, then hit Finish.
When the NewActivity.java file opens up it should look like this:
package com.example.yourappname;
import android.app.Activity;
public class NewActivity extends Activity {
}
You can leave the Superclass blank and add extends Activity to the code itself if you prefer.
The final step is adding the Activity to your Manifest. So doubleclick AndroidManifest.xml to open it up and then click the 'Application' tab on the bottom. Next to the 'Application Nodes' box, click 'Add'. Highlight 'Activity' (the square box with a capital A) and click 'Ok'. Now look for the 'Attributes for Activity' box and enter a Name for the Activity and precede it by a period. In this example you'd enter '.NewActivity'.
And then you can add your onCreate() code so it looks like this:
public class NewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
//rest of the code
}
}
main_view would be your main view xml file, main_view.xml, that you would create in your layout directory.
To call the new Activity, your Intent in the code (in a different Activity) to start a new Activity looks something like this:
Intent startNewActivityOpen = new Intent(PresentActivity.this, NewActivity.class);
startActivityForResult(startNewActivityOpen, 0);
And that's it, you have the code to call the new activity and you created it. I hope this helps someone.
I know this is an old question, but I know there are still people with this same question(I did up until today)
If you add a new activity to your manifest file, there's a special link to click on to automatically create the new Activity, complete with the onCreate() method ready to be filled in.
Open the AndroidManifest.xml, and go to the 'Application' tab. Under 'Application Nodes', find and click the 'Add' button. You will likely create a new element at the top level, so select that option, highlight 'Activity', and press OK.
Once you've created the Activity, go to the 'Attributes for Activity' and fill in the name. Once you've filled in the name you want, click on the blue 'Name*' link next to the field. The new file wizard will show up, and all you have to do is press OK.
Voila! New Activity, registered in the manifest and as a ready-to-go Java class.
You create the activity by extending the activity class . Once you have creatd the activity class , You need to add the activity in the androidmanifest file specifying the properties for the activity...
A sample one would be something like this ...
<activity android:name=".JsonActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The action here indicates that it is the one that starts first ..
I dont think you need to modify the R.java file ... Once you add these in the android manifest file and save it automatically gets updated. Also the things that u added like the layouts, menus, strings, id's etcc.... in the various xml files also get automatically updated...
Correct me if i am wrong ...
I tried searching for this question on Google and haven't seen this solution yet, so I thought I'd post it here.
In Eclipse, you can click on the "New" button on the toolbar. Under Android, select Android Activity, and run through the wizard. This is the best solution by far, since it lets you set up a layout and an Activity all in one, while also updating the Manifest for you.
How to add New Activity Eclipse step by Step:
Stpe1:Double click on the androidManifest
Step2:on the Menu bar click Aplication
Step3:Scroll down to application node and CLick add button
Step 4:click select Activity and Ok
step 5:clik on the the Texte(Name* Note:make sur u clik on the texte
not into the textbox )
step6:there a new Java Class dialog
## Heading ##write the classe name
## Heading ## check checkbox construct from the super classe and and ok..
There is also the tried and tested method of starting with one of the samples and going from there.
The Hello tutorial is as good a starting point is any, just select the create from existing sample option.
The latest update to the eclipse plugin even includes a tool to rename your package should you change your mind though I haven't used it yet so can't say if it works. (Right click on the package then select Android Tools, Rename Application Package).
It is important to say that if you type the desired name for the new Activity on Name box, a dot must be put before the new name. Otherwise, the window to complete the creation of Java code will not open when you click on names link.