0

我想每 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) 但是在模拟器中运行应用程序后,应用程序会运行(在运行之前将关闭)!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();
}

日志错误:

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

更重要的是,我可以刷新 TextView 和方法不重要问候

4

1 回答 1

0

您的第二次尝试应该有效。是否尝试更改"100""1000"

(100 = 0,1 秒,1000 = 1 秒)。

于 2015-01-30T14:49:36.710 回答