I am a noob learning Android and making my own little demo project at the same time.
Basically, when the user starts my app it shows levels.java with 8 buttons, when the user clicks the 8th button this fires:
public void button_clicked8(View v) {
text1.setText("clicked 8");
startActivity(new Intent(this, GameScreen.class));
}
which starts my main gamescreen class where the simple game is played.
If the user gets the math problem wrong 3 times, the game is over and I fire this code:
r_settings.setGameStarted(false);
r_settings.setGameOver(0);
r_settings.setInternalLevel(0);
r_settings.setDisplayLevel(0);
this.finish();
which basically resets some static variables and sends the user back to the levels.java screen.
So far all of the above works like a charm, in the levels screen if the user presses the 8 button again the game starts again, the problem is this works for around 4 times, on the 4th or 5th time it goes to the gamescreen but nothing works... and then in logcat I get this error:
http://imageshack.us/photo/my-images/546/68081548.png/
http://imageshack.us/photo/my-images/59/93717315.png/
(you need to click the image to see it full size)
Where am I going wrong?
Thanks!
R
Well, you get a NullPointerException at GameScreen.java:64. Does it work after you fix that?
Related
I have a strange problem with my app. On my HTC One X max apps in recent apps is 9 and if my app gets pushed out of these 9 apps when I start it again it acts weird and breaks in all kinds of ways.
In my BaseActivity OnRestart I have code that switches current activity to main activity. This doesn't happen in above scenario.
protected override void OnRestart()
{
base.OnRestart();
if(someTimeHasPassed)
{
var intent = new Intent(this, typeof(MainActivity));
// Kills every open activity to disable user from going back.
finishAllOpenActivities();
StartActivity(intent);
}
}
I'm not sure what part of code to post. Anyone had any similar issues?
EDIT: After the above scenario happens
Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
in OnCreate() never executes because when I press home button I can see my app normally, no black screen. And by normally I can see the background and default header with activity name, nothing else gets loaded.
Not sure why OnCreate is even being called because I'm returning to that screen.
EDIT 2: After more examination, this seems to happen when some apps are in front of mine in recent apps. Could this be a memory issue?
I have a simple html/javascript app that starts and works up fine on an Android device. However, when the app is put in background by using the 'home button' and then brought back to the foreground by clicking on the app again - it starts with a blank screen (white blank screen on android ) and stays that way until the 'back button' is pressed.
Once the 'back button' is pressed the screen refreshes to the last page displayed by the app.
I am new to PhoneGap and I am sure there is something simple/fundamental that I am missing - in terms of how to handle the 'resume' event/etc.. I have followed the instructions provided on this link by Phil Mitchell http://wiki.phonegap.com/w/page/35501397/Tutorials (which is a great resource btw..)
Thanks.
Update :
After looking at the DroidGap code, I have tried to add the following line :-
super.setBooleanProperty("keepRunning", false);
But this does not seem to help. I am happy for the app to exit every time the home button is entered and then do a full restart when the app is clicked on the mobile.
Any help is much appreciated..
I was having the same problem and I want to share a hack that worked for me.
My app is based on ionic/angular/cordova and the android version was giving me a white screen around 50% the times on pause/resume.
When it happened, tapping anywhere on the screen or hitting the back button would make the screen to render again just fine.
I added a div tied to a scope variable named random, like this:
<body ng-app="starter" id="body">
<div>{{ random }}</div>
<ion-nav-view>
</ion-nav-view>
</body>
Then I added a listener inside my app.js to capture the resume event fired by cordova, to then change the random variable and call $apply(), like this:
document.addEventListener("resume", function() {
angular.element(document.querySelector('#body')).scope().random = Math.random();
angular.element(document.querySelector('#body')).scope().$apply();
})
Since the ion-nav-view takes over the whole screen that div never shows up, but angular doesn't know that and refreshes the page, causing the whole thing to work.
(I tried to tie the variable to an invisible element but then angular doesn't refresh the page)
I hope this helps someone!
There seem to be issues around rendering the pages thru PhoneGap and 'Dialog'ing thru Client side FB authentication flow using the JS SDK. If I disable the client side authentication, then the rendering and display of the page works very well, when the app is brought to foreground from background (pause/resume/etc).
I am assuming that the page will need to sense that it is being displayed over a mobile device and use the IOS or Androi SDK for client side authentication.. (if/when I get around to testing that, I update the answer - if there is anybody who has any more light to shed, please feel free to embellish).
If you still say "I am happy for the app to exit every time the home button is entered and then do a full restart when the app is clicked on the mobile." then go for it.
#Override
protected void onStop(){
super.onStop();
}
Try this from where you create activity or from the class which "extends DroidGap".
The Kindle Fire has a bar at the bottom that includes a "home" button and a "back" button.
When I click the "back" button, my app's "onPause()" event is invoked.
When I click the "home" button, my app crashes. An Android dialog box is displayed. It says my app has stopped unexpectedly. I am presented with a "force close" button.
Sooooo what event do I need to be handling to prevent this. This only happens on my app, not the ones I downloaded, so yeah, it's me, lol.
Edit
As per this web page, I added events and toasts to the app just to gain some insight into how things work. When I click the Back button, I see the toasts produced by the onPause(), onStop(), and onDestroy() methods. When I click the Home button, there are no toasts, just the crash.
RESOLUTION
Akhil suggested I look at the logcat. I don't run the emulator since my machine seems understrength for Android development (or perhaps I expect too much from the emulator); it takes forever to start it up. Anyway, after figuring out how to run the emulator (and look at the logcat for the first time, ha) I saw that I was throwing an exception related to serialization. I'm off to solve it now. Thanks Akhil for the kick in the right direction!
Ah, and the emulator did display the onPause() toast when I clicked Home, so reality is still functioning as expected.
FINAL
The error was related to the serialization I added to make the onSaveInstanceState(Bundle savedInstanceState) method work. Basically, my app (and old game I converted to android) wasn't serializable, therefore this code in onSavedInstanceState() would not compile:
savedInstanceState.putSerializable(GAME, game);
'game' is innocuous, so I added "implements Serializable" to Game's class definition. However, I neglected to add the same to a private class within Game. This is what was causing the exception.
When you click the "back" button, your app's "onPause(), onStop(),onDestroy()" events will be invoked.When you click the "home" button, your app's "onPause(), onStop()," events will be invoked. ( This is a general scenario, assuming)
Put log statements in your onPause(), onStop() and see where you are getting the error.
Home Button cannot be intercepted in android due to security flaws.One option available is to overrride the onStop which is called when you press the home button and the app closes:-
#Override
protected void onStop()
{
super.onStop();
//do whatever you want here
}
I have an app that has a few Spinners and a couple of Buttons that pop up Dialogs. Other Buttons start new Activities.
On startup, these all work fine. However, when a new Activity is started and then we return, all pop-up elements stop working properly - the screen dims, but no dialog appears. In this state:
if you tap where the dialog's buttons should be (such as an "OK" button), their callbacks are called and everything works fine
rotating the phone will cause a redraw and the dialog will be visible again
This seems to me to be an Android bug. There seem to be very few references to it anywhere, and I'm at a loss as to what triggers it, and how I can work around it. As requested by #ethan, code fragment for one possible path is below, but there's little to it; I don't need to return any results, and the problem is exhibited when the user simply presses the Back button (which just has the default binding).
private OnClickListener button_click = new OnClickListener()
{
public void onClick(View v) {
int lId = v.getId();
...
if ( lId == R.id.cancel_button ) {
finish();
}
...
}
}
This is probably not helpful - I'm hoping someone will recognize the symptom. In the meantime, I'm trying to work up a mock example that exhibits the problem.
It does not occur in 1.5 or 1.6, but does in 2.2.
I am using Tabbar in my application.
I started nearly 3 to 4 activities using tabagroupActivity in each tab.
Here everything working fine,
But whenever pressing back button,its closing with error message "Stopped Unexpectedly"
How to write code for Back button functionality to move back to the previous activity in case of tabbars.
Please provide any suggestion or example to handle this issue.
Thank you ....please