Java 创建一个简单的应用程序,可以';我没法让清除按钮工作

Java 创建一个简单的应用程序,可以';我没法让清除按钮工作,java,android,Java,Android,所以,我试着跟随这个网站的教程 我做了第一部分,在那里我得到了文本框和一个发送按钮。我可以在文本框中输入文本,然后单击“发送”。但文本框的文本仍保留在那里。它不会消失。因此,本教程的下一部分是如何使文本消失。我正试着跟着做,但我不能让它工作 这是我的密码 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmln

所以,我试着跟随这个网站的教程

我做了第一部分,在那里我得到了文本框和一个发送按钮。我可以在文本框中输入文本,然后单击“发送”。但文本框的文本仍保留在那里。它不会消失。因此,本教程的下一部分是如何使文本消失。我正试着跟着做,但我不能让它工作

这是我的密码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <EditText
        android:id="@+id/editText"
        android:layout_width="183dp"
        android:layout_height="46dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="@string/edit_message"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toStartOf="@+id/button_clear"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="@string/button_send"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/editText"
        tools:layout_editor_absoluteY="14dp" />
</androidx.constraintlayout.widget.ConstraintLayout>


strings.xml

<resources>
    <string name="app_name">The Most Useless App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
</resources>


MainActivity.xml

package com.example.myfirstapp;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text
        TextView textView = findViewById(R.id.textView);
        textView.setText(message);



    }


}
我犯的错误

无法解析符号textView

无法解析符号活动\u显示\u消息

无法解析符号额外\u消息

活动com.example.myfirstapp.main未在中注册 舱单


任何帮助都将不胜感激。

是一个设置问题,您应该做的第一件事是 要进行干净构建,您可以在android studio上方选择选项
buld
,然后选择
clean build
。如果不起作用,请发布
build.gradle的输出

还有一种可能性是,您刚刚开始使用android,您不知道有时需要导入所需的库

将鼠标指向
MainActivity
,如果您在windows中,请按alt+enter,然后选择将
Activity
添加到
AndroidManifest
,然后执行干净的生成

我建议您使用向导构建一个新项目,您可以转到Android studio并选择file new project并跟踪该项目,这样MainActivity将自动添加到您的清单中

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myfirstapp">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".DisplayMessageActivity"
                android:parentActivityName=".MainActivity">
                <!-- The meta-data tag is required if you support API level 15 and lower -->
                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value=".MainActivity" />
            </activity>

        </application>

    </manifest>
    package com.example.myfirstapp;

    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;


    public class MainActivity extends AppCompatActivity {



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_display_message);

            // Get the Intent that started this activity and extract the string
            Intent intent = getIntent();
            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

            // Capture the layout's TextView and set the string as its text
            TextView textView = findViewById(R.id.textView);
            textView.setText(message);



        }


    }