2017-01-16 139 views
0

我在Android中创建了一个进度条,但它一点也不工作。我究竟做错了什么?我刚开始学习android。以下是我的代码。Android进度条无法正常工作

MainActivity.java-

public class MainActivity extends AppCompatActivity { 
    ProgressBar progressBar; 
    int mporgress=0; 
    EditText time; 
    private Handler mHandler = new Handler(); 
    private static final int PROGRESS = 0x1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 
      public void Startbuttononclick(View view){ 
       Button startbutton=(Button) findViewById(R.id.button); 
       startbutton.setOnClickListener(new View.OnClickListener(){ 
        @Override 
        public void onClick(View v) { 

         mporgress = Integer.parseInt(time.getText().toString()); 
         progressBar.setProgress(mporgress); 

         new Thread(new Runnable() { 
          public void run() { 
           while (mporgress < 100) { 
            // Update the progress bar 
            mHandler.post(new Runnable() { 
             public void run() { 
              progressBar.setProgress(mporgress); 
              mporgress = doWork(); 
              try{ 
               Thread.sleep(1000); 
              }catch (InterruptedException e){} 
             } 
            }); 
           } 
          } 
         }).start(); 

        } 
       }); 
      } 
    public void doprogress(View view) { 

    } 
     public int doWork(){ 
        mporgress++; 
      return mporgress; 
     } 
} 

Activity_main.xml-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.android.progressbar.MainActivity"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Enter the time:" 
     android:inputType="number" 
     android:id="@+id/editText" /> 

    <ProgressBar 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="500dp" 
     android:layout_height="200dp" 
     android:id="@+id/progressBar4" 
     android:layout_gravity="center" 
     android:scrollbarSize="500dp" /> 

    <Button 
     android:text="START" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" 
     android:layout_gravity="center" 
     android:onClick="Startbuttononclick" /> 
</LinearLayout> 

看看这个图片如下─ - https://i.stack.imgur.com/2TBIb.png

注意 - 此图像中输入时间为一个EditText提示而不是TextView。

它出现,但不工作。

我有这么far.- https://i.stack.imgur.com/UujpB.png

但还是进度条仍然连续旋转完成这些变化。

+0

定义 “完全没有工作”。根本不显示?没有更新?还有其他的东西吗? –

+0

@GabeSechan我已经更新了这个问题。请检查它。 – Knattic

+0

我想,它是因为你正在MainThread上调用睡眠。尝试将睡眠呼叫转移到外线程。 –

回答

1

好像你忘了初始化EditText -timeProgressBar。 INIT这里面onCreate

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    progressBar =(ProgressBar) findViewById(R.id.progressBar4); 
    time = (EditText) findViewById(R.id.editText); //here 
} 

也在里面按钮单击bar

progressBar = new ProgressBar(this); 
mporgress = Integer.parseInt(time.getText().toString()); 
    ................ 
      ............. 
+0

该应用程序仍在崩溃。和下面是对数显示java.lang.NullPointerException:尝试@Knattic需要初始化经由findViewById每个视图调用虚拟方法“无效android.widget.ProgressBar.setProgress(INT)”上的空对象引用 – Knattic

+0

。当你调用setContentView时,它们不会神奇地填充。 –

+0

对不起,我忘了这么做。 – Knattic