변환 불가능한 유형 : android.view.View to androidtutorial.project.nightclock.Classes.TextClock

Oct 06 2020

여기에서 초기화하는 레이아웃에 텍스트 시계가 있지만 초기화 중에 (변환 불가능한 유형 : android.view.View to androidtutorial.project.nightclock.Classes.TextClock) 메시지가 있습니다. 내가 아래에 언급 한 줄에 아무도 StackOverflow에 존재하는 내 문제를 해결하지 않으므로 누구든지 나를 수정하십시오.

package androidtutorial.project.nightclock.Activities;
    import android.app.admin.DevicePolicyManager;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.TextView;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    import androidtutorial.project.nightclock.Classes.TextClock;
    import androidtutorial.project.nightclock.R;
    
    public class AlwayOnDisplay extends AppCompatActivity {
        private TextView dateTimeDisplay;
        private SimpleDateFormat dateFormat;
        private String date;
        private TextClock textClock;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Full flag screen
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.activity_alway_on_display);
            textClock=(TextClock)findViewById(R.id.textClock);// error on this line
            Calendar calendar=Calendar.getInstance();
            dateTimeDisplay=(TextView)findViewById(R.id.date);
            dateFormat = new SimpleDateFormat("EEE, MMM d, ''yy");
            date = dateFormat.format(calendar.getTime());
            dateTimeDisplay.setText(date);
    
    
        }
    
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            WindowManager wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
            //Lock device
            DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        }
    }

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"
    android:background="@color/blackColor"
    tools:context=".Activities.AlwayOnDisplay">

    <TextClock
        android:id="@+id/textClock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/adamina"
        android:textStyle="bold"
        android:textSize="25sp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/adamina"
        android:gravity="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textClock"
        android:id="@+id/date"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

답변

1 Imadkhan Oct 26 2020 at 16:25

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"
    android:background="@color/blackColor"
    tools:context=".Activities.AlwayOnDisplay">

    <androidtutorial.project.nightclock.Classes.TextClock
        android:id="@+id/textClock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/adamina"
        android:gravity="center"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/adamina"
        android:gravity="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textClock" />

</androidx.constraintlayout.widget.ConstraintLayout>