私はアンドロイドが初めてで、いくつかのチュートリアルを試しています。プログレスバーの処理に関する簡単なチュートリアルに出会いました。私の最小 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