我正在创建一个活动来管理Sqlite数据库上的表(Medicamentos)的数据。 我有一个listfragment来显示数据,当从菜单(Agregar Medicamentos)中选择时,重定向到另一个活动以添加一个新活动。然后返回到片段并刷新视图(但此时它崩溃了。)
这是片段的代码:
import java.util.ArrayList;
import org.geofarma.medser.inventario.db.DbHandler;
import org.geofarma.medser.inventario.entidades.Medicamento;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ListMedicamentoFragment extends ListFragment {
public class MedicamentosAdapter extends ArrayAdapter<Medicamento> {
Context m_context;
public MedicamentosAdapter(Context context,int textViewResourceId,Medicamento[] medicamentos){
super(context,textViewResourceId,medicamentos);
m_context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.item_medicamento, parent, false);
TextView lbl = (TextView)row.findViewById(R.id.txtNombreMedicamento);
lbl.setText(getItem(position).getNombre());
return row;
}
}
ArrayList<Medicamento> m_medicamento;
MedicamentosAdapter medicamentosAdapter;
DbHandler dbHandler;
@Override
public void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHandler = new DbHandler(getActivity(),null,null,1);
m_medicamento = dbHandler.readMedicamento();
Medicamento[] arrayMedicamento = new Medicamento[m_medicamento.size()];
arrayMedicamento = m_medicamento.toArray(arrayMedicamento);
dbHandler.close();
setHasOptionsMenu(true);
medicamentosAdapter = new MedicamentosAdapter(getActivity(), R.layout.item_medicamento, arrayMedicamento);
setListAdapter(medicamentosAdapter);
};
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
menu.add(R.string.action_addMedicamento);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i("onOptionsItemSelected", String.valueOf(item.getItemId()));
if(item.getItemId() == 0){
Intent intent = new Intent(this.getActivity(),NuevoMedicamento.class);
startActivityForResult(intent, 1);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
if(data!=null){
boolean success = data.getBooleanExtra("SUCCESS", false);
if(success){
this.medicamentosAdapter.clear();
dbHandler = new DbHandler(getActivity(),null,null,1);
m_medicamento = dbHandler.readMedicamento();
dbHandler.close();
Medicamento[] arrayMedicamento = new Medicamento[this.m_medicamento.size()];
arrayMedicamento = m_medicamento.toArray(arrayMedicamento);
this.medicamentosAdapter.addAll(arrayMedicamento);
this.medicamentosAdapter.notifyDataSetChanged();
}
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_medicamentos, container,false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Medicamento medicamento = m_medicamento.get(position);
Toast.makeText(getActivity(), medicamento.getNombre(), Toast.LENGTH_LONG).show();
}
}
执行onActivityResult
方法时出现异常。
更新 这是日志。
05-25 23:56:53.080: E/AndroidRuntime(20058): FATAL EXCEPTION: main
05-25 23:56:53.080: E/AndroidRuntime(20058): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=1, data=Intent { (has extras) }} to activity {org.geofarma.medser.inventario/org.geofarma.medser.inventario.PrincipalActivity}: java.lang.UnsupportedOperationException
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.app.ActivityThread.deliverResults(ActivityThread.java:2991)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3034)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.app.ActivityThread.access$1100(ActivityThread.java:127)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.os.Looper.loop(Looper.java:137)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.app.ActivityThread.main(ActivityThread.java:4511)
05-25 23:56:53.080: E/AndroidRuntime(20058): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 23:56:53.080: E/AndroidRuntime(20058): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 23:56:53.080: E/AndroidRuntime(20058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
05-25 23:56:53.080: E/AndroidRuntime(20058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
05-25 23:56:53.080: E/AndroidRuntime(20058): at dalvik.system.NativeStart.main(Native Method)
05-25 23:56:53.080: E/AndroidRuntime(20058): Caused by: java.lang.UnsupportedOperationException
05-25 23:56:53.080: E/AndroidRuntime(20058): at java.util.AbstractList.remove(AbstractList.java:638)
05-25 23:56:53.080: E/AndroidRuntime(20058): at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
05-25 23:56:53.080: E/AndroidRuntime(20058): at java.util.AbstractList.removeRange(AbstractList.java:658)
05-25 23:56:53.080: E/AndroidRuntime(20058): at java.util.AbstractList.clear(AbstractList.java:466)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.widget.ArrayAdapter.clear(ArrayAdapter.java:258)
05-25 23:56:53.080: E/AndroidRuntime(20058): at org.geofarma.medser.inventario.ListMedicamentoFragment.onActivityResult(ListMedicamentoFragment.java:92)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
05-25 23:56:53.080: E/AndroidRuntime(20058): at android.app.ActivityThread.deliverResults(ActivityThread.java:2987)
05-25 23:56:53.080: E/AndroidRuntime(20058): ... 11 more
05-25 23:56:53.110: E/SQLiteDatabase(20058): close() was never explicitly called on database '/data/data/org.geofarma.medser.inventario/databases/DbMedicamentos'
05-25 23:56:53.110: E/SQLiteDatabase(20058): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:2072)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1126)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1083)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1170)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:844)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at org.geofarma.medser.inventario.db.DbHandler.createMedicamento(DbHandler.java:101)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at org.geofarma.medser.inventario.NuevoMedicamento$1.onClick(NuevoMedicamento.java:31)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.view.View.performClick(View.java:3627)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.view.View$PerformClick.run(View.java:14329)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.os.Handler.handleCallback(Handler.java:605)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.os.Looper.loop(Looper.java:137)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at android.app.ActivityThread.main(ActivityThread.java:4511)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
05-25 23:56:53.110: E/SQLiteDatabase(20058): at dalvik.system.NativeStart.main(Native Method)
05-25 23:56:53.115: E/System(20058): Uncaught exception thrown by finalizer
05-25 23:56:53.115: E/System(20058): java.lang.IllegalStateException: Don't have database lock!
05-25 23:56:53.115: E/System(20058): at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2230)
05-25 23:56:53.115: E/System(20058): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2322)
05-25 23:56:53.115: E/System(20058): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2318)
05-25 23:56:53.115: E/System(20058): at android.util.LruCache.trimToSize(LruCache.java:197)
05-25 23:56:53.115: E/System(20058): at android.util.LruCache.evictAll(LruCache.java:285)
05-25 23:56:53.115: E/System(20058): at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2283)
05-25 23:56:53.115: E/System(20058): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1255)
05-25 23:56:53.115: E/System(20058): at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:2043)
05-25 23:56:53.115: E/System(20058): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:185)
05-25 23:56:53.115: E/System(20058): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
05-25 23:56:53.115: E/System(20058): at java.lang.Thread.run(Thread.java:856)