开发者

Is AssetManager still needed by future functions?

开发者 https://www.devze.com 2023-03-17 11:03 出处:网络
When do I create and close an instance of AssetManager. I had a bug in my program.The DDMS was saying....

When do I create and close an instance of AssetManager.

I had a bug in my program. The DDMS was saying....

07-04 18:44:59.241: DEBUG/KeyguardViewMediator(65): pokeWakelock(5000)
07-04 18:44:59.541: DEBUG/KeyguardViewMediator(65): pokeWakelock(5000)
07-04 18:45:00.101: INFO/ARMAssembler(65): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x33b540:0x33b64c] in 9815520 ns
07-04 18:45:00.281: INFO/ARMAssembler(65): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x33c088:0x33c250] in 2206721 ns
07-04 18:45:00.561: WARN/ActivityManager(65): finishReceiver called but no pending broadcasts
07-04 18:45:10.843: DEBUG/AndroidRuntime(277): Shutting down VM
07-04 18:45:10.843: WARN/dalvikvm(277): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-04 18:45:11.191: ERROR/AndroidRuntime(277): FATAL EXCEPTION: main
07-04 18:45:11.191: ERROR/AndroidRuntime(277): android.content.res.Resources$NotFoundException: File res/layout/simple_spinner_item.xml from xml type layout resource ID #0x1090008
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1916)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1871)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.content.res.Resources.getLayout(Resources.java:731)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.View.measure(View.java:8171)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.TableRow.getColumnsWidths(TableRow.java:308)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.TableLayout.findLargestCells(TableLayout.java:497)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.TableLayout.measureVertical(TableLayout.java:462)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.TableLayout.onMeasure(TableLayout.java:428)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.View.measure(View.java:8171)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.View.measure(View.java:8171)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.View.measure(View.java:8171)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.View.measure(View.java:8171)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.View.measure(View.java:8171)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.os.Looper.loop(Looper.java:123)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at java.lang.reflect.Method.invoke(Method.java:521)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):开发者_Python百科     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at dalvik.system.NativeStart.main(Native Method)
07-04 18:45:11.191: ERROR/AndroidRuntime(277): Caused by: java.lang.RuntimeException: Assetmanager has been closed
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:483)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1898)
07-04 18:45:11.191: ERROR/AndroidRuntime(277):     ... 36 more

I won't post all my codes since it is really long but my program is structured similar to this....

public class myAppActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        usesAnInstanceOfAssetManager();
        addsTextToASpinner(); 
    }


    private void usesAnInstanceOfAssetManager() {
        AssetManager assetManager = getAssets();
        ///blah blah blah.....
        assetManager.close();
        return;
    }

    public void addsTextToASpinner(){
        // code to add text fields to a spinner
        // executing this function caused the error shown above
        return;
    }
}

I noticed the line:

07-04 18:45:11.191: ERROR/AndroidRuntime(277): Caused by: java.lang.RuntimeException: Assetmanager has been closed

So the problem seemed to be that the first function "usesAnInstanceOfAssetManager" was closing the asset manager and the second function needed the assetManager. I commented out the assetManager.close() line in the function "usesAnInstanceOfAssetManager" and my app did not crash. So my question/s is....

What is the best practice for using an AssetManager? Can I only use it once per program? I though by declaring an instance of it and closing it when I was done was the appropriate course of action but that doesn't appear to be the case. Is AssetManager "static"? Do I always need it?


So far I can see, AssetManager is created by the system, there's no need to open or close AssertManager.

What the application need to do is open/close the inputStream it use. Correct me if I'm wrong.


You can close the AssetManager after the Activity called AddTextToSpinner() I assume. If you need it else where, just open it again.

If you need more info about the AssetManager you should visit this link: http://developer.android.com/reference/android/content/res/AssetManager.html

The way I use my AssetManager is as following:

I call my parseXMLFile(), in there I open my AssetManager, I open my XML file, Read and Store the data in objects, close AssetManager.

This way I use the AssetManager once, and later in my application I can just work with the objects.

I hope this was the answer you were looking for!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号