简体   繁体   English

重新启动应用程序后,活动第二次findViewById()调用返回null

[英]Activity second findViewById() call returns null after app restart

I have checked all the scenarios on StackOverFlow, but with no use. 我已经检查了StackOverFlow上的所有方案,但是没有用。

Simply, I have implemented this auto-restart after crash tutorial, it works perfectly. 简而言之,我已经实现了崩溃后自动重启的教程,它运行完美。

I have different activities in my app, and if the app crashes and restarts, all of them restart normally. 我的应用程序中有不同的活动,如果应用程序崩溃并重新启动,则所有这些活动都将正常重启。 except one. 除了一个。

Here's the (onCreate) in ConceptActivity.java 这是ConceptActivity.java中的(onCreate)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_concept);

    txtOurStory = (TextView) findViewById(R.id.txtOurStory);
    txtOurVision = (TextView) findViewById(R.id.txtOurVision);

    MyConfig config = Web.getConfig(); //retrieve data from server

    txtOurStory.setText(config.ConceptStory);
    txtOurVision.setText(config.ConceptVision);       
}

and activity_concept.xml activity_concept.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtOurStory"
                style="@style/concept_text" />

            <TextView
                android:id="@+id/txtOurVision"
                style="@style/concept_text" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

The code works perfectly on the first run, but if restarted from a crash, findviewbyid returns null .. I am using the exact steps/code in other activities and it's not happening. 该代码在第一次运行时运行良好,但是如果从崩溃中重新启动,则findviewbyid返回null ..我在其他活动中使用的是确切的步骤/代码,但没有发生。 I tried almost all solutions found on stackoverflow wherever the scenario is similar, still nothing. 我尝试了所有在stackoverflow上找到的几乎所有解决方案,无论情况如何,都仍然没有。

any ideas? 有任何想法吗? thanks a lot in advance 提前非常感谢

UPDATE UPDATE

Error from LogCat LogCat错误

05-24 16:02:20.356 22368-22368/com.company.appname E/art: Throwing OutOfMemoryError "Failed to allocate a 48024300 byte allocation with 16777216 free bytes and 36MB until OOM"
05-24 16:02:20.366 22368-22368/com.company.appname E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.company.appname, PID: 22368
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.appname/com.company.appname.MenuActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:145)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                                                       Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:640)
                                                                          at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
                                                                          at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                                                                          at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428)
                                                                          at android.app.Activity.setContentView(Activity.java:2267)
                                                                          at com.company.appname.MenuActivity.onCreate(MenuActivity.java:31)
                                                                          at android.app.Activity.performCreate(Activity.java:6288)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:145) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5942) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
                                                                       Caused by: java.lang.reflect.InvocationTargetException
                                                                          at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                          at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:614)
                                                                          at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                                                                          at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
                                                                          at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428) 
                                                                          at android.app.Activity.setContentView(Activity.java:2267) 
                                                                          at com.company.appname.MenuActivity.onCreate(MenuActivity.java:31) 
                                                                          at android.app.Activity.performCreate(Activity.java:6288) 
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:145) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5942) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
                                                                       Caused by: java.lang.OutOfMemoryError: Failed to allocate a 48024300 byte allocation with 16777216 free bytes and 36MB until OOM
                                                                          at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                          at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                          at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:726)
                                                                          at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:547)
                                                                          at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014)
                                                                          at android.content.res.Resources.loadDrawableForCookie(Resources.java:3747)
                                                                          at android.content.res.Resources.loadDrawable(Resources.java:3620)
                                                                          at android.content.res.TypedArray.getDrawable(TypedArray.java:762)
                                                                          at android.widget.ImageView.<init>(ImageView.java:151)
                                                                          at android.widget.ImageView.<init>(ImageView.java:140)
                                                                          at android.widget.ImageView.<init>(ImageView.java:136)
                                                                          at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                          at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
                                                                          at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                                                                          at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
                                                                          at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428) 
                                                                          at android.app.Activity.setContentView(Activity.java:2267) 
                                                                          at com.company.appname.MenuActivity.onCreate(MenuActivity.java:31) 
                                                                          at android.app.Activity.performCreate(Activity.java:6288) 
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:145) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5942) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

txtOurStory id is not available in layout file. txtOurStory ID在布局文件中不可用。

Make sure all the ID's which you are using in Java must be available in layout file. 确保您在Java中使用的所有ID必须在布局文件中可用。

Change this line id wrong 更改此行ID错误

txtOurStory = (TextView) findViewById(R.id.txtOurMission);
txtOurVision = (TextView) findViewById(R.id.txtOurVision);

Seems the issue never happened since I following @Prashant's comment/suggestion: 自从我关注@Prashant的评论/建议以来,似乎从未发生过该问题:

this is out of memory exception which you need to analyze why your app is using so much memory and try to free them when you don't need it. 这是内存不足的例外,您需要分析为什么您的应用程序使用了如此多的内存,并在不需要时尝试释放它们。 analyze memory heap. 分析内存堆。 i guess there may be image in your layout at line no 18 used by MenuActivity.java which may be larger in resolution.. 我猜想您的布局中可能存在MenuActivity.java使用的第18行的图像,它的分辨率可能更高。

I did go through my app and changed the Image Loading library which caused this exception, so the app never crashed since then therefore I couldn't really figure out if THIS was the solution to the problem. 我确实浏览了我的应用程序,并更改了导致此异常的图像加载库,因此此后该应用程序从未崩溃过,因此我无法真正确定这是否是解决问题的方法。 but it seems it was. 但似乎是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM