我正在制作一个简单的程序 - 实际上遇到了一个我无法解决的问题。希望这里的任何人都可以帮助我。
编程意图:
=>我通过构造函数将Context
从MainActivity
传递给非活动类
参数。
=>非活动类工作正常 - AssetManager
找到要读取的文件
来自/ src / assets并在
=>但是一旦MainActivity
显示活动 - 它就会崩溃
代码是:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FileReader fr = new FileReader("dtd_smartshopper.dtd",this.getApplicationContext());
}
}
}
public class FileReader {
public FileReader(String fn, Context con) {
String line = null;
try {
AssetManager am = con.getAssets();
InputStream is = am.open(fn);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
while ((line = in.readLine()) != null) {
Log.i("FileReader",line);
}
am.close();
} catch (IOException e) {
Log.e("onBtnOkClick","IOException: " + e);
}
}
}
堆栈跟踪是:
06-04 12:38:54.222: I/FileReader(330): <!-- End of SmartShopper DTD Master File -->
06-04 12:38:54.313: D/AndroidRuntime(330): Shutting down VM
06-04 12:38:54.313: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-04 12:38:54.322: E/AndroidRuntime(330): FATAL EXCEPTION: main
06-04 12:38:54.322: E/AndroidRuntime(330): android.content.res.Resources$NotFoundException: File res/layout/abc_action_menu_layout.xml from xml type layout resource ID #0x7f030009
06-04 12:38:54.322: E/AndroidRuntime(330): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1916)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1871)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.content.res.Resources.getLayout(Resources.java:731)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.support.v7.internal.view.menu.BaseMenuPresenter.getMenuView(BaseMenuPresenter.java:72)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.support.v7.internal.view.menu.ActionMenuPresenter.getMenuView(ActionMenuPresenter.java:151)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.support.v7.internal.widget.ActionBarView.setMenu(ActionBarView.java:428)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:609)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.support.v7.app.ActionBarActivityDelegateBase.supportInvalidateOptionsMenu(ActionBarActivityDelegateBase.java:421)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:230)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.os.Handler.handleCallback(Handler.java:587)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-04 12:38:54.322: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 12:38:54.322: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:521)
06-04 12:38:54.322: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-04 12:38:54.322: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-04 12:38:54.322: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
06-04 12:38:54.322: E/AndroidRuntime(330): Caused by: java.lang.RuntimeException: Assetmanager has been closed
06-04 12:38:54.322: E/AndroidRuntime(330): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:483)
06-04 12:38:54.322: E/AndroidRuntime(330): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1898)
06-04 12:38:54.322: E/AndroidRuntime(330): ... 18 more
我想这可能是一个小问题 - 希望你能帮助我!
答案 0 :(得分:1)
AssetManager
(这就是应用崩溃的原因)。你没有这条需求线:
am.close();
您需要关闭BufferedReader
:
in.close();