我想每隔1秒刷新一次TextView,我尝试了一些Code但是做了很多工作。 这是一个循环,它将值放在文本中。
public void buttonOnClick(View v) throws InterruptedException {
Button button=(Button) v;
String t;
boolean delivered =true;
boolean sent = true;
DecimalFormat tiki = new DecimalFormat("00");
txt = (TextView) this.findViewById(R.id.textv2);
message = (EditText) findViewById(R.id.msg1);
for(int i = 0; i <nos.length; i++) {
for(int j = 0; j <3; j++) {
t=(tiki.format(j));
text = nos[i] + t;
Thread.sleep(2000);
}
}
}
我尝试将此代码添加到onCreate(Bundle savedInstanceState)但在Simulater中运行应用程序后,应用程序剂量运行(运行前将关闭)! 1.
final Handler handler = new Handler();
handler.post( new Runnable(){
private int k = 0;
public void run() {
final TextView progess = (TextView)findViewById(R.id.progress);
progess.setText(String.valueOf(k) + "%");
k++;
if( k <= 100 )
{
// Here `this` refers to the anonymous `Runnable`
handler.postDelayed(this, 15);
}
}
});
2。
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
debugView.setText("something");
}
});
}
}, 0, 100);
3
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
txt.append(num);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
}
Logcat错误:
01-30 14:25:34.007 974-974/com.example.zaniar.mobis I/Process﹕ Sending signal. PID: 974 SIG: 9
01-30 14:27:03.217 1025-1025/com.example.zaniar.mobis D/AndroidRuntime﹕ Shutting down VM
01-30 14:27:03.217 1025-1025/com.example.zaniar.mobis W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-30 14:27:03.267 1025-1025/com.example.zaniar.mobis E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.zaniar.mobis/com.example.zaniar.mobis.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
at android.app.ActivityThread.access$600(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1794)
at com.example.zaniar.mobis.MainActivity.<init>(MainActivity.java:41)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
at android.app.ActivityThread.access$600(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
01-30 14:27:07.608 1025-1025/com.example.zaniar.mobis I/Process﹕ Sending signal. PID: 1025 SIG: 9
importanter是,我可以刷新TextView和方法并不重要 此致
答案 0 :(得分:0)
你的第二次尝试应该有效。是否尝试将"100"
更改为"1000"
(100 = 0,1秒,1000 = 1秒)。