未来的功能是否还需要AssetManager?

时间:2011-07-04 23:19:43

标签: java android ddms

何时创建和关闭AssetManager的实例。

我的程序中有一个错误。 DDMS说.... [/ p>

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:ERROR / AndroidRuntime(277):引起:java.lang.RuntimeException:Assetmanager已关闭

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

使用AssetManager的最佳做法是什么?我可以每个程序只使用一次吗?我虽然通过声明它的一个实例并在我完成时关闭它是适当的行动方案,但似乎并非如此。 AssetManager是“静态的”吗?我总是需要吗?

2 个答案:

答案 0 :(得分:3)

到目前为止,我可以看到,AssetManager是由系统创建的,不需要打开或关闭AssertManager。

应用程序需要做的是打开/关闭它使用的inputStream。 如果我错了,请纠正我。

答案 1 :(得分:0)

我可以在调用AddTextToSpinner()的Activity之后关闭AssetManager。 如果您需要其他地方,请再次打开它。

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

我使用AssetManager的方式如下:

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

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

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