无法转换的类型:android.view.View 到 androidtutorial.project.nightclock.Classes.TextClock

huangapple go评论102阅读模式
英文:

Inconvertible type: android.view.View to 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 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 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(&quot;EEE, MMM d, &#39;&#39;yy&quot;);
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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@color/blackColor&quot;
tools:context=&quot;.Activities.AlwayOnDisplay&quot;&gt;
&lt;TextClock
android:id=&quot;@+id/textClock&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:fontFamily=&quot;@font/adamina&quot;
android:textStyle=&quot;bold&quot;
android:textSize=&quot;25sp&quot;
android:gravity=&quot;center&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:fontFamily=&quot;@font/adamina&quot;
android:gravity=&quot;center&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/textClock&quot;
android:id=&quot;@+id/date&quot;
/&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 0

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

&lt;androidtutorial.project.nightclock.Classes.TextClock
android:id=&quot;@+id/textClock&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:fontFamily=&quot;@font/adamina&quot;
android:gravity=&quot;center&quot;
android:textSize=&quot;25sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/date&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:fontFamily=&quot;@font/adamina&quot;
android:gravity=&quot;center&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/textClock&quot; /&gt;

</androidx.constraintlayout.widget.ConstraintLayout>

英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@color/blackColor&quot;
tools:context=&quot;.Activities.AlwayOnDisplay&quot;&gt;
&lt;androidtutorial.project.nightclock.Classes.TextClock
android:id=&quot;@+id/textClock&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:fontFamily=&quot;@font/adamina&quot;
android:gravity=&quot;center&quot;
android:textSize=&quot;25sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/date&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:fontFamily=&quot;@font/adamina&quot;
android:gravity=&quot;center&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/textClock&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

huangapple
  • 本文由 发表于 2020年10月6日 19:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64225288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定