扩展库活动不会调用onCreate

时间:2016-12-26 09:31:43

标签: java android inheritance

我添加了一个项目' A'作为我项目中的图书馆' B'。现在在项目' B'中,我有一项活动' MainActivity'它扩展了一项活动' SplashActivity'项目A。

当我运行该应用时,永远不会调用' MainActivity'(项目B)的onCreate,而是调用' SplashActivity'(项目A)的OnCreate每次。可能是什么问题?

如果我想调用' SplashActivity'(项目A)的onCreate方法,然后调用' MainActivity'(项目B)的onCreate,那该怎么办?

代码如下:

    private BitmapSource LoadImage(string path)
    {
        lock (_syncRoot) //lock the object so it doesn't get executed more than once at a time.
        {
            BitmapDecoder decoder = null;

            try
            {
                //If the image is not found in the folder, then show the image not found.
                if (!File.Exists(path) && (path != null))
                {
                    using (var stream = new System.IO.MemoryStream())
                    {
                        if (!File.Exists(Path.GetTempPath() + "ImageNotFound.jpg"))
                        {
                            System.Drawing.Bitmap ss = Ashley.ProductData.MarketSeries.Presentation.Properties.Resources.ImageNotFound;

                            using (FileStream file = new FileStream(Path.GetTempPath() + "ImageNotFound.jpg", FileMode.Create, FileAccess.Write))
                            {
                                ss.Save(stream, ImageFormat.Jpeg);
                                stream.Position = 0;
                                stream.WriteTo(file);
                            }
                        } 
                    }

                    path = Path.Combine(Path.GetTempPath(), "ImageNotFound.jpg");
                    NoImage = false;
                }
                else
                {
                    if (!EnableForEdit)
                        NoImage = false;
                    else
                        NoImage = true;
                }

                if (!string.IsNullOrEmpty(path) && (!NoImage || File.Exists(path)))
                {
                    using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                        return decoder.Frames.FirstOrDefault();
                    }

                }
                else
                    return null;
            }
            catch (OutOfMemoryException ex)
            {
                MessageBox.Show("Insufficient memory to handle the process. Please try again later.", "Application alert");                    

                return null;
            }
            catch (Exception ex)
            {
                // Error handling.
                 ShowMessages.AlertBox(ex.Message, MethodInfo.GetCurrentMethod().Name);
                throw ex;
            }
            finally
            {
                decoder = null;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

更改您的xml参考

setContentView(R.layout.activity_splash_activity_temp);
new SplashActivity().setLogo();

setContentView(R.layout.activity_main); 
SplashActivity splashactivity = new SplashActivity();
splashactivity.setLogo();

答案 1 :(得分:0)

我解决了自己的答案,感谢@jimit patel和@ρяσѕρєяK,我解决了它,如果有人在寻找答案的话 代码:

@Override
protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.activity_splash_activity_temp);
    ImageView imageView = (ImageView) findViewById(R.id.splash_logo);
    super.setLogo(imageView,this);
    super.onCreate(savedInstanceState);

}

只需在代码末尾调用super.onCreate,代码就会按预期运行。 :)