gpt4 book ai didi

java - Android Studio fragment

转载 作者:行者123 更新时间:2023-12-02 03:07:54 26 4
gpt4 key购买 nike

我正在尝试创建一个带有由标志(图像按钮)组成的侧边栏的测验应用程序,当您单击每个标志时,您可以在右侧的 fragment 中输入它所属的国家/地区屏幕。但是,我在 Play.java 文件的 if 语句中遇到错误,其中显示:

fragment = new FragmentOne();

fragment = new FragmentTwo();

Play.java

import android.os.Bundle;                                                           
import android.app.FragmentManager;
import android.view.View;
import android.view.Menu;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;

import layout.FragmentOne;
import layout.FragmentTwo;


public class Play extends MainActivity {

Fragment fragment;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);

}

public void ChangeFragment(View view) {


if(view == findViewById(R.id.imageButton10)) {
fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fragment);
ft.commit();
}
if(view == findViewById(R.id.imageButton9)) {
fragment = new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fragment);
ft.commit();
}
}

}

activity_play.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/activity_play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.karolinawullum.quizapp.Play">


<LinearLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/LinearLayout1">


<TextView
android:text="Which country does this flag belong to? Press flag to answer."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="10sp" />


<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/germany"
android:id="@+id/imageButton10"
android:layout_marginBottom="20dp"
android:layout_marginTop="50dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:onClick="ChangeFragment" />

<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/greece"
android:id="@+id/imageButton9"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:onClick="ChangeFragment"
android:scaleType="fitXY"/>

<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/france"
android:id="@+id/imageButton8"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>

<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/finland"
android:id="@+id/imageButton7"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>

<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/denmark"
android:id="@+id/imageButton6"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>

<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/cyprus"
android:id="@+id/imageButton5"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>

<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/belgium"
android:id="@+id/imageButton4"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>

<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/austria"
android:id="@+id/imageButton3"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>

</LinearLayout>

<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:name="layout.FragmentOne"
android:id="@+id/fragment_place"
android:layout_weight="0.72" />


</LinearLayout>

FragmentOne.java

package layout;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentOne extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}



}

有人知道我做错了什么吗?

最佳答案

在您的 Play.java 类中,您的 Fragment 变量的类型为 android.app.Fragment。尽管您的 FragmentOne.java (我假设您的 FragmentTwo.java 也是如此)类的类型为 android.support.v4.app.Fragment

因此,在您的 Play.java 类中,您尝试使用错误类型初始化 Fragment 变量。

要么:

  • 使您的 FragmentOne.javaFragmentTwo.java 扩展 android.app.Fragment 而不是支持版本一
  • 通过更改导入,使 Play.java 中的 Fragment 变量的类型为 android.support.v4.Fragment

关于java - Android Studio fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41490375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com