0

In my Android App, everything works perfect except when I hit the home button and wait 10-15 minutes and come back to the app, the screen is blank. I can still access the menu items, but the screen is just black.

If I am on the activity as normal, select the home button then come back to the app it is fine...its only after it has sat for a while...

Any ideas?

I put Toast messages along the way through when the app resumes and it makes it all the way to the last execution of code. So I dont know what is happening.

Also, when the screen is blank, pressing back or home causes a Force Close

EDIT: Adding exception when clicking home button:

04-25 15:12:09.625: E/AndroidRuntime(6351): java.lang.RuntimeException: Unable to pause activity {com.MyApp/com.MyAppMain.MainScreen}: java.lang.NullPointerException
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2731)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2678)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2651)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.ActivityThread.access$1700(ActivityThread.java:132)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1045)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.os.Looper.loop(Looper.java:150)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.ActivityThread.main(ActivityThread.java:4263)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at java.lang.reflect.Method.invoke(Method.java:507)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at dalvik.system.NativeStart.main(Native Method)
04-25 15:12:09.625: E/AndroidRuntime(6351): Caused by: java.lang.NullPointerException
04-25 15:12:09.625: E/AndroidRuntime(6351):     at com.MyAppMain.MainScreen.onPause(MainScreen.java:110)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.Activity.performPause(Activity.java:3935)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1313)
04-25 15:12:09.625: E/AndroidRuntime(6351):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2708)
04-25 15:12:09.625: E/AndroidRuntime(6351):     ... 12 more




import localytics.localytics.android.LocalyticsSession;

import android.app.Activity; import android.content.Intent; import android.location.Location; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; import com.myapp.R; import com.WhiteLabel.tools.Globals; import com.WhiteLabel.tools.LocationService.LocationResult; import com.WhiteLabel.tools.PreferenceHelper;

public class LoadingScreen extends Activity { protected Location currentLocation; private ApplicationClass application; private ImageButton btnLocal; private ImageButton btnNational; private ImageButton btnDOD; private ImageButton btnDestinations; private LocalyticsSession localyticsSession;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    try
    {           
        //final Activity activity = this;
        application = ApplicationClass.getInstance();       
        setTheme(application.appTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_screen);

        application.addressSaved = false;

        InitializeControls();

      //  String notifytime = PreferenceHelper.GetNotificationTime(this);

        InitializeLocalytics(true);

        Toast.makeText(this, "Use the menu button to access your account.", Toast.LENGTH_LONG).show();
    }
    catch(Exception ex)
    {           
        String s = ex.toString(); 
    }
}

@Override 
protected void onResume() 
{     
    try
    {       
        super.onResume(); 

        //if(!Globals.isRelease)
            //Toast.makeText(this, "Hit onresume on loading screen.", Toast.LENGTH_LONG).show();

        if(this.localyticsSession!=null)
            this.localyticsSession.open();

        Intent extrasIntent = getIntent();

        if(extrasIntent!=null)
        {           
            Bundle extras = extrasIntent.getExtras();

            if(extras!=null)
            {                               
                String message = extras.getString("load_directive");

                if(message.equalsIgnoreCase("notification"))
                {
                    getIntent().putExtra("load_directive", "");

                    String s = message; 
                    GetDDClick();
                }
            }
        }

        if(!Globals.isRelease)
            Toast.makeText(this, "Hit after extras intent.", Toast.LENGTH_LONG).show();

        //TODO - check for null application obj
        if(application.loadFailed)
        {
            application.loadFailed = false;
            Toast.makeText(getBaseContext(), "Connection issue.  Please verify you are connected to the internet!", Toast.LENGTH_LONG).show();              
        }

        if(!Globals.isRelease)
            Toast.makeText(this, "Hit end of onresume.", Toast.LENGTH_LONG).show();

    }
    catch(Exception ex)
    {
        if(!Globals.isRelease)
            Toast.makeText(this, "onResume error: "+ex.toString(), Toast.LENGTH_LONG).show();
    }
}

public void onPause() 
{  
    try
    {       
        this.localyticsSession.close();     
        this.localyticsSession.upload();     
    }
    catch(Exception ex)
    {
        if(!Globals.isRelease)
            Toast.makeText(this, "onPause error:"+ex.toString(), Toast.LENGTH_LONG).show();
    }       

    super.onPause(); 
} 

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) 
{ 
    super.onSaveInstanceState(savedInstanceState); 
    // your stuff or nothing 
} 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) 
{ 
    super.onRestoreInstanceState(savedInstanceState); 
    // your stuff or nothing 
} 

