主要活动
package com.example.richu.fragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeFragment(View view){
Fragment fragment;
if(view == findViewById(R.id.button3)){
fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1,fragment);
ft.commit();
}
if(view == findViewById(R.id.button4)){
fragment = new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1,fragment);
ft.commit();
}
}
}
MainActivity.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.richu.fragment.MainActivity">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="2dp"
tools:layout_editor_absoluteY="5dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="292dp"
tools:layout_editor_absoluteY="5dp" />
<fragment
android:id="@+id/fragment1"
android:name="layout.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="13dp"
tools:layout_editor_absoluteY="13dp" />
</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;
import com.example.richu.fragment.R;
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);
}
}
FragmentOne.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
tools:context="layout.FragmentOne">
</FrameLayout>
FragmentTwo.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;
import com.example.richu.fragment.R;
public class FragmentTwo 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_two, container, false);
}
}
FragmentTwo.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
tools:context="layout.FragmentTwo">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Hello" />
</FrameLayout>
我试图学习片段基础知识。刚在活动中添加了两个按钮。单击一个按钮时,应加载FragmentOne,单击第二个按钮时,FragmentTwo应加载。
MainActivity.java
中的以下代码显示错误,错误是不兼容的类型。 FragmentOne
和FragmentTwo
是我创建的两个片段。
- fragment = new FragmentOne();
- fragment = new FragmentTwo();
请找到解决方案。谢谢。
答案 0 :(得分:1)
在主要活动中,您使用import android.app.Fragment;
FragmentTwo和FragmentOne中的你使用import android.support.v4.app.Fragment;
您必须决定使用版本更适合您。
链接显示差异: Difference between android.app.Fragment and android.support.v4.app.Fragment
答案 1 :(得分:0)
问题是objFragment和fragmentManager之间的片段类型不匹配。第一个来自包android.support.v4.app
,而第二个来自android.app package
。要解决此问题,请将getFragmentManager()
更改为getSupportFragmentManager()
。只需替换代码行
FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction();
到
android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();