//Main activity.java
package com.example.sahilnitish.easyyagriculture;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity
{
boolean status=false;
Button bn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn=(Button)findViewById(R.id.bn);
bn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(!status)
{
Fragmentone f1 =new Fragmentone();
fragmentTransaction.add(R.id.fragment_container,f1);
fragmentTransaction.commit();
bn.setText("Load Second fragment");
status=true;
}
else
{
FragmentTwo f2 = new FragmentTwo();
fragmentTransaction.add(R.id.fragment_container,f2);
fragmentTransaction.commit();
bn.setText("Load first fragment");
status=false;
}
}
});
}
}
错误:-
Error:(29,40) Error: no suitable method for add(int,Fragmentone)方法FragmentTransaction.add(Fragment,String)不适用(参数不匹配;int不能转换为Fragment)方法FragmentTransaction.add(int,Fragment)不适用(参数不匹配;Fragmentone不能转换为Fragment)
fragmentone.java
package com.example.sahilnitish.easyyagriculture;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragmentone extends Fragment
{
public Fragmentone()
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one_layout, container, false);
}
}
您正在使用带有支持片段的FragmentManager。
要么让你的MainActivity扩展ActivityCompat并使用supportFragmentManager,要么让你的Fragment扩展一个"正常"的Fragment。
//Main activity.java
package com.example.sahilnitish.easyyagriculture;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
boolean status=false;
Button bn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn=(Button)findViewById(R.id.bn);
bn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(!status)
{
Fragmentone f1 =new Fragmentone();
fragmentTransaction.add(R.id.fragment_container,f1);
fragmentTransaction.commit();
bn.setText("Load Second fragment");
status=true;
}
else
{
FragmentTwo f2 = new FragmentTwo();
fragmentTransaction.add(R.id.fragment_container,f2);
fragmentTransaction.commit();
bn.setText("Load first fragment");
status=false;
}
}
});
}
}
这是因为你使用了错误的FragmentManager。
因此,首先将import语句从 更改为:import android.app.FragmentManager;
:
import android.support.v4.app.FragmentManager;
,你也应该使用getSupportFragmentManager()
。
so change:
FragmentManager fragmentManager = getFragmentManager();
:
FragmentManager fragmentManager = getSupportFragmentManager();
您的Activity class
在您的情况下是MainActivity
,应该扩展AppCompatActivity
而不是Activity
。