2012-11-09 96 views
0

我是新來的android,我想做一個應用程序需要一個進度條,但我寫的代碼似乎每次運行時都會崩潰。直到現在,我無法找到錯誤。我正在使用垂直進度條。進度條不工作

thanx提前。

package com.example.progtask; 

import android.app.Activity; 

import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ProgressBar; 

public class MainActivity extends Activity implements OnClickListener, Runnable { 
    private ProgressBar pb; 
    private Button show; 
    private Button hide; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     pb = (ProgressBar) findViewById(R.id.progressBar1); 
     show = (Button) findViewById(R.id.button1); 
     hide = (Button) findViewById(R.id.hide); 
     // pb.setVisibility(ProgressBar.GONE); 
     show.setOnClickListener(this); 
     hide.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if (v.equals(show)) { 

      pb.setVisibility(ProgressBar.VISIBLE); 
      pb.setProgress(0); 
      pb.setMax(12); 
      new Thread(this).start(); 
     } else { 
      pb.setVisibility(ProgressBar.GONE); 
     } 

    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     int n = 0; 
     while (n < 10) { 
      n++; 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     pb.setProgress(n); 

    } 

} 

下面是XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:padding="@dimen/padding_medium" 
     android:text="picelate" 
     tools:context=".MainActivity" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="82dp" 
     android:layout_toLeftOf="@+id/textView1" 
     android:text="show" /> 

    <Button 
     android:id="@+id/hide" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_toRightOf="@+id/textView1" 
     android:text="hide" /> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView1" 
     android:layout_alignParentLeft="true" /> 

</RelativeLayout> 
+1

打印您的日誌貓錯誤消息,並在這裏發佈。 – subodh

+0

@ subodh.java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.asynctask/com.example.asynctask.MainActivity}:java.lang.ClassCastException:android.widget.Button無法轉換爲android.widget。 ProgressBar – Abx

+0

它表示您正試圖將按鈕投射到** ProgressBar **中。查看您的佈局** activity_main **,它可能是您的按鈕的複製粘貼,並且名稱僅爲progressBar1。 – subodh

回答

0

這是它的工作原理:

public class MainActivity extends Activity implements OnClickListener, Runnable { 

    private ProgressBar pb; 
    private Button show; 
    private Button hide; 
int n; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     pb = (ProgressBar) findViewById(R.id.progressBar1); 
     show = (Button) findViewById(R.id.button1); 
     hide = (Button) findViewById(R.id.button2); 
     // pb.setVisibility(ProgressBar.GONE); 
     show.setOnClickListener(this); 
     hide.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch (v.getId()) 
      { 
       case R.id.button1: 

       pb.setVisibility(ProgressBar.VISIBLE); 
       pb.setProgress(0); 
       pb.setMax(100); 
       n = 0; 
       new Thread(this).start(); 
       break; 

       case R.id.button2: 
      // do the needful. 
        pb.setVisibility(ProgressBar.GONE); 
      break; 
      } 
    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     int n = 0; 
     while (n < 100) { 
      n++; 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }pb.setProgress(n*10); 

     } 
     pb.setProgress(n*10); 

    } 


} 

XML文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

     <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="show" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:text="hide" /> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:progressDrawable="@drawable/progbar" 
     android:layout_gravity="bottom" 
     /> 

</LinearLayout> 
2

試試這個

 @Override 
     public void onClick(View v) { 
      switch (v.getId()) 
     { 
      case R.id.button1: 

      pb.setVisibility(ProgressBar.VISIBLE); 
      pb.setProgress(0); 
      pb.setMax(12); 
      new Thread(this).start(); 
      break; 

      case R.id.hide: 
     // do the needful. 
     break; 
     } 

    } 
+0

Thanx subodh,我在xml文件中做了一些改變,現在它的工作完美了.....我將相對佈局更改爲線性佈局 – Abx