问题
Hello I am working on an app and in which at one point I need to take input from user and store it in database and then fetch that data and put it on different TextView. Problem is I did stored the data in database and when i trying to fetch it My app shows a pop up i.e : App is not responding and logcat shows this: Package has already posted 50 toasts. Not showing more. I have used 6 database operations before this pullAccStt can this be a problem.
DatabseAdapotor
public long updateAccSettTable(String loactionName, String hospitalname, String PatientId, String FirstName, String MiddleName, String LastName, String DOB,
String Gender, String Weight, String Height, String MobNo, String emailId, String Ethinicity, String patientConcentStatus, String DevicePatientId,
String CoSensorId, String DeviceId, String VideoId){
SQLiteDatabase db= helper.getWritableDatabase();
ContentValues contentValuesAccSett= new ContentValues();
contentValuesAccSett.put(DatabaseHelper.PatientId,PatientId);
contentValuesAccSett.put(DatabaseHelper.LocationName,loactionName);
contentValuesAccSett.put(DatabaseHelper.HospitalName,hospitalname);
contentValuesAccSett.put(DatabaseHelper.FirstName,FirstName);
contentValuesAccSett.put(DatabaseHelper.MiddleName,MiddleName);
contentValuesAccSett.put(DatabaseHelper.LastName,LastName);
contentValuesAccSett.put(DatabaseHelper.DateOfBirth,DOB);
contentValuesAccSett.put(DatabaseHelper.Gender,Gender);
contentValuesAccSett.put(DatabaseHelper.Weight,Weight);
contentValuesAccSett.put(DatabaseHelper.Height,Height);
contentValuesAccSett.put(DatabaseHelper.MobileNo,MobNo);
contentValuesAccSett.put(DatabaseHelper.EmailId,emailId);
contentValuesAccSett.put(DatabaseHelper.Ethnicity,Ethinicity);
contentValuesAccSett.put(DatabaseHelper.P_ConsentStatus,patientConcentStatus);
contentValuesAccSett.put(DatabaseHelper.DevicePId,DevicePatientId);
contentValuesAccSett.put(DatabaseHelper.DeviceId,DeviceId);
contentValuesAccSett.put(DatabaseHelper.CoSensorId,CoSensorId);
contentValuesAccSett.put(DatabaseHelper.VideoId,VideoId);
long id1= db.insert(DatabaseHelper.Accoun_setting_Table,null,contentValuesAccSett);
if (id1<0){
Message.message(helper.context,"Unsuccessfull");
}else {
Message.message(helper.context,"Account setting Updated");
}
db.close();
return id1;
}
public String pullAccSett(){
String patientId = null;
Message.message(helper.context,"patient id is: ");
SQLiteDatabase db=helper.getWritableDatabase();
String query="Select * From "+DatabaseHelper.Accoun_setting_Table+" ;";
Cursor AccCursor=db.rawQuery(query,null);
Message.message(helper.context,"query "+query);
while(AccCursor.moveToFirst()) {
int index1 = AccCursor.getColumnIndex(DatabaseHelper.PatientId);
patientId=AccCursor.getString(index1);
}
AccCursor.close();
db.close();
return patientId ;
}
Activity Class calling this
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class AccountSettingFragment extends Fragment {
FloatingActionButton fabEdit;
DatabaseAdaptor databaseAdaptor=null;
TextView PatientId;
private Context context;
public AccountSettingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_account_setting, container, false);
fabEdit=(FloatingActionButton)view.findViewById(R.id.btnFabEdit);
PatientId=(TextView)view.findViewById(R.id.patientId);
context=getActivity();
databaseAdaptor=new DatabaseAdaptor(context);
databaseAdaptor.pullAccSett();
fabEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AccountsSettingEdit.class);
getActivity().startActivity(intent);
}
});
return view;
}
}
Message Class
import android.content.Context;
import android.widget.Toast;
/**
* Created by frnds on 3/2/2016.
*/
public class Message {
public static void message(Context context,String message){
Toast.makeText(context,message,Toast.LENGTH_LONG).show();
}
}
And when i call this pullAccSett(); my app hangs and goes to not responding. and log cat shows message: Package has already posted 50 toasts. Not showing more. What is the reason behind this.
回答1:
Your while loop i thinks will runs for infinite time. because every time it will be true and you have no break statement. so change your code like this.
try {
if (AccCursor.moveToFirst()) {
while (AccCursor.moveToNext()) {
//code for fetch data from cursor
}
}
AccCursor.close();
}catch (Exception e) {
Log.e("Getdata", e.getMessage(), e);
}
回答2:
There is some enhancement for the code above. If cursor is in position -1, AccCursor.moveToNext will move it to position 0, so you can do better something like:
try {
while(AccCursor.moveToNext()) {
//Execute code
}
AccCursor.close();
}catch(final Exception lException) {
Log.e("Getdata", "Exception looping Cursor: " + lException.getMessage());
}
来源:https://stackoverflow.com/questions/36836393/package-has-already-posted-50-toasts-not-showing-more