使用Handler更新progressBar时出现NullPointerException

时间:2016-03-05 05:10:07

标签: java android

public class MainActivity extends Activity {

    Button button = (Button)findViewById(R.id.btn);
    ProgressBar bar = (ProgressBar)findViewById(R.id.pbar);

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


        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                updateHandler.post(updateThread);
            }
        });

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


     Handler updateHandler = new Handler(){

        public void handleMessage(Message msg) {
            bar.setProgress(msg.arg1);
            updateHandler.post(updateThread);
        };
    };

    Runnable updateThread  = new Runnable() {

        int i = 0;
        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println("------------");
            i = i+10;
            Message msg = updateHandler.obtainMessage();
            msg.arg1 = i;

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            updateHandler.sendMessage(msg);
            if (i==100) {
                updateHandler.removeCallbacks(updateThread);
            }
        }
    };


}

logcat的:

  

java.lang.RuntimeException:无法实例化活动ComponentInfo {com.zxy.handlerpbtest / com.zxy.handlerpbtest.MainActivity}:java.lang.NullPointerException

3 个答案:

答案 0 :(得分:0)

这一行

ProgressBar bar = (ProgressBar)findViewById(R.id.pbar);
Button button = (Button)findViewById(R.id.btn);

应该在你的onCreate方法

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

    ProgressBar bar = (ProgressBar)findViewById(R.id.pbar);
    Button button = (Button)findViewById(R.id.btn);
}

答案 1 :(得分:0)

您可以重写此代码行:

ProgressBar bar = (ProgressBar)findViewById(R.id.pbar);
Button button = (Button)findViewById(R.id.btn);

如:

public class MainActivity extends Activity {

    Button button ;
    ProgressBar bar ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bar = (ProgressBar)findViewById(R.id.pbar);
        button = (Button)findViewById(R.id.btn);
.....
}}

然后你不会得到空指针异常。

答案 2 :(得分:0)

这是完整的解决方案:

public class MainActivity extends Activity {

    Button button;
    ProgressBar bar;

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

        button = (Button) findViewById(R.id.btn);
        bar = (ProgressBar) findViewById(R.id.pbar);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                updateHandler.post(updateThread);
            }
        });

    }


    Handler updateHandler = new Handler() {

        public void handleMessage(Message msg) {
            bar.setProgress(msg.arg1);
            updateHandler.post(updateThread);
        }
    };

    Runnable updateThread = new Runnable() {

        int i = 0;

        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println("------------");
            i = i + 10;
            Message msg = updateHandler.obtainMessage();
            msg.arg1 = i;

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            updateHandler.sendMessage(msg);
            if (i == 100) {
                updateHandler.removeCallbacks(updateThread);
            }
        }
    };

}