1

我是 android 新手,正在尝试一些教程。我遇到了处理进度条的简单教程。我的最小 SDK 是 11。

这是我尝试过并在运行时强制关闭活动的方法

public class HandlerDemo extends Activity {

ProgressBar bar;
Handler handler = new Handler() {
    @Override
    public void handleMessage(){    
        bar.incrementProgressBy(5);
    }

    @Override
    public void close() {
    }

    @Override
    public void flush() {
    }

    @Override
    public void publish(LogRecord arg0) {
    }
};

AtomicBoolean isRunning = new AtomicBoolean(false);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_handler_demo);
    bar = (ProgressBar)findViewById(android.R.id.progress);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_handler_demo, menu);
    return true;
}

public void onStart() {
    super.onStart();
    bar.setProgress(0);

    Thread background = new Thread(new Runnable() {

        @Override
        public void run() {
            try{
                for(int i=0;i<20 && isRunning.get();i++){
                        Thread.sleep(1000);
                        handler.sendMessage(handler.obtainMessage());
                }
            }
            catch(Throwable t){
            }
        }
    });
    isRunning.set(true);
    background.start();
}

public void onStop(){
    super.onStop();
    isRunning.set(false);
}
}

日志猫

03-14 23:18:51.408: E/AndroidRuntime(717): FATAL EXCEPTION: main
03-14 23:18:51.408: E/AndroidRuntime(717): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.progrss/com.example.progrss.HandlerDemo}: java.lang.NullPointerException
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.os.Looper.loop(Looper.java:126)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.app.ActivityThread.main(ActivityThread.java:3997)
03-14 23:18:51.408: E/AndroidRuntime(717):  at java.lang.reflect.Method.invokeNative(Native Method)
03-14 23:18:51.408: E/AndroidRuntime(717):  at java.lang.reflect.Method.invoke(Method.java:491)
03-14 23:18:51.408: E/AndroidRuntime(717):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-14 23:18:51.408: E/AndroidRuntime(717):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-14 23:18:51.408: E/AndroidRuntime(717):  at dalvik.system.NativeStart.main(Native Method)
03-14 23:18:51.408: E/AndroidRuntime(717): Caused by: java.lang.NullPointerException
03-14 23:18:51.408: E/AndroidRuntime(717):  at com.example.progrss.HandlerDemo.onStart(HandlerDemo.java:41)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1132)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.app.Activity.performStart(Activity.java:4320)
03-14 23:18:51.408: E/AndroidRuntime(717):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
03-14 23:18:51.408: E/AndroidRuntime(717):  ... 11 more
4

1 回答 1

4

改变

bar = (ProgressBar)findViewById(android.R.id.progress);

bar = (ProgressBar)findViewById(R.id.progress);

目前您正在尝试访问 android.R.id.progress

于 2013-03-14T17:58:53.233 回答