在我问这个问题之前,我尝试了这里提供的所有可用选项和解决方案,但没有成功。学习使用此链接https://abhiandroid.com/ui/fragment创建片段。有两个片段类 除了MainActivity。这两个片段类不会抛出错误,但是
fragmentTransaction.replace(R.id.frameLayout, fragment);
在这种方法下。参数“ fragment”用红色下划线
private void loadFragment(Fragment fragment) {
// create a FragmentManager
FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the
//Fragment
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit(); // save the changes
}
错误消息说 第二个参数类型错误。找到所需的android.support.v4.app.Fragment android.app.Fragment
当我用鼠标悬停并且要重建时,我有这个
错误:(51,55)错误:类型不兼容:android.support.v4.app.Fragment 无法转换为android.app.Fragment
为清楚起见,我添加代码
MainActivity XML
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<!-- display two Button's and a FrameLayout to replace the Fragment's -->
<Button
android:id="@+id/firstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/button_background_color"
android:text="First Fragment"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/secondFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/button_background_color"
android:text="Second Fragment"
android:textColor="@color/white"
android:textSize="20sp" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" />
</LinearLayout>
MainActivity
打包com.example.android.fragmentexample;
import android.support.v4.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button firstFragment, secondFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new Fragment();
// get the reference of Button's
firstFragment = (Button) findViewById(R.id.firstFragment);
secondFragment = (Button) findViewById(R.id.secondFragment);
// perform setOnClickListener event on First Button
firstFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// load First Fragment
loadFragment(new FirstFragment());
}
});
// perform setOnClickListener event on Second Button
secondFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// load Second Fragment
loadFragment(new SecondFragment());
}
});
}
private void loadFragment(Fragment fragment) {
// create a FragmentManager
FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the
//Fragment
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit(); // save the changes
}
}
FirstFragment 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"
tools:context="com.example.android.fragmentexample.FirstFragment">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--TextView and Button displayed in First Fragment -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="This is First Fragment"
android:textColor="@color/black"
android:textSize="25sp" />
<Button
android:id="@+id/firstButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/green"
android:text="First Fragment"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
FirstFragment.java
package com.example.android.fragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class FirstFragment extends Fragment {
View view; Button firstButton;
// public FirstFragment() {
// // Required empty public constructor
// }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_first, container, false);
//get the reference of the FirstButton
firstButton = view.findViewById(R.id.firstButton);
//perform setOnClickListener on the firstButton
firstButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//display a message by using a toast
Toast.makeText(getActivity(), "First Fragment",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
secondFragment类和xml文件与FirstFragment类相似,并且xml文件仅在必要时才改变明暗度
答案 0 :(得分:1)
在您的MainActivity中,
只需更改这两个进口
import android.app.FragmentManager;
import android.app.FragmentTransaction;
到
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
现在可以正常工作!
答案 1 :(得分:0)
使用支持片段管理器
getSupportFragmentManager()
答案 2 :(得分:0)
在您的MainActivity的loadFragment()中使用
FragmentManager fm = getSupportFragmentManager();
代替
FragmentManager fm = getFragmentManager();