0

我是 android 新手,我想制作一个需要进度条的应用程序,但我编写的代码似乎每次运行时都会崩溃。到目前为止我无法找到错误。我正在使用垂直进度条.

提前谢谢。

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>
4

2 回答 2

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;
        }

    }
于 2012-11-09T09:21:12.960 回答
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>
于 2012-11-12T06:07:08.470 回答