2012-03-22 60 views
1

我的應用程序在後臺線程中執行一些網絡活動或從webservices獲取數據。在開始之前,它彈出一個進度對話框。該對話框在處理程序上被解除。這一切都正常工作,除非在對話框啓動時屏幕方向發生變化此時,我的應用程序崩潰了。即時消息使用res/layout中的一個肖像的兩個佈局文件夾和res /佈局土地。當我將屏幕方向從縱向改爲橫向或反之亦然時,我的應用會崩潰。當進程對話框和後臺線程處於活動狀態時處理屏幕方向更改?

我知道爲什麼會發生這種情況。

當我切換方向時,Android會創建一個新的視圖。我可能會崩潰,因爲\後臺線程試圖改變舊的狀態。 (它可能也有麻煩,因爲後臺線程不在UI線程上)。所以任何好友都可以告訴我如何解決這個問題,這樣我就可以繼續了。

當進度對話框和後臺線程處於活動狀態時,如何處理屏幕方向更改?

在此先感謝。

我的代碼是在這裏

public class LoginActivity extends Activity { 

EditText edUserName, edPassword; 
Button btnLogin; 
TextView txt_Alert; 
ProgressDialog dialog; 
String url, Status, result; 
ConnectToWebService cn; 

final Handler handler = new Handler() { 

    @Override 
    public void handleMessage(Message msg) { 

     super.handleMessage(msg); 

     dialog.dismiss(); 
     if (check_Status()) { 
      login(); 
     } 

    } 

}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    setTitle("Login"); 
    edUserName = (EditText) findViewById(R.id.edName); 
    edPassword = (EditText) findViewById(R.id.edPass); 
    btnLogin = (Button) findViewById(R.id.btnLogin); 
    txt_Alert = (TextView) findViewById(R.id.txt_Required); 



    btnLogin.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      String uName = edUserName.getText().toString().trim(); 
      String Pass = edPassword.getText().toString(); 

      cn = new ConnectToWebService(); 
      processThread(); 

} 


protected void processThread() { 
    dialog = ProgressDialog.show(LoginActivity.this, "", 
      "Singing in ...", false); 

    new Thread() { 

     public void run() { 


      Status = cn.callWebService(url); 
      handler.sendEmptyMessage(0); 

     } 

    }.start(); 

} 

我logcat的

03-23 00:03:48.865: E/AndroidRuntime(11799): java.lang.IllegalArgumentException: View not attached to window manager 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.view.Window$LocalWindowManager.removeView(Window.java:432) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.Dialog.dismissDialog(Dialog.java:278) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.Dialog.access$000(Dialog.java:71) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.Dialog$1.run(Dialog.java:111) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.Dialog.dismiss(Dialog.java:268) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at com.palewar.coinop.LoginActivity$1.handleMessage(LoginActivity.java:42) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.os.Looper.loop(Looper.java:130) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at android.app.ActivityThread.main(ActivityThread.java:3683) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at java.lang.reflect.Method.invokeNative(Native Method) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at java.lang.reflect.Method.invoke(Method.java:507) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619) 
03-23 00:03:48.865: E/AndroidRuntime(11799): at dalvik.system.NativeStart.main(Native Method) 

回答

0

HIII駁回,,

爲了更好地優化您必須使用:

1.如果您的情況不需要,將Activty的方向修改爲特定的人像或風景。

2.使用onOrientationChange()並仔細處理事件。當您正在使進度對話框顯示時,但是如果方向改變窗口管理器開始動作並且視圖不存在於其上執行動作(例如顯示對話框事件)

3.Simple解決方案,但不是很好是有

wihtin try 
{ 
processThread(); 
/**basically this line dialog = ProgressDialog.show(LoginActivity.this, "", 
      "Singing in to CoinOp...", false);*/ 
} 
catch (Exception e); 
2

我發現我的問題解決方案,並做了這樣的

package com.palewar; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 

public class ThreadActivity extends Activity { 


    static ProgressDialog dialog; 
    private Thread downloadThread; 
    final static Handler handler = new Handler() { 

     @Override 
     public void handleMessage(Message msg) { 

      super.handleMessage(msg); 

      dialog.dismiss(); 

     } 

    }; 

    protected void onDestroy() { 
     if (dialog != null && dialog.isShowing()) { 
      dialog.dismiss(); 
      dialog = null; 
     } 
     super.onDestroy(); 
    } 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     downloadThread = (Thread) getLastNonConfigurationInstance(); 
     if (downloadThread != null && downloadThread.isAlive()) { 
      dialog = ProgressDialog.show(ThreadActivity.this, "", 
        "Signing in...", false); 
     } 

     dialog = ProgressDialog.show(ThreadActivity.this, "", 
       "Signing in ...", false); 

     downloadThread = new MyThread(); 
     downloadThread.start(); 
     // processThread(); 
    } 

    // Save the thread 
    @Override 
    public Object onRetainNonConfigurationInstance() { 
     return downloadThread; 
    } 


    static public class MyThread extends Thread { 
     @Override 
     public void run() { 

      try { 
       // Simulate a slow network 
       try { 
        new Thread().sleep(5000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       handler.sendEmptyMessage(0); 

      } finally { 

      } 
     } 
    } 

} 

讓我知道它適用於你我們所有的不是

+0

雅,它也適用於我,原因是當我們縱向或LandScape設備顯示ProgressBar時,那麼方法onDestroy()將被調用。 – String 2013-04-04 12:29:04

相關問題