何时创建和关闭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: ERROR/AndroidRuntime(277): Caused by: java.lang.RuntimeException: Assetmanager has been closed
所以问题似乎在于第一个函数"usesAnInstanceOfAssetManager"关闭了asset manager,而第二个函数需要assetManager。我在函数“usesAnInstanceOfAssetManager”中注释掉了assetManager.close()行,我的应用程序没有崩溃。所以我的问题是....
使用AssetManager的最佳实践是什么?我只能在程序中使用一次吗?我认为声明一个实例并在完成后关闭它是适当的做法,但情况并非如此。 AssetManager是否是“静态”的?我总是需要它吗?