开发Android平板应用程序过程中被困扰了很多天的问题!求大神支招
问题是这样的,首先从loginactivity页面登陆(暂且称A),跳转到第二个页面(暂且称B)时,会解析本地的一个XML文档,然后将XML文档中的信息在B页面上的listview中显示出来。当在A页面按登陆按钮时,会成功跳转到B页面,B页面中的LISTVIEW也能出现数据。但是,在B页面停留大概1秒钟的时间(几乎就是闪了一下),程序又自动地跳转回A页面,这时,如果再按A上的登陆按钮,程序就自动退出了。
下面是logcat中的错误信息:
11-19 16:44:32.019: E/StrictMode(1601): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
11-19 16:44:32.019: E/StrictMode(1601): java.lang.Throwable: Explicit termination method 'close' not called
11-19 16:44:32.019: E/StrictMode(1601): at dalvik.system.CloseGuard.open(CloseGuard.java:184)
11-19 16:44:32.019: E/StrictMode(1601): at java.io.FileInputStream.<init>(FileInputStream.java:80)
11-19 16:44:32.019: E/StrictMode(1601): at com.doctorstation.BLL.EntityService.setInputStream(EntityService.java:46)
11-19 16:44:32.019: E/StrictMode(1601): at com.doctorstation.BLL.EntityService.<init>(EntityService.java:34)
11-19 16:44:32.019: E/StrictMode(1601): at com.doctorstation.UI.LoginActivity$1.onClick(LoginActivity.java:130)
11-19 16:44:32.019: E/StrictMode(1601): at android.view.View.performClick(View.java:3511)
11-19 16:44:32.019: E/StrictMode(1601): at android.view.View$PerformClick.run(View.java:14109)
11-19 16:44:32.019: E/StrictMode(1601): at android.os.Handler.handleCallback(Handler.java:605)
11-19 16:44:32.019: E/StrictMode(1601): at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 16:44:32.019: E/StrictMode(1601): at android.os.Looper.loop(Looper.java:137)
11-19 16:44:32.019: E/StrictMode(1601): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-19 16:44:32.019: E/StrictMode(1601): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 16:44:32.019: E/StrictMode(1601): at java.lang.reflect.Method.invoke(Method.java:511)
11-19 16:44:32.019: E/StrictMode(1601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-19 16:44:32.019: E/StrictMode(1601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-19 16:44:32.019: E/StrictMode(1601): at dalvik.system.NativeStart.main(Native Method)
11-19 16:44:32.019: W/System.err(1601): StrictMode VmPolicy violation with POLICY_DEATH; shutting down.
下面是B页面的源码:
package com.doctorstation.UI;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.doctorstation.DAL.BasicInfo;
import com.doctorstation.DAL.PatMaster;
import com.doctorstation.BLL.DoctorXMLReader;
import com.doctorstation.BLL.EntityService;
import com.doctorstation.UI.R;
public class PatientMasterIndex extends Activity /*implements
OnItemClickListener, OnCheckedChangeListener*/ {
String id;
String visit_id; //病房
String dept_name;
String dept_code;
String bed_no; //床号
String pat_name;
String pat_sex;
String pat_birthday;
String pat_in;
String pat_out;
String nursing_class;
String DocName;
String DeptName;
CheckBox cb_notallpat;
private ListView patListView; //声明listView类型变量
SimpleAdapter ordersListItemAdapter;
List<Map<String, Object>> ordersListItem = new ArrayList<Map<String, Object>>();
//String[] ordersKeys = new String[] {"BedNo", "Name","Sex","Birthday", "DateIn","DateOut","NursingClass" };
//int[] ordersIds = new int[] {R.id.bednumber,R.id.patname,R.id.patsex,R.id.patbirth,R.id.patin,R.id.patout,R.id.nurclass};
@Override
//String[] ordersKeys = new String[] { "doctor_order_lancher", "ItemTitle",
//"ItemText", "AdministrationCode", "Administration", "OrderStatus",
//"RepeatIndicator", "Paient_Id", "Visit_Id", "new_order" };
// int[] ordersIds = new int[]
// {R.id.patient_master_index2_sex_img,R.id.patient_master_index2_name,R.id.patient_master_index2_bed_no,R.id.patient_master_index2_administration_code,R.id.patient_master_index2_administration,
// R.id.patient_master_index2_order_status,R.id.patient_master_index2_repeat_indicator,R.id.patient_master_index2_patient_id,
// R.id.patient_master_index2_visit_id,
// R.id.patient_master_index2_is_new_img}; //需要修改
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.patientmanagement);
patListView = (ListView) findViewById(R.id.patient_master_index_listview);
cb_notallpat=(CheckBox)findViewById(R.id.cb_notallpat);
Intent intent = this.getIntent(); //获取已有的intent对象
Bundle bundle = intent.getExtras(); //获取intent里面的bundle对象
DocName = bundle.getString("DocName"); //获取Bundle里面的字符串
DeptName=bundle.getString("DeptName");
TextView tv=(TextView)findViewById(R.id.textView2);
TextView tv1=(TextView)findViewById(R.id.textView4);
tv.setText(DocName);
tv1.setText(DeptName);
ordersListItemAdapter =new SimpleAdapter(this,getorderListData(),//数据源
R.layout.listviewitem,//ListItem的XML实现
new String[]{"BedNo", "Name","Sex","Birthday", "DateIn","DateOut","NursingClass"},//动态数组与ImageItem对应的子项
new int[] {R.id.bednumber,R.id.patname,R.id.patsex,R.id.patbirth,R.id.patin,R.id.patout,R.id.nurclass});
patListView.setAdapter(ordersListItemAdapter);
}
public List<Map<String,Object>> getorderListData(){
DoctorXMLReader dxr =new DoctorXMLReader("basic_info.xml");//解析本地XML文件,这里DoctorXMLReader是基于PULL解析XML的方法
List<BasicInfo> allBI;
try {
allBI = dxr.getBasicInfo();
for(BasicInfo bi:allBI)
{
//for(int i=0;i<allBI.size();i++){
Map<String,Object> map=new HashMap<String,Object>();
//"BedNo", "Name","Sex","Birthday", "DateIn","DateOut","NursingClass"
map.put("BedNo",bi.gete());
map.put("Name",bi.getg());
map.put("Sex",bi.geti());
map.put("Birthday",bi.getj());
map.put("DateIn",bi.geto());
map.put("DateOut",bi.getp());
map.put("NursingClass",bi.getr());
ordersListItem.add(map);
}
}
catch (Exception e) {
e.printStackTrace();
}
return ordersListItem;
}
}
android 内存泄露 listview ?resource release
[解决办法]
资源读写的时候出现问题,
11-19 16:44:32.019: E/StrictMode(1601): java.lang.Throwable: Explicit termination method 'close' not called
11-19 16:44:32.019: E/StrictMode(1601): at dalvik.system.CloseGuard.open(CloseGuard.java:184)
这个意思是 没有显示的调用close方法 去释放资源。
[解决办法]
首先楼主你给出的源码文件不对,应该是EntityService 这个类文件。com.doctorstation.BLL.EntityService.setInputStream(EntityService.java:46)
你去看下,这个文件中的FileInputStream对象没有调用close()方法来关闭文件输入流,或者由于try catch语句在错误出现的情况下跳过了cloase()函数的调用。