Java 类型参数t具有不兼容的上限:视图和片段
我想获取我的活动片段,但我得到了错误: 类型参数t具有不兼容的上限:视图和片段 通过我的主要活动中的Java 类型参数t具有不兼容的上限:视图和片段,java,android,android-fragments,fragment,Java,Android,Android Fragments,Fragment,我想获取我的活动片段,但我得到了错误: 类型参数t具有不兼容的上限:视图和片段 通过我的主要活动中的findViewById(R.id.f_指令)。 我希望你能帮助我 我的活动: public class MainActivity extends AppCompatActivity { Fragment f_instruction; public static void closeFragment() { } @Override protected void onCreate(Bundle sa
findViewById(R.id.f_指令)
。
我希望你能帮助我
我的活动:
public class MainActivity extends AppCompatActivity {
Fragment f_instruction;
public static void closeFragment() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f_instruction = findViewById(R.id.f_instruction);
}
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/f_instruction"
android:name="e.marco.gymdiary.first_fragment"
android:layout_width="410dp"
android:layout_height="732dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
}
活动的XML格式:
public class MainActivity extends AppCompatActivity {
Fragment f_instruction;
public static void closeFragment() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f_instruction = findViewById(R.id.f_instruction);
}
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/f_instruction"
android:name="e.marco.gymdiary.first_fragment"
android:layout_width="410dp"
android:layout_height="732dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我也有同样的问题。这帮助我解决了问题:
无法通过getViewById()方法获取片段。正确的方法是: 如果您正在进行活动:
Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.your_fragment_id);
如果您在片段中:
Fragment fragment=getChildFragmentManager().findFragmentById(R.id.your_fragment_id);
另外,这是如何使用上下文获取片段管理器(例如,如果您在适配器中,这很有用)