I am in the final stages of a Cocos2d - Android game and I am having a problem that I can't figure out.
When I lock my tablet (Nexus 7) or when I hit the home button to leave the game, it restarts. It goes right back to the splash screen like the game is opening all over again. For your reference, here is my Main Activity and my Android Manifest:
public class MainActivity extends Activity {
//To get a static reference to context for shared preferences
private static Context context;
protected CCGLSurfaceView _glSurfaceView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//To get a static reference to context for shared preferences
MainActivity.context = getApplicationContext();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_glSurfaceView = new CCGLSurfaceView(this);
setContentView(_glSurfaceView);
}
//To get a static reference to context for shared preferences
public static Context getAppContext(){
return MainActivity.context;
}
#Override
public void onStart()
{
super.onStart();
CCDirector.sharedDirector().attachInView(_glSurfaceView);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
CCDirector.sharedDirector().setDisplayFPS(true);
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
CCScene scene = Splash.scene();
CCDirector.sharedDirector().runWithScene(scene);
}
#Override
public void onPause()
{
super.onPause();
CCDirector.sharedDirector().pause();
SoundEngine.sharedEngine().pauseSound();
}
#Override
public void onResume()
{
super.onResume();
CCDirector.sharedDirector().resume();
SoundEngine.sharedEngine().resumeSound();
}
#Override
public void onStop()
{
super.onStop();
SoundEngine.sharedEngine().pauseSound();
}
and
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bigfiresoftware.alphadefender"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/adicon"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="bigfiresoftware.alphadefender.MainActivity"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:hardwareAccelerated="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:configChanges="keyboard|keyboardHidden|orientation"
</manifest>
Any/all help is very much appreciated. Thanks.
add below line in your manifest file to your activity.
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
when you locked the screen the orientation of your screen changes.So that your activity destroys and again it creates.If you ad this line your orientation will not change.
The answers above didn't fix it but I finally found the answer. This was a tough one.
If anyone else finds this problem:
Remove the targetSdkVersion in AdnroidManifest.xml. Or change to some others, I haven't tried others but removal worked.
android:minSdkVersion="9"
android:targetSdkVersion="17" /> //git rid of this guy
add this line in your onstop() method
CCDirector.sharedDirector().end();
and you need to write an onDestroy() method to remove all the instances when you close the game.
Related
This question already has an answer here:
android: Why onDestroy not be called when i call finish?
(1 answer)
Closed 3 months ago.
I have updated the latest Android Studio and created a new Blank Project. I added the following code to the MainActivity.java file, but onDestroy() is never called. Is there any way to get destroy event?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edevshop.destroy">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
just check it out and modify your onDestroy() method as below:
#Override
protected void onDestroy() {
Log.d("TAG", "Yay.. onDestroy called!");
super.onDestroy();
}
Now... Run App > Open LOGCAT > close App (By pressing BACK button)
You'll see a LOG.
On android device 4.0.1 I am trying to build application with ActionBar but getting the NullPointException. I have tried the following solutions:
Adding Theme Theme.Holo.Light to the application theme.
In OnCreate of Activity, setting ActionBar feature before setContentView as requestWindowFeature(Window.FEATURE_ACTION_BAR).
Used SherlockActivity and called getSupportActionBar()
But no luck. In all methods, I am getting null ActionBar. Can anyone please point me out what is the issue. I am pasting Activity and AndroidManifest.xml here.
AndroidManifest.xml
<code>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tzoomers.birthdaysdiary"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light">
<activity
android:name="com.tzoomers.birthdaysdiary.BirthdaysDiary"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ContentActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SyncActivity">
</activity>
</application>
</manifest>
</code>
SyncActivity.java
<code>
public class SyncActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.layout_sync_activity);
ActionBar actionBar = getActionBar();
if(actionBar != null)
{
getActionBar().setDisplayHomeAsUpEnabled(false);
}
else
{
Toast.makeText(this, "Action bar is null", Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
}
Please help what can be exact issue instead of pointing urls. I have tried all the solutions. If I am missing something in XML or JAVA files please point that.
Thanks in advance.
I JUST solved this problem myself, with the new updates its a bit tricky and some of the old fixes don't work as well anymore. Try this:
set your MainActivity Java code to extend ActionBarActivity
use getSupportAcionBar(); call to retrieve your action bar
Be sure your (custom) TabListener extends android.support.v7.app.ActionBar.TabListener
you can use FragmentManager to add and remove tab Fragments when tabs are selected and unselected.
Here's some of my code snippets to help illustrate, hope it works for you too :)
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) //Overrode Default Constructor
{
super.onCreate(savedInstanceState);
android.support.v7.app.ActionBar tabsActionBar = getSupportActionBar();
/***following changes ActionBar to a Tabbed ActionBar***/
tabsActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);`
android.support.v7.app.ActionBar.Tab tabArray = tabsActionBar.newTab();to later contain 'inflated' digitalClock (tabbed) UI
tabArray.setText(R.string.tab_one);
/***following sets clockTabListener private class as Listener for
* this object***/
tabArray.setTabListener( new clockTabListener( this, digitalClockFragment.class.getName() ) );//actual call to digitalClockFragment
tabsActionBar.addTab(tabArray);//adds TabArray to Action Bar Tab(s)
/******Second call for setting New Tab to AnalogClockFragment******/
tabArray = tabsActionBar.newTab();
tabArray.setText(R.string.tab_two);
tabArray.setTabListener( new clockTabListener( this, analogClockFragment.class.getName() ) );//actual call to analogClockFragment
tabsActionBar.addTab(tabArray);
private class clockTabListener implements android.support.v7.app.ActionBar.TabListener
{
private final Activity currentActivity;
private final String currentFragment;
private Fragment launchFragment;
private android.app.FragmentManager frgManager;
public clockTabListener(Activity activityName, String fragmentName)
{
currentActivity = activityName;
currentFragment = fragmentName;
frgManager = getFragmentManager();
}
/******************************************************************/
/******************************************************************/
/******************************************************************/
#Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1)
{
launchFragment = Fragment.instantiate(currentActivity, currentFragment);
frgManager.beginTransaction().replace(android.R.id.content, launchFragment).commit();
}
I have no idea what it could be. I searched through my code for like 30 min
MainActivity:
public class MainActivity extends Activity {
public void onResume(){
ArrayList<String> voiceResults = getIntent().getExtras()
.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
if (voiceResults.size() >= 1) {
String infoId = "That is what you said";
Card ShowDataCard = new Card(this);
ShowDataCard.setText(voiceResults.get(0));
//ShowDataCard.setText("Testing");
ShowDataCard.setInfo(infoId);
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}
else {
String mainText = "You did not say anything.";
String infoId = "Why didn't you say anything?";
Card ShowDataCard = new Card(this);
ShowDataCard.setText(mainText);
ShowDataCard.setInfo(infoId);
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.glass.texttospeech"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.glass.texttospeach.MainActivity"
android:label="#string/app_name"
android:icon="#drawable/icon" >
<intent-filter>
<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data android:name="com.google.android.glass.VoiceTrigger"
android:resource="#xml/voice_trigger_start" />
</activity>
</application>
So what could it be? The intent filters are correct, the packages are correct, voice trigger is correct.
I am just trying to have it print what you said. I have done it before but it will not work now!
when you are overriding Activity methods, don't forget to call super methods.
here you are overriding onResume() but not calling super method. call super method like
#Override
public void onResume() {
super.onResume();
// do your work....
}
and use Override annotation when you are overriding any method for readability and check for NullPointException when you are getting extras from Intent. Intent.getExtras...() will return null if there is no mapping found for a given key
Ive been trying to make an app for my girlfriend and I which will allow us to type messages and then allow th other to read it and click the button below to give a response and keep this going back and forth. I've gotten somewhat far I guess but I'm completely stuck on why it wont work now. Ive been working based on the skeleton of this code.
Originally, the code allowed me to open the application and then once I pressed the button at the bottom, it would give me an error saying that the program is no longer able to run and that I had to "force close" it. I fixed that but then when I did, the MainActivity window would keep reappearing when I would click the button. I attempted to fix that but now the application doesn't open anymore
I tried the debugger but I'm not advanced enough to figure out what it really says....
If you would like project folder or somehow upload here, I have no problem doing that. Just trying to make this as a little present for my girl. Thanks in advance!
[edit]
I should mention that there are no syntax errors
[edit]
Manifest code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chatfriends"
android:versionCode="1"
android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" tools:ignore="OldTargetApi">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" tools:ignore="MissingPrefix"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity__alex" />
</application>
</manifest>
[edit]
MainActivity:
package com.chatfriends;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.layout.activity_main, menu);
return true;
}
protected void onStart() {
super.onStart();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onStop() {
super.onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
public void startActivity_alex(View v){
Intent intent = new Intent(MainActivity.this, Activity__alex.class);
startActivity(intent);
}
}
Edit: Now the app runs, but when I click on the button below it gives me an error.
Here is the catlog
Change
<activity
android:name=".chatfriends.MainActivity"
to
<activity
android:name=".MainActivity"
inside your manifest because you MainActivity is inside com.chatfriends package and not in com.chatfriends.chatfriends
You should define your activity name in the Manifest file as:
android:name=".MainActivity"
instead of android:name=".chatfriends.MainActivity"
Because the error stating the class com.chatfriends.chatfriends.MainActivity is not found, and you have your MainActivity class directly under com.chatfrieds package.
Edit
The error message tells you "NoSuchMethodException" onClick(View). It seems you defined onClick handler or your button in the layout XML file.
You need to add this function in your activity:
public void onClick(View view){
// Add your code here for button click
}
I m trying to start an IntentService from the main activity of y application and it won't start. I have the service in the manifest file. Here's the code:
MainActivity
public class Home extends Activity {
private LinearLayout kontejner;
IntentFilter intentFilter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
kontejner = (LinearLayout) findViewById(R.id.kontejner);
intentFilter = new IntentFilter();
startService(new Intent(getBaseContext(), HomeService.class));
}
}
Service:
public class HomeService extends IntentService {
public HomeService() {
super("HomeService");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getBaseContext(), "TEST", Toast.LENGTH_LONG).show();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salefinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Home"
android:label="#string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".HomeService" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
How can I make it work?
onHandleIntent gets called from a background thread. You can't modify the UI, or in this case, make Toast from outside the UI thread. So, I wouldn't expect anything to happen with your service.
Just try writing something out with Log.d() to see if your service is getting called.
It seams that android cached a bad version of the app - I forced closed it and started it again, and it worked...