我是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);
}
}
logcat的
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
答案 0 :(得分:4)
变化
bar = (ProgressBar)findViewById(android.R.id.progress);
到
bar = (ProgressBar)findViewById(R.id.progress);
目前您正在尝试访问android.R.id.progress