Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在处理程序类中发送时间戳?_Android - Fatal编程技术网

Android 如何在处理程序类中发送时间戳?

Android 如何在处理程序类中发送时间戳?,android,Android,我已经创建了一个处理程序,在其中我已经解析了时间戳,但是我得到了一个错误 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 我想这是因为我在xml中定义了textview,我不知道如何放置textview。有谁能帮我解释为什么我会出现这个错误,因为我只在run()中放置了text

我已经创建了一个处理程序,在其中我已经解析了时间戳,但是我得到了一个错误

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
我想这是因为我在xml中定义了textview,我不知道如何放置textview。有谁能帮我解释为什么我会出现这个错误,因为我只在run()中放置了textview

主要活动

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                notificationDialog();
                break;
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void notificationDialog() {
        mHandler=new Handler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
                    String format = simpleDateFormat.format(new Date());

                    TextView textView = findViewById(R.id.date);
                    textView.setText(format);

            }
        }).start();
    }
}
XML文件

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id = "@+id/button"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text = "Click"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintLeft_toLeftOf = "parent"
        app:layout_constraintRight_toRightOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />


</LinearLayout>
LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto"
xmlns:tools=”http://schemas.android.com/tools"
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”
工具:context=“.MainActivity”>

您不能从其他线程更改UI。Android提供了几种方式来传达对UI线程的更改:

为了解决这个问题,Android提供了几种从其他线程访问UI线程的方法。下面列出了一些可以提供帮助的方法:

Activity.runOnUiThread(可运行)

View.post(可运行)

View.postDelayed(可运行,长)

一种快速完成您正在尝试的任务的方法是使用Activity.RunUnuithRead(Runnable)

变量
format
需要声明为final,才能在内部Runnable中使用。 我们创建一个新的runnable,并告诉活动在UiThread中运行它


对于更复杂的场景,可以使用该类

对于如此简单的代码,为什么要使用
线程
?我想定期发送此消息
private void notificationDialog() {
        Handler mHandler=new Handler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
                final String format = simpleDateFormat.format(new Date());

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView textView = findViewById(R.id.date);
                        textView.setText(format);     
                    }
                });

            }
        }).start();
    }