private void InitializeControls()
{
    btnLocal = (ImageButton)findViewById(R.id.btnLocal);
    btnNational = (ImageButton)findViewById(R.id.btnNational);
    btnDOD = (ImageButton)findViewById(R.id.btnDOD);
    btnDestinations = (ImageButton)findViewById(R.id.btnDestinations);

    btnLocal.setOnClickListener(new View.OnClickListener() {  public void onClick(View v) { GetLocalClick();    }});
    btnNational.setOnClickListener(new View.OnClickListener() {  public void onClick(View v) {  GetNationalClick();   }});
    btnDOD.setOnClickListener(new View.OnClickListener() {  public void onClick(View v) {  GetDDClick();   }});
    btnDestinations.setOnClickListener(new View.OnClickListener() {  public void onClick(View v) {  GetDestinationsClick();   }});
}

private void InitializeLocalytics(Boolean instantiate)
{
    if(instantiate)
        this.localyticsSession = new LocalyticsSession(this.getApplicationContext(),Globals.LocalyticsAppKey);         

    this.localyticsSession.open();        // open the session  
    this.localyticsSession.tagScreen("Main Menu");
    this.localyticsSession.upload();      // upload any data 
}

private void GetLocalClick()
{
    application.loadingButtonPressed = "local";
    // set application var to local
    GoToDDView();
}

private void GetNationalClick()
{
    application.loadingButtonPressed = "national";
    application.currentDivision = "national";
    // set application var to national
    GoToDDView();
}

private void GetDDClick()
{
    application.loadingButtonPressed = "daily";
    GoToSelectedView();
}

private void GetDestinationsClick()
{
    application.loadingButtonPressed = "travel";
    GoToDDView();
}

private void GoToDDView()
{
    Intent getResultsWindow = new Intent(LoadingScreen.this, Daily.class);
    startActivity(getResultsWindow);  
}

private void GoToSelectedView()
{
    Intent getResultsWindow = new Intent(LoadingScreen.this, DailySelected.class);
    startActivity(getResultsWindow);  
}

private void GoToHomeServices()
{
    Intent getResultsWindow = new Intent(LoadingScreen.this, MainMenu.class);
    startActivity(getResultsWindow); 
}

private void GoToMyAccount()
{
    Intent configIntent = new Intent(this,MyAccount.class);
    startActivity(configIntent);
}

public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.loadingscreenmenu, menu);
    return true; 
}

public boolean onOptionsItemSelected (MenuItem item)
{
    switch (item.getItemId()) 
    {                   
        case R.id.PROFILE:
            GoToMyAccount();
            return true;
        case R.id.MYVOUCHERS:
            Intent loadVouchersIntent = new Intent(this, Vouchers.class);
            startActivity(loadVouchersIntent);
            return true;
    }

    return false;
}

}

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Jesse
  • 2,674
  • 6
  • 30
  • 47

2 Answers2

1

When your application goes to the background, it might be killed when the device is running out of memory. Maybe that's part of your problem.

You could use savedInstanceState to save the application state.

timoschloesser
  • 2,790
  • 4
  • 25
  • 33
  • This may have worked, no crash yet...but still testing to make sure and i have to wait for it so it sucks, takes a while – Jesse Apr 25 '12 at 20:30
  • Okay I added the entire class. – Jesse Apr 26 '12 at 13:37
  • It almost seems like the app is reaching the onStop state and app process is being killed, but instead of starting with onCreate, it tries to start with onResume. – Jesse Apr 26 '12 at 13:43
  • So you have a nullpointer exception in your onPause. I guess your Globals class is the reason. Am I right that this class is extending Application? If so you should consider that all the variables are re-initialized when your app is recreated after android killed it. – timoschloesser Apr 26 '12 at 17:26
  • Can you post line 110 of your MainActivity (where the error is pointing to)? – timoschloesser Apr 26 '12 at 17:27
  • Globals is a class that holds nothing but static vars. It is never initialized nor does it need to be. Line 110 is: this.localyticsSession.close(); But this only fails when exiting the app after it has already black screened... – Jesse Apr 26 '12 at 20:55
0

From my experience, the screen goes black when I try to add some time consuming synchronous task in the on resume method ( that is a task running on the main thread of activity and not in a background thread). If your code or process is relatively long in your onResume, you should wrap it inside an asynchronous class like AysncTask or AsyncTask loader. For example with simple AsyncTask defined as an anonymous inner class in your onResume.

@Override
protected void onResume() {

  new AysncTask<Void,Void,Void>() {

     @Override
     protected String doInBackground(Void... voids) {
         yourTask();
         return;        
     }

  }.execute();

}
Njuacha Hubert
  • 388
  • 3
  • 14