Java 显示错误MSSAGE的代码:-错误:不兼容的类型:计划\u片段无法转换为片段

Java 显示错误MSSAGE的代码:-错误:不兼容的类型:计划\u片段无法转换为片段,java,android,android-studio,android-fragments,Java,Android,Android Studio,Android Fragments,向分段活动添加时钟时显示上传错误的代码。 显示的错误消息为-错误:不兼容类型:无法将计划\u片段转换为片段。 我是新手,从youtube教程中编写了这段代码,现在正试图与我的代码合并。请帮忙 package com.example.fastfill.ui.main; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;

向分段活动添加时钟时显示上传错误的代码。 显示的错误消息为-错误:不兼容类型:无法将计划\u片段转换为片段。 我是新手,从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\u fragment用作片段,则需要扩展片段。

Schedule\u fragment是一个活动,因为它扩展了活动,您需要首先将其转换为片段。您有一个扩展AppCompatActivity的类Schedule\u片段。即使您将其称为Schedule_fragment,它仍然是一个活动,因为它扩展了活动。活动不是片段的实例。我建议您在Java中使用关键字“extends”和“implements”来自学如何解决这个问题。@Traendy我已经通过entending Fragment对代码进行了编辑,但现在使用了setContentView等功能,findViewById和getSupportFragmentManager的下划线为红色,因为给出错误的错误无法解决相应的元素,正如我所说的,您应该研究这个主题,例如:在这个线程中解释片段、活动和面向对象编程是不可行的。我已经通过entending Fragment编辑了代码,但是现在这些功能setContentView、findViewById和getSupportFragmentManager带有红色下划线,因为出现错误时无法解析相应的元素