android progressBar问题

时间:2011-01-28 23:06:13

标签: android

我有一个点击按钮正在加载rss feed.i想要加载进度条直到我的列表打开。我创建了一个进度条,它可以工作,但是当我按下返回按钮返回主菜单时进度条再次出现并且它没有停止(甚至不让我看到我的菜单)。这是我的代码

ProgressBar myProgressBar;
  int myProgress = 0;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main1);

  Button nea = (Button) findViewById(R.id.nea);
   nea.setOnClickListener(new View.OnClickListener() {
   public void onClick (View view) {
    setContentView(R.layout.bar);

     myProgressBar=(ProgressBar)findViewById(R.id.bar);

           new Thread(myThread).start();

    Intent myIntent = new Intent(view.getContext(), nea.class);
    startActivityForResult(myIntent, 0);


   }  

  });

}

然后,出于onCreate

  private Runnable myThread = new Runnable(){

    @Override
    public void run() {
     // TODO Auto-generated method stub
     while (myProgress<100){
      try{
       myHandle.sendMessage(myHandle.obtainMessage());
       Thread.sleep(1000);
      }
      catch(Throwable t){
      }
     }
    }

    Handler myHandle = new Handler(){

     public void handleMessage(Message msg) {
      // TODO Auto-generated method stub
      myProgress++;
      myProgressBar.setProgress(myProgress);
     }
    };
      };

1 个答案:

答案 0 :(得分:0)

从您启动的startActivityForResult活动返回时,您的活动不会被重新创建,因此您将contentView作为R.layout.bar停留。

尝试覆盖onActivityResult(int requestCode,int resultCode,Intent data)方法和 调用setContentView(R.layout.main1);在里面重置您的视图。您可能需要再次将监听器添加到按钮。