Development Environment: Eclipse 3.7.0
Developing: Android 3.2 application for Market Place
Using: aChartEngine 0.7.0
I'm new to the development scene but have done a bit of coding in the past various languages, I've created the ZopaStats(on Marketplace) app, but I'm now trying to convert a text based stats page to be displayed in a bar graph using achartengine.
I can get the graph to display from an activity via another activity i.e.:
Intent achartIntent = new TemperatureChart().execute(this);
startActivity(achartIntent);
but this gives me an additional activity screen i.e.:
Main Screen -> 1st Activity (Original Text Stats View) -> 2nd Activity (Graph)
Therefore, with I hit back on the graph screen, I get the blank 1st activity screen.
I hope I'm making sense here.
So what I tried to do was launch the activity from the Main Screen (i.e. my Main class) e.g.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent achartIntent = new TemperatureChart().execute(this);
startActivity(achartIntent);
}
});
But eclipse gives me the following error in the code:
The method execute(Context) in the type TemperatureChart is not applicable for the arguments new (View.OnClickListener(){}}
I've tried letting Eclipse change the method but this then causes other problems, so I think what I'm really looking for (in a round about way) is to find out what the difference is when I can try to start the activity from another Activity class rather than starting it from the main class.
I apologise for the misuse of terms etc, as I say I'm new. I've been looking at this for a few days now but the Intent and Activity documentation doesn't help me much so I just need a few pointers.
Thanks,
In your example, the this reference that you're passing to execute() is your annonymous inner subclass of OnClickListener. This is not a context object, which is what eclipse is complaining about.
Rather, you want to pass in the activity instance. Assuming the code snippet you posted lives in a class named MyExampleActivity, then you can use MyExampleActivity.this from inside the inner class to access the instance of the containing class. You should be able to pass that to TemperatureChart.execute()
There is no difference in starting an activity from the main activity or from any other activity. You just create an intent, and call startActivity on it.
For completeness, the new code is this:
N.B. My original class is called ZopaStats.class
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent achartIntent = new MarketZopaGraph().execute(ZopaStats.this);
startActivity(achartIntent);
}
});
This works great, only a single Activity windows, once again many thanks for the quick response, in record time ;)
when we use achartengine to draw graph in includes it own activity...i.e.org.achartengine.GraphicalActivity..
when we press back it shows own activity which is used to show graph..to hide these activity call finish() method on onPause() method.
Related
MORE INFO
I have doubts about the Android Structure of an App I´m trying to develop, I´m not expert, just an amateur and curious guy.
I GOT CLEAR ANSWERS FROM YOU GUYS AND I´M SO THANKFUL FOR IT. I HAVE BEEN DOING IT RIGHT, JUST NEED SOMEONE WHO TELL ME SO.
structure -
MainActivity - ButtonA - ButtonB - ButtonC - ButtonD - ButtonE - Button F
ActivityA - ButtonA1 - ButtonA2 - ButtonA3 - ButtonA4
ActivityB - ButtonB1 - ButtonB2 - ButtonB3 - ButtonB4
ActivityC - ButtonC1 - ButtonC2 - ButtonC3
Settings - Name - Date - Numbers -
Layouts: GOT IT RIGHT
Function: READY TO GO
THIS IS WHAT I´M TRYING TO DO
-Each button of each activity (above) start a short DrawableAnimation of 30 images tops.
-Each DrawableAnimation repeat itself continuously.
QUESTIONS
The App start without problems, each button start a new DrawableAnimation without problem, but The App don´t clean the memory used after finish one DrawableAnimation, so sometimes I get the Out of Memory Error, How can I fix this? Is there a way to clean memory after one DrawableAnimation and before a new one starts?
When a DrawableAnimation Start on an Activity different that the MainActivity (second, third, etc) return to the MainActivity or quit because the Out of Memory Error. What is happening here?
The App have to record how many times a Button is click, how can I do this?
CODE TO START EACH DRAWABLEANIMATION
private void addListenerOnButton() {
view = (ImageView) findViewById(R.id.image);
button = (Button) findViewById(R.id.startanimation);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
frameAnimation.stop(); frameAnimation = null;
view.setBackgroundResource(R.drawable.animation01);
frameAnimation = (AnimationDrawable) view.getBackground();
frameAnimation.start();
I try to stop or clean memory using frameAnimation.stop(); frameAnimation = null; but it does´t work.
1.Yes, MainActivity is the default activity, but you can change your "launcher" activity in your AndroidManifest file. The line below defines the launcher activity
<category android:name="android.intent.category.LAUNCHER" />
However, it should not be "MainActivity.xml", but "activity_main.xml", because Android's convention is sensitive about letter cases. The java file is "MainActivity.java". If you want a new screen, you should create a new activity(e.g. SecondActivity.java) with its layout(e.g. activity_second.xml). There are also alternative ways to change the screen, for example hiding and showing new views in single layout file but they are more advanced.
2.You can start a new activity from the current activity. For example, if you want to start SecondActivity from MainActivity, first you should declare the intent;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Then start this;
startActivity(intent);
You can locate this for example in an setOnClickListener method.
3.Not so sure what you wanted in this one.
If you want to make things in a right way, you should:
1) Learn from the free, avaialble, easy to find sources (in your case it's https://developer.android.com/develop/index.html).
2) Gain some experience, by writing code, writing code and writing more code.
3) If you will meet any difficulties while writing code (unexpected output, exceptions and e.t.c.) you should ask the question here while showing your code.
Good luck.
P.S.: You will not find a private teacher on stackoverflow.
Another rank amateur here ... If you're working in a development environment such as Eclipse, you can "make a new activity" and the layout will be at least started for you, but yes, each new screen will need its own xml file for the layout.
To go to another activity, you will need to use Intent per http://developer.android.com/training/basics/firstapp/starting-activity.html and each Activity will need its own Java file.
Your third bullet point doesn't seem to be a question, so I won't try to answer it.
Good luck!
Im working on a little game and having some issues.
There is the Menu
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()){
case R.id.bStartGame:
Intent a = new Intent(Menu.this, Action.class);
startActivityForResult(a, 1);
break; }
then the activity which starts a surfaceview
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
}
and then the the surfaceView with the game mechanics.
Most of my code is in this view.
Now I have the problem to find a good solution for the gameoverscreen.
If I start a new activity inside the surfaceview, it works - but i dont get the result() which is the score achieved during a session.
So now I wanted to ask you guys how to solve this issue.
I thought of a way, but dont know how to implement it.
It would be to pass the highscore from the surfaceview to the activity and set it as a result(which the menu activity gets back) there.
And start an xml file via dialog, which would be the gameoverscreen and as soon as the player touches the back button he gets back to the menu where he can see his achieved score.
Can you please tell me how to code this?
Kind regards
Denis
There's a number of ways to solve this:
-use startActivityForResult and then send it back from your new activity, catching it in the old activity using onActivityResult (check https://developer.android.com/reference/android/app/Activity.html)
-do what i did (the lazy, hacky way :): start the new activity with startActivity() and add the highscore as extra data added to the intent. In your new activity, use getIntent().getInt (ow whatever) to get the sent score data and do with it what you will. Then close that activity and you'll return to the previous one holding your surfaceview.
NOW THE TRICK: before you start your new activity with it's score added to the intent, just run the same score calculation in your surfaceview's activity as you would in your new activity! That way, when you return to your surfaceview's activity, you will still have the correct, new score (if stored/onresume'd correctly; don't forget to add it to your save/restore state and/oror the surfaceview's private variables)!
The only downside is that you'll have two location you have to update your scoring mechanics at. And it's not good programming. But it works and it's easy.
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
}
});
I have a log in page that pulls information from a data base, I then want to use this some of this information to populate different textviews on a new page/activity. I can get a textview to change on the activity where I have my submit button, but when I try to change the textview on my second activity, it just crashed (The application has stopped unexpectedly).
Here's my code for changing the textview (where txtID is my textview on a separate activity)
TextView test2 = (TextView) findViewById(R.id.txtID);
test2.setText(test);
my xml for seperate activity
<TextView android:text="TextView" android:id="#+id/txtID"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
Oh, I'm using a tableview for my login page, then tabs for my the rest of my pages. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)
You don't want to directly touch the UI elements of another Activity. You can make use of bundles to pass information back and forth. Here is an example:
Say we have Activity A, and it has some information as a String it wants to pass to become the text of a TextView in Activity B.
//Setup our test data
String test = "Some text";
//Setup the bundle that will be passed
Bundle b = new Bundle();
b.putString("Some Key", test);
//Setup the Intent that will start the next Activity
Intent nextActivity = new Intent(this, ActivityB.class);
//Assumes this references this instance of Activity A
nextActivity.putExtras(b);
this.startActivity(nextActivity);
So now in the onCreate method for Activity B, we can get that String and assign it as the text to the TextView like you have
public void onCreate(Bundled savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //Setup some layout, set to your own
String test = getIntent().getExtras().getString("Some Key");
TextView test2 = (TextView) findViewById(R.id.txtID);
test2.setText(test);
}
I'd probably let each separate Activity take care of its own display, and not try to have Activity 1 directly update the display of Activity 2, which is kind of what it looks like you were doing.
The Notepad Tutorial demonstrates an application with two Activities, where one Activity calls another, passing in data. (Take a look at onListItemClick in Notepadv3.) You could maybe follow this model to pass data from Activity 1 to Activity 2, where Activity 2 then takes care of its proper display, using the data it received.
If you're still having problems (like your application crashing), then please post the complete minimal code necessary to replicate your problem. Note the Notepad Tutorial and the Hello, World Tutorial include steps for debugging, which might help you isolate the exact problem.
Andy... If you try to directly touch a UI widget in another activity, your app will crash. Been there, done that accidentally. Instead, consider passing an immutable stateful object between the activities. This can be done using startActivityForResult for instance. I have some sample code here.
I am calling an activity from within itself - basically i've a list of new storys and two filter buttons that when clicked restart the activity with an intent passed that changes the news stories.
When i run the app it works, but for a second i get the old activity UI while the app reads from the new xml feed and then the UI updates. Is there any way to stop this from happening and get the activity to restart cold.
here's the code I am currently attaching to the onclicklistener
public void openFootballNews(View v) {
Intent i = new Intent(this, News_Landing.class); // News_landing class is the class this code is in
Bundle bundle = new Bundle();
bundle.putString("code", "football"); // this, if set, changes the xml feed to read
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.onCreate(null); //this has halved the time the old UI is on the screen for but I cant get rid of it completely
startActivity(i);
}
any help would be great, thanks!
Starting an activity from itself doesn't make much sense (unless your aim is to do something esoterically recursive ;) ). Also, I may be mistaken, but I believe activities are kept in a stack so that as you flip between news stories, you're piling up one nearly-identical activity after another. I'd similarly think calling onCreate() by hand is bad form.
Would need to see all of your code, but my guess is that you are reading your feed and creating your list inside onCreate(), and that your best bet is to refactor that into a openNews(String sport) method, which you call once in onCreate() and again in your listener(s).