安卓:有没有办法解决SuperNotCalledException办法? [英] Android: Is there a way around SuperNotCalledException?

查看:180
本文介绍了安卓:有没有办法解决SuperNotCalledException办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖从活动派生的类(称为NativeActivity的),所以我可以将我自己的内容视图中创建的Java而留下它的功能,其余的机智。我必须用NativeActivity的,因为这是得到Xperia PLAY的触摸板输入的唯一途径。我需要覆盖的方法是NativeActivity.onCreate()方法,因为这是我不想要的内容视图被设置。问题是,如果我不,我重写onCreate()方法调用super.onCreate(),一个SuperNotCalledException被抛出。这是从Activity类的到来。但是,所有的Activity.onCreate()方法并设置一个布尔值:

I'm trying to override a class derived from Activity (called NativeActivity) so I can set my own content view created in Java while leaving the rest of its functionality in tact. I must use NativeActivity, because it is the only way to get touchpad input on Xperia Play. The method I need to override is the NativeActivity.onCreate() method, because that is where the content view that I don't want is being set. The problem is, if I don't call super.onCreate() in my overridden onCreate() method, a SuperNotCalledException gets thrown. This is coming from the Activity class. But all the Activity.onCreate() method does is set a boolean:

        protected void onCreate(Bundle savedInstanceState) {
            mVisibleFromClient = !mWindow
                    .getWindowStyle()
                    .getBoolean(
                            com.android.internal.R.styleable.Window_windowNoDisplay,
                            false);
            mCalled = true;
        }

我可以做我自己的code查收,使用Activity.getWindow()方法。不幸的是,mCalled布尔是私有的,所以我不能只是将其设置为true,在我自己的code。我似乎无法弄清楚如何绕过这一要求。任何想法?

I can do that check in my own code, using the Activity.getWindow() method. Unfortunately, the mCalled boolean is private, so I can't just set it to true in my own code. I can't seem to figure out how to get around this requirement. Any ideas?

推荐答案

一件事,我从研究源头code为活动想通了,就是你可以调用,在基地的活动设置mCalled为true的方法之一化酶,这是不是在子类中重写,并且什么也不做在基Activity类。所以在NativeActivty的情况下,一个可以称之为像 super.onRestart(); ,因为这个类不被覆盖NativeActivity的,并且你可以看到下面,它不'T做Activity类什么:

One thing I figured out from studying the sourcecode for Activity, is you could call one of the methods that set mCalled to true in the base Activity clase, which are not overridden in the subclass, and which do nothing in the base Activity class. So in the case of NativeActivty, one could call something like super.onRestart();, because this class is not overridden by NativeActivity, and as you can see below, it doesn't do anything in the Activity class:

protected void onRestart() {
    mCalled = true;
}

这是相当hackish的,并可能在未来某个版本被打破,但可以是一个快速的解决方案,如果你不希望有重新只是因为一个简单的布尔的活动的整个子类。

This is quite hackish, and could be broken at some future version, but can be a quick solution if you don't want to have to recreate an entire subclass of Activity just because of one simple boolean.

这篇关于安卓:有没有办法解决SuperNotCalledException办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