英文:
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("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>
答案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">
<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>
英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论