ProgressDialog 未关闭
ProgressDialog not closing
这是我的代码,看起来一切都很好,但是当应用程序启动时不会关闭 ProgressDialog,它是可以取消的,但不会消失,它自应用程序启动后就显示出来,而且它是不应该那样
private void displayView(int position) {
final int thePos=position;
final ProgressDialog myProgressDialog = ProgressDialog.show(MainActivity.this, "", "Procesando...", true);
myProgressDialog.setCancelable(true);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// update the main content by replacing fragments
Fragment fragment = null;
switch (thePos) {
/*case 0:
fragment = new HomeFragment();
break;*/
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new CalendarioFragment();
break;
case 5:
fragment = new ContactoFragment();
break;
case 6:
fragment = new OnclickFragment();
break;
default:
break;
}
if (fragment != null) {
try{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and tit|le, then close the drawer
mDrawerList.setItemChecked(thePos, true);
mDrawerList.setSelection(thePos);
setTitle(navMenuTitles[thePos]);
mDrawerLayout.closeDrawer(mDrawerList);
}
catch(Exception E)
{
Log.e("MainActivity", "Error in creating fragment");
}
}
else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}});
}
catch (Throwable t) {
// just end the background thread
Log.i("Animation", "Thread exception " + t);
}
}
private void threadMsg(String msg) {
if (!msg.equals(null) && !msg.equals("")) {
Message msgObj = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
handler.sendMessage(msgObj);
}
}
// Define the Handler that receives messages from the thread and update the progress
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
myProgressDialog.dismiss();
}
}
};
})
.start(); } //After call start method thread called run Method
有人能看到错误吗?请给我看看
好吧,几个小时后,我意识到进度对话框是错误的,我将它添加到 onItemClick 中并在 class 中进行切换,所以答案是将 ProgressDialog 添加到导航抽屉
private class SlideMenuClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
final int thePos=position;
final ProgressDialog myProgressDialog = ProgressDialog.show(MainActivity.this, "", "Procesando...", true);
myProgressDialog.setCancelable(true);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// display view for selected nav drawer item
displayView(thePos);
myProgressDialog.dismiss();
}});
}
catch (Throwable t) {
// just end the background thread
Log.i("Animation", "Thread exception " + t);
}
}
private void threadMsg(String msg) {
if (!msg.equals(null) && !msg.equals("")) {
Message msgObj = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
handler.sendMessage(msgObj);
}
}
// Define the Handler that receives messages from the thread and update the progress
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
myProgressDialog.dismiss();
}
}
};
})
.start(); }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
CreaMenu();
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
final int thePos=position;
// update the main content by replacing fragments
Fragment fragment = null;
switch (thePos) {
/*case 0:
fragment = new HomeFragment();
break;*/
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new CalendarioFragment();
break;
case 5:
fragment = new ContactoFragment();
break;
case 6:
fragment = new OnclickFragment();
break;
default:
break;
}
if (fragment != null) {
try{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and tit|le, then close the drawer
mDrawerList.setItemChecked(thePos, true);
mDrawerList.setSelection(thePos);
setTitle(navMenuTitles[thePos]);
mDrawerLayout.closeDrawer(mDrawerList);
}
catch(Exception E)
{
Log.e("MainActivity", "Error in creating fragment");
}
}
else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
//After call start method thread called run Method
这是我的代码,看起来一切都很好,但是当应用程序启动时不会关闭 ProgressDialog,它是可以取消的,但不会消失,它自应用程序启动后就显示出来,而且它是不应该那样
private void displayView(int position) {
final int thePos=position;
final ProgressDialog myProgressDialog = ProgressDialog.show(MainActivity.this, "", "Procesando...", true);
myProgressDialog.setCancelable(true);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// update the main content by replacing fragments
Fragment fragment = null;
switch (thePos) {
/*case 0:
fragment = new HomeFragment();
break;*/
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new CalendarioFragment();
break;
case 5:
fragment = new ContactoFragment();
break;
case 6:
fragment = new OnclickFragment();
break;
default:
break;
}
if (fragment != null) {
try{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and tit|le, then close the drawer
mDrawerList.setItemChecked(thePos, true);
mDrawerList.setSelection(thePos);
setTitle(navMenuTitles[thePos]);
mDrawerLayout.closeDrawer(mDrawerList);
}
catch(Exception E)
{
Log.e("MainActivity", "Error in creating fragment");
}
}
else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}});
}
catch (Throwable t) {
// just end the background thread
Log.i("Animation", "Thread exception " + t);
}
}
private void threadMsg(String msg) {
if (!msg.equals(null) && !msg.equals("")) {
Message msgObj = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
handler.sendMessage(msgObj);
}
}
// Define the Handler that receives messages from the thread and update the progress
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
myProgressDialog.dismiss();
}
}
};
})
.start(); } //After call start method thread called run Method
有人能看到错误吗?请给我看看
好吧,几个小时后,我意识到进度对话框是错误的,我将它添加到 onItemClick 中并在 class 中进行切换,所以答案是将 ProgressDialog 添加到导航抽屉
private class SlideMenuClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
final int thePos=position;
final ProgressDialog myProgressDialog = ProgressDialog.show(MainActivity.this, "", "Procesando...", true);
myProgressDialog.setCancelable(true);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// display view for selected nav drawer item
displayView(thePos);
myProgressDialog.dismiss();
}});
}
catch (Throwable t) {
// just end the background thread
Log.i("Animation", "Thread exception " + t);
}
}
private void threadMsg(String msg) {
if (!msg.equals(null) && !msg.equals("")) {
Message msgObj = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
handler.sendMessage(msgObj);
}
}
// Define the Handler that receives messages from the thread and update the progress
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
myProgressDialog.dismiss();
}
}
};
})
.start(); }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
CreaMenu();
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
final int thePos=position;
// update the main content by replacing fragments
Fragment fragment = null;
switch (thePos) {
/*case 0:
fragment = new HomeFragment();
break;*/
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new CalendarioFragment();
break;
case 5:
fragment = new ContactoFragment();
break;
case 6:
fragment = new OnclickFragment();
break;
default:
break;
}
if (fragment != null) {
try{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and tit|le, then close the drawer
mDrawerList.setItemChecked(thePos, true);
mDrawerList.setSelection(thePos);
setTitle(navMenuTitles[thePos]);
mDrawerLayout.closeDrawer(mDrawerList);
}
catch(Exception E)
{
Log.e("MainActivity", "Error in creating fragment");
}
}
else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
//After call start method thread called run Method