1
我是新來的android和嘗試一些教程。我遇到了處理進度條的簡單教程。我最小的SDK是11progressbar活動力量關閉
以下是我已經嘗試並獲得活動,部隊密切當我運行它
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
的logcat的是那麼清楚,'com.example.progrss.HandlerDemo.onStart(HandlerDemo.java:41)'。行號41有一個空指針異常。同樣,當你提出這樣的問題時,你應該放在哪一行是41. – Siddharth 2013-04-22 10:58:36
@Siddharth:我一定會記住 – 2013-04-22 11:00:44
請在第41行編輯問題。請保持乾淨。 – Siddharth 2013-04-22 11:02:11