असंगत प्रकार: android.view.View to androidtutorial.project.nightclock.Classes.TextNlock

Oct 06 2020

मेरे पास लेआउट में एक पाठ घड़ी है जिसे मैं यहां आरंभ करता हूं लेकिन प्रारंभ के दौरान, मेरे पास (Inconvertible प्रकार: android.view.View से androidtutorial.project.nightclock.Classes.TextClock) का संदेश है। जिस लाइन पर मैं नीचे उल्लेख करता हूं, कोई भी मेरी समस्या का हल नहीं करता है जो स्टैकऑवरफ्लो पर मौजूद है, इसलिए कृपया मुझे कोई भी सही करे।

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>