简体   繁体   English

Android:使用用户输入填充水平进度条

[英]Android: Filling a horizontal progress bar with user input

Ok so my program is supposed to help a user keep track of the progress they have made at any particular task. 好的,所以我的程序应该可以帮助用户跟踪他们在任何特定任务上所取得的进展。

It has an <EditText> view that will ask a user to input a number, the program has an OnClickListener() that will activate a sequence of code once the user clicks a button to add the numerical input. 它具有一个<EditText>视图,该视图将要求用户输入数字,该程序具有一个OnClickListener() ,一旦用户单击按钮以添加数字输入,它将激活一系列代码。

The sequence turns this input into a string then an int. 该序列将此输入转换为字符串,然后转换为int。 The int is added to the progress of a status bar (which is used only as a graphical representation of their progress). 将int添加到状态栏的进度(仅用作其进度的图形表示)。

I am getting no errors on Netbeans, and nothing seems to be intuitively wrong with what I wrote, but the app crashes every time I try to run it.This is my first time attempting progress bars so I might just be missing something I'm not seeing. 我在Netbeans上没有遇到任何错误,并且我写的内容似乎没有任何直觉上的错误,但是每次我尝试运行它时该应用程序都会崩溃。这是我第一次尝试进度条,所以我可能只是缺少了一些东西没有看到。 Thanks for any help you are able to provide! 感谢您提供的任何帮助! I really appreciate it! 我真的很感激!

package com.example.progresstracker;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    Button add;
    ProgressBar mProgress;


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

        mProgress.setProgress(0);
        mProgress.setMax(100);
        add = (Button) findViewById(R.id.bAdd);
        mProgress = (ProgressBar) findViewById(R.id.prog);


        add.setOnClickListener(new View.OnClickListener() {

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

                 EditText eText = (EditText) findViewById(R.id.edit);
                 String message = eText.getText().toString();
                 int num = Integer.parseInt(message);


                 mProgress.incrementProgressBy(num);



            }
        });



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Try putting this line 尝试把这条线

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

Before this line 在这行之前

mProgress.setProgress(0);

You need to initialise mProgress before you can start calling its methods. 您需要先初始化mProgress,然后才能开始调用其方法。

Ps If you're getting errors its always useful to include the stack trace alongside your code. Ps如果遇到错误,将堆栈跟踪与代码一起包括总是有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM