3

我正在调试一个 android 应用程序(不幸的是,它是由其他人编写的)。该应用程序有一个持续 1 秒的启动活动,然后使用意图转换到登录页面活动。该应用程序运行良好,直到我注意到未在清单文件中设置 targetSDKVersion。我将它设置为 18。然后当我在模拟器中运行应用程序时,应用程序崩溃,我在 logcat 中看到以下错误:

10-24 06:14:26.840: E/AndroidRuntime(2457): FATAL EXCEPTION: main
10-24 06:14:26.840: E/AndroidRuntime(2457): java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.os.Parcel.readException(Parcel.java:1435)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.os.Parcel.readException(Parcel.java:1385)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:835)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:5034)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1399)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.Choreographer.doFrame(Choreographer.java:532)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.os.Handler.handleCallback(Handler.java:730)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.os.Looper.loop(Looper.java:137)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at java.lang.reflect.Method.invoke(Method.java:525)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 06:14:26.840: E/AndroidRuntime(2457):     at dalvik.system.NativeStart.main(Native Method)

应用程序在显示初始屏幕后和登录页面活动加载之前立即崩溃。以下块显示来自启动活动的相关代码块。

飞溅活动

private Thread mSplashThread;    
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    final SplashScreenActivity sPlashScreen = this;   
        mSplashThread =  new Thread(){

@Override
        public void run(){
        try {
            synchronized(this){
                // Wait given period of time or exit on touch
                wait(1000);
            }
        }
        catch(InterruptedException ex){                    
        }

        finish();

        // Run next activity
        Intent intent = new Intent();
        intent.setClass(sPlashScreen, LandingPageActivity.class);
        startActivity(intent);                 
        }
    };
    mSplashThread.start();        
    }

@Override
public boolean onTouchEvent(MotionEvent evt)
{
    if(evt.getAction() == MotionEvent.ACTION_DOWN)
    {
    try{
        synchronized(mSplashThread){
            mSplashThread.notifyAll();
            }
    }
    catch (IllegalArgumentException e){
        Log.v("This is it", e.getCause().getMessage());
    }
}
return true;
}

现在,在我调试此问题的所有研究中,我发现了检查以下代码段的建议,但我的应用程序中没有这样的内容:

@Override
    public void onAttachedToWindow()
    {  
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();  
    }
4

3 回答 3

4

问题已解决。在其中一个空基类中确实出现了“onAttachedToWindow”。删除该覆盖后它起作用了。正如我在原始帖子中提到的,我在另一篇帖子中遇到了这个解决方案。这是对该帖子的参考。

然而,就我而言,此代码不在任何面向用户的活动中。它在我的登录页面继承的空白父活动中。希望有人在这篇文章后保存几个小时。

于 2013-10-25T11:14:53.563 回答
3

我有一个解决方案给你。它不是很有用,但可以解决您的问题。您应该将您的 minsdk 版本更改为 8。

<uses-sdk android:minSdkVersion="8" />

让我们试试,然后告诉我结果;)

于 2014-06-26T03:56:57.820 回答
0

如果你只使用延迟试试这个;

public class MainActivity extends Activity implements OnClickListener{


    private Thread mSplashThread;    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        View root = getWindow().getDecorView().findViewById(android.R.id.content);
        root.setOnClickListener(this);
        final SplashScreenActivity sPlashScreen = this;   
        mSplashThread =  new Thread(){

            @Override
            public void run(){
                try {
                    Thread.currentThread().sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finish();

                // Run next activity
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, LandingPageActivity.class);
                startActivity(intent);                 
            }
        };

    }

    @Override
    public void onClick(View v) {
        mSplashThread.start();
    }

}
于 2013-10-24T14:14:25.127 回答