gpt4 book ai didi

android - 进度条 Activity 强制关闭

转载 作者:行者123 更新时间:2023-11-29 16:08:46 25 4
gpt4 key购买 nike

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

这是我尝试过的方法,当我运行它时让 Activity 强制关闭

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

最佳答案

改变

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

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

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

关于android - 进度条 Activity 强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15416716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com