Android onResume空白屏幕

时间:2012-04-25 18:47:05

标签: android onresume forceclose

在我的Android应用程序中,一切都很完美,除非我按下主页按钮并等待10-15分钟然后返回应用程序,屏幕显示为空白。我仍然可以访问菜单项,但屏幕只是黑色。

如果我正常进行活动,请选择主页按钮,然后返回应用程序就可以了......只有在它已经坐了一段时间之后......

有什么想法吗?

当应用程序恢复时,我会在整个过程中放入Toast消息,并且它会一直执行到上次执行代码的过程。所以我不知道发生了什么。

此外,当屏幕为空白时,按后退或主页会导致强制关闭

编辑:单击主页按钮时添加例外:

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;

公共类LoadingScreen扩展了Activity {     protected Location currentLocation;     私有ApplicationClass应用程序;     私人ImageButton btnLocal;     私人ImageButton btnNational;     private ImageButton btnDOD;     private ImageButton btnDestinations;     私人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;
}

}

2 个答案:

答案 0 :(得分:0)

当您的应用程序进入后台时,可能会在设备内存不足时将其终止。也许这是你问题的一部分。

您可以使用savedInstanceState来保存应用程序状态。

答案 1 :(得分:0)

根据我的经验,当我尝试在on resume方法中添加一些耗时的同步任务(该任务在活动的主线程上而不是在后台线程中运行)时,屏幕变黑。 如果您的onResume中的代码或过程相对较长,则应将其包装在异步类(如AysncTask或AsyncTask加载器)中。例如,将简单的AsyncTask定义为onResume中的匿名内部类。

@Override
protected void onResume() {

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

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

  }.execute();

}