当我按下后退按钮时应用程序没有响应
Application not responding when i press the back button
在我的应用程序中,我只有一个从数据库获取数据到列表视图的异步任务,但如果没有数据,则什么也不会显示,当我按下后退按钮时,应用程序没有响应问题是甚至没有错误我的日志甚至没有一个错误。
当我按下后退按钮时,它开始滞后,然后它说应用程序没有响应,但如果我的数据库中有数据,应用程序运行正常。
有什么想法吗?
private class SyncData extends AsyncTask<String, String, String> {
String msg;
@Override
protected void onPreExecute() //Starts the progress dailog
{
System.out.println("Start");
//lottieAnimationView.playAnimation();
customProgress.showProgress(supervisor_specialty.this, "Loading..."
, false);
}
@Override
protected String doInBackground(String... strings) // Connect to the database, write query and add items to array list
{
try {
Connection conn = connectionClass.CONN(); //Connection Object
if (conn == null) {
success = false;
msg = "Sorry something went wrong,Please check your internet connection";
} else {
// Change below query according to your own database.
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs != null) // if resultset not null, I add items to itemArraylist using class created
{
while (rs.next()) {
try {
Blob rsBlob = rs.getBlob("Store_Picture");
Boolean active = rs.getBoolean("Active");
itemArrayList.add(new ClassLista(rs.getString("StoreArabicName"),active,rsBlob));
//Picasso.with(agzakhana_mainAr.this).load(rs.getString("SpecialityIcon")).into(specialityicon);
} catch (Exception ex) {
ex.printStackTrace();
}
}
msg = "تم";
success = true;
} else {
msg = "No Data found!";
success = false;
}
}
} catch (Exception e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
Log.d("Error", writer.toString());
success = false;
}
return msg;
}
@Override
protected void onPostExecute(String msg) // disimissing progress dialoge, showing error and setting up my listview
{
//progress2.hideProgress();
customProgress.hideProgress();
System.out.println("End");
if (msg != null) {
Toast.makeText(supervisor_specialty.this, msg + "", Toast.LENGTH_LONG).show();
}
if (!success) {
} else {
try {
myAppAdapter = new MyAppAdapter(itemArrayList, supervisor_specialty.this);
while (myAppAdapter.getCount() == 0) {
}
listView21.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView21.setAdapter(myAppAdapter);
} catch (Exception ex) {
}
}
}
}
public class MyAppAdapter extends BaseAdapter//has a class viewholder which holds
{
public class ViewHolder {
TextView StoreName;
ImageView active;
ImageView StoreIcon;
}
public List<ClassLista> parkingList;
public Context context;
ArrayList<ClassLista> arraylist;
private MyAppAdapter(List<ClassLista> apps, Context context) {
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<ClassLista>();
arraylist.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{
View rowView = convertView;
MyAppAdapter.ViewHolder viewHolder = null;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.listitems2, parent, false);
viewHolder = new MyAppAdapter.ViewHolder();
viewHolder.StoreName = rowView.findViewById(R.id.store_name);
viewHolder.active = rowView.findViewById(R.id.active);
viewHolder.StoreIcon = rowView.findViewById(R.id.store_pic);
rowView.setTag(viewHolder);
} else {
viewHolder = (MyAppAdapter.ViewHolder) convertView.getTag();
}
// here setting up names and images
if (parkingList.get(position).getName() != null){
viewHolder.StoreName.setText(parkingList.get(position).getName() + "");
}
if(parkingList.get(position).getActive()){
viewHolder.active.setImageResource(R.drawable.checkk);
}else {
viewHolder.active.setImageResource(R.drawable.timer);
}
Blob blob = parkingList.get(position).getStoreicon();
if (blob!= null){
byte[] decodedString = new byte[0];
try {
decodedString = Base64.decode(blob.getBytes(1,(int) blob.length()), Base64.NO_WRAP);
} catch (SQLException e) {
e.printStackTrace();
}
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
viewHolder.StoreIcon.setImageBitmap(decodedByte);
}else {
viewHolder.StoreIcon.setImageResource(R.drawable.agzakahana);
}
// Picasso.with(context).load(parkingList.get(position).getDiscountimage()).into(viewHolder.imagediscount);
listView21.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//What happens when you click on a place!
// Intent intent = new Intent(LoggedIn.this,MapsActivity.class);
// startActivity(intent);
}
});
//LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(context, R.anim.thelistanim), 0.3f); //0.5f == time between appearance of listview items.
//listView21.setLayoutAnimation(lac);
//listView21.startLayoutAnimation();
return rowView;
}
}
执行这段代码时:
ResultSet rs = stmt.executeQuery(query);
如果没有数据,这并不意味着它应该为空!
所以当你检查
if (rs != null)
while (rs.next())
因此 while 循环会导致无限循环,导致应用程序出现滞后而不是崩溃。
在我的应用程序中,我只有一个从数据库获取数据到列表视图的异步任务,但如果没有数据,则什么也不会显示,当我按下后退按钮时,应用程序没有响应问题是甚至没有错误我的日志甚至没有一个错误。 当我按下后退按钮时,它开始滞后,然后它说应用程序没有响应,但如果我的数据库中有数据,应用程序运行正常。 有什么想法吗?
private class SyncData extends AsyncTask<String, String, String> {
String msg;
@Override
protected void onPreExecute() //Starts the progress dailog
{
System.out.println("Start");
//lottieAnimationView.playAnimation();
customProgress.showProgress(supervisor_specialty.this, "Loading..."
, false);
}
@Override
protected String doInBackground(String... strings) // Connect to the database, write query and add items to array list
{
try {
Connection conn = connectionClass.CONN(); //Connection Object
if (conn == null) {
success = false;
msg = "Sorry something went wrong,Please check your internet connection";
} else {
// Change below query according to your own database.
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs != null) // if resultset not null, I add items to itemArraylist using class created
{
while (rs.next()) {
try {
Blob rsBlob = rs.getBlob("Store_Picture");
Boolean active = rs.getBoolean("Active");
itemArrayList.add(new ClassLista(rs.getString("StoreArabicName"),active,rsBlob));
//Picasso.with(agzakhana_mainAr.this).load(rs.getString("SpecialityIcon")).into(specialityicon);
} catch (Exception ex) {
ex.printStackTrace();
}
}
msg = "تم";
success = true;
} else {
msg = "No Data found!";
success = false;
}
}
} catch (Exception e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
Log.d("Error", writer.toString());
success = false;
}
return msg;
}
@Override
protected void onPostExecute(String msg) // disimissing progress dialoge, showing error and setting up my listview
{
//progress2.hideProgress();
customProgress.hideProgress();
System.out.println("End");
if (msg != null) {
Toast.makeText(supervisor_specialty.this, msg + "", Toast.LENGTH_LONG).show();
}
if (!success) {
} else {
try {
myAppAdapter = new MyAppAdapter(itemArrayList, supervisor_specialty.this);
while (myAppAdapter.getCount() == 0) {
}
listView21.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView21.setAdapter(myAppAdapter);
} catch (Exception ex) {
}
}
}
}
public class MyAppAdapter extends BaseAdapter//has a class viewholder which holds
{
public class ViewHolder {
TextView StoreName;
ImageView active;
ImageView StoreIcon;
}
public List<ClassLista> parkingList;
public Context context;
ArrayList<ClassLista> arraylist;
private MyAppAdapter(List<ClassLista> apps, Context context) {
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<ClassLista>();
arraylist.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{
View rowView = convertView;
MyAppAdapter.ViewHolder viewHolder = null;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.listitems2, parent, false);
viewHolder = new MyAppAdapter.ViewHolder();
viewHolder.StoreName = rowView.findViewById(R.id.store_name);
viewHolder.active = rowView.findViewById(R.id.active);
viewHolder.StoreIcon = rowView.findViewById(R.id.store_pic);
rowView.setTag(viewHolder);
} else {
viewHolder = (MyAppAdapter.ViewHolder) convertView.getTag();
}
// here setting up names and images
if (parkingList.get(position).getName() != null){
viewHolder.StoreName.setText(parkingList.get(position).getName() + "");
}
if(parkingList.get(position).getActive()){
viewHolder.active.setImageResource(R.drawable.checkk);
}else {
viewHolder.active.setImageResource(R.drawable.timer);
}
Blob blob = parkingList.get(position).getStoreicon();
if (blob!= null){
byte[] decodedString = new byte[0];
try {
decodedString = Base64.decode(blob.getBytes(1,(int) blob.length()), Base64.NO_WRAP);
} catch (SQLException e) {
e.printStackTrace();
}
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
viewHolder.StoreIcon.setImageBitmap(decodedByte);
}else {
viewHolder.StoreIcon.setImageResource(R.drawable.agzakahana);
}
// Picasso.with(context).load(parkingList.get(position).getDiscountimage()).into(viewHolder.imagediscount);
listView21.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//What happens when you click on a place!
// Intent intent = new Intent(LoggedIn.this,MapsActivity.class);
// startActivity(intent);
}
});
//LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(context, R.anim.thelistanim), 0.3f); //0.5f == time between appearance of listview items.
//listView21.setLayoutAnimation(lac);
//listView21.startLayoutAnimation();
return rowView;
}
}
执行这段代码时:
ResultSet rs = stmt.executeQuery(query);
如果没有数据,这并不意味着它应该为空! 所以当你检查
if (rs != null)
while (rs.next())
因此 while 循环会导致无限循环,导致应用程序出现滞后而不是崩溃。