简体   繁体   中英

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.

The sequence turns this input into a string then an int. The int is added to the progress of a status bar (which is used only as a graphical representation of their progress).

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

Ps If you're getting errors its always useful to include the stack trace alongside your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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