2012-05-14 80 views
-1

我需要在進度條填充後顯示第二個活動。我嘗試了下面的代碼,但它沒有顯示進度條,只顯示我的第二個活動。進度條不顯示在屏幕上

這是代碼:

public class MiSuper2 extends Activity { 
     String strListas[] = null; 
     private ProgressBar mProgress; 
     private int mProgressStatus = 0; 
     private Handler mHandler = new Handler(); 
     private StoreData stdArticulos = null; 
     public Cursor cursor = null; 
     private long fileSize = 0; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     stdArticulos = new StoreData(this); 

     fileSize = 0; 
     setContentView(R.layout.main); 
     stdArticulos = new StoreData(this); 
     cursor = stdArticulos.leerArticulos(); 

     mProgress = (ProgressBar) findViewById(R.id.progressbar_activity); 

     new Thread(new Runnable() { 
      public void run() { 
       while (mProgressStatus < 100) { 
        mProgressStatus = doWork(); 

        mHandler.post(new Runnable() { 
         public void run() { 
          mProgress.setProgress(mProgressStatus); 
         } 
        }); 
       } 
      } 
     }).start(); 

     if(cursor.moveToFirst()) { 
      do{ 
       strListas[cursor.getPosition()] = cursor.getString(cursor.getPosition()); 
      }while(cursor.moveToNext()); 
     }  

     Intent intent = new Intent(MiSuper2.this, PntArticulo.class); 
     startActivity(intent); 

    } 

    public int doWork() {  
     while (fileSize <= 1000000) { 
      fileSize++; 
      return (int) fileSize; 
     } 
     return 100; 
    } 
} 

這是main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imvLogo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/presentacion" 
     android:contentDescription="@string/logo"/> 

    <ProgressBar 
     android:id="@+id/progressbar_activity" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" android:layout_marginTop="100dp"/> 

</LinearLayout> 

請幫

+0

您是否需要任何特定的時間進行移動或就像啓動屏幕 – Ramz

+0

第一個屏幕上顯示的內容是什麼?在eclipse視圖編輯器中檢查main.xml時,它是否顯示欄?另外,請不要猶豫,使用Log.d(String,String)語句來查看發生了什麼。 – Blaskovicz

回答

2

看起來你正在使用的doWork()函數佔用的時間,讓你」重新進度欄做一些事情。即使你寫了一個大循環,它仍然執行得非常快,所以你看不到你的進度條移動。相反,你想通過使用Thread.sleep()來模擬你的線程做一些計算密集型的事情,這需要一個參數,即以毫秒爲單位的睡眠時間。

試着改變你的代碼如下:

new Thread(new Runnable() { 
    public void run() { 

     while (mProgressStatus < 100) { 
      try { 
       mProgressStatus += doWork(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      mHandler.post(new Runnable() { 
       public void run() { 
        mProgress.setProgress(mProgressStatus); 
       } 
      }); 
     } 

     runOnUiThread(new Runnable() { 
      @Override 
       public void run() { 
        startActivity(new Intent(MiSuper2.this, Second.class)); 
       } 
      }); 
     } 
}).start(); 

而且......

public int doWork() throws InterruptedException { 
    Thread.sleep(1000); 
    return 1; 
} 

這將通過1%每秒增加你的進度條。最後,關於Thread.sleep()的文檔:https://developer.android.com/reference/java/lang/Thread.html#sleep(long

編輯:拉姆茲擊敗了我這個答案,但沒有提供解釋爲什麼它的答案。希望我的解釋有幫助。編輯2:我想你編輯你的問題,因爲我第二次開始看它。之前您的XML中存在一些錯誤,但現在已經消失。無論如何,您的問題是現在您需要在您的工作線程中呼叫startActivity()。否則,UI線程不會等待doWork()函數返回,並在您的應用程序啓動時立即啓動其他Activity。對不起,我應該提到過這個。我上面發佈的代碼將隨此更改而更新。

+0

謝謝答案shanet我使用您的示例代碼,但仍然只顯示第二個活動,但進度條不 – japuentem

+0

看到我的第二個編輯。 – shanet

1

請試試這個代碼SplashScreen.java

package com.cud.point; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

public class SplashScreen extends Activity { 

ProgressBar bar; 

TextView txt; 

int total=0; 

boolean isRunning=false; 

// handler for the background updating 

Handler handler=new Handler() { 

@Override 

public void handleMessage(Message msg) { 

total=total+20; 

String perc=String.valueOf(total).toString(); 

txt.setText(perc+"% completed"); 

bar.incrementProgressBy(20); 

} 

}; 

@Override 

public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.splash); 

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

txt=(TextView)findViewById(R.id.txt); 
Handler x = new Handler(); 
x.postDelayed(new SplashHandler(), 5000); 
} 
class SplashHandler implements Runnable 
{ 

    public void run() { 
     // TODO Auto-generated method stub 
     startActivity(new Intent(getApplication(),YourSecound Activity.class)); 
     SplashScreen.this.finish(); 
    } 


} 

public void onStart() { 

super.onStart(); 

// reset the bar to the default value of 0 

bar.setProgress(0); 

    // create a thread for updating the progress bar 

Thread background=new Thread(new Runnable() { 

public void run() { 

try { 

for (int i=0;i<5 && isRunning;i++) { 

// wait 100ms between each update 

Thread.sleep(1000); 

handler.sendMessage(handler.obtainMessage()); 

} 

} 

catch (Throwable t) { 

    }  }  }); 

isRunning=true; 

    // start the background thread 

background.start(); 

} 

public void onStop() { 

super.onStop(); 

isRunning=false; 

} 

} 

splash.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:padding="15px" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading......" /> 

<TextView 
    android:id="@+id/txt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/imageView1"/> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/splash" /> 

<ProgressBar 
    android:id="@+id/progress" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/imageView1" 
    android:layout_alignLeft="@+id/imageView1" 
    android:max="100" /> 

</RelativeLayout> 

這是我的項目的一個例子所以請在XML文件中必要的變化

+0

Thaks Ramz爲您提供幫助,這正是我所需要的,工作正常 – japuentem

+0

其確定您的解決方案請接受此答案 – Ramz