简体   繁体   中英

Inconvertible type: android.view.View to androidtutorial.project.nightclock.Classes.TextClock

I have a text clock in the layout that I initialize here but during initialization, I have a message of (Inconvertible type: android.view.View to androidtutorial.project.nightclock.Classes.TextClock). On line which I mention below No one solves my problem which is present on StackOverflow so please anyone corrects me.

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 file

<?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>

I have done a change to your xml code. Hopefully it will work

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM