java - 显示错误消息的代码 :- error: incompatible types: Schedule_fragment cannot be converted to Fragment

标签 java android android-studio android-fragments

向分段 Activity 添加时钟时显示上列错误的代码。 显示的错误消息是 - 错误:不兼容的类型:Schedule_fragment 无法转换为 Fragment。 我对此很陌生,并且从 YouTube 教程中编写了此代码,现在尝试与我的代码合并。请帮忙。

package com.example.fastfill.ui.main;   
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;  
import com.example.fastfill.R;


public class Schedule_fragment extends Fragment implements TimePickerFragment.TimePickerListener {
    private TextView displaytime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule_fragment);
        displaytime=findViewById(R.id.displaytime);
        Button showtime=findViewById(R.id.showtime);
        showtime.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                DialogFragment timePickerFragment=new TimePickerFragment();
                timePickerFragment.show(getSupportFragmentManager(),"timePicker");
            }
        });
    }
    @Override
    public void onTimeSet(TimePicker timePicker, int hour, int minute) {
        displaytime.setText("Hour = " +hour + "Minute = "+ minute);
    }
}

最佳答案

您正在扩展AppCompatActivity,如果您想使用Schedule_fragment作为fragment,则需要扩展Fragment。

关于java - 显示错误消息的代码 :- error: incompatible types: Schedule_fragment cannot be converted to Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58486619/

相关文章:

Java 使用正则表达式来匹配测验的模式

java - 在给定时间后跳到带有线程的 for 循环中的下一个索引

java - 在java中创建方法依赖

android - 如何在小窗口打开 "zxing Barcode"扫描画面?

android - Activity 生命周期中的小构象

java - Android Studio Project的Gradle守护程序无法启动

java - 在 Groovy 脚本中执行 .jar

android - 通过android studio上传视频到Youtube

java - 应用程序不断崩溃,在android studio中编程

android - 在 com.android.build.gradle.AppExtension_Decorated 上找不到属性 'xxxx'