编程语言
首页 > 编程语言> > java-将来的功能是否仍需要AssetManager?

java-将来的功能是否仍需要AssetManager?

作者:互联网

什么时候创建并关闭AssetManager的实例.

我的程序中有一个错误. DDMS在说…

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):     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

我不会发布所有代码,因为它真的很长,但是我的程序的结构与此类似.

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;
    }
}

我注意到这一行:

07-04 18:45:11.191:错误/ AndroidRuntime(277):原因:java.lang.RuntimeException:Assetmanager已关闭

因此,问题似乎在于第一个函数“ usesAnInstanceOfAssetManager”正在关闭资产管理器,而第二个函数则需要资产管理器.我在函数“ usesAnInstanceOfAssetManager”中注释了assetManager.close()行,并且我的应用程序没有崩溃.所以我的问题是…

使用AssetManager的最佳实践是什么?每个程序只能使用一次吗?尽管通过声明它的一个实例并在完成后将其关闭是我的适当做法,但事实并非如此. AssetManager是否“静态”?我是否总是需要它?

解决方法:

我假设在名为AddTextToSpinner()的Activity之后可以关闭AssetManager.
如果您在其他地方需要它,只需再次打开它.

如果您需要有关AssetManager的更多信息,请访问以下链接:
http://developer.android.com/reference/android/content/res/AssetManager.html

我使用AssetManager的方式如下:

我称我的parseXMLFile(),
在那打开我的AssetManager,
我打开XML文件,将数据读取并存储在对象中,
关闭AssetManager.

这样,我就可以使用AssetManager一次,以后在我的应用程序中我就可以使用这些对象.

我希望这是您正在寻找的答案!

标签:ddms,java,android
来源: https://codeday.me/bug/20191208/2089754.html