在Fragment类中扩展Fragment而不是FragmentActivity时出现java片段错误
我有一个主活动页面,根据用户的选择从中调用片段列表。当我在片段类中扩展Fragment时,主活动页面中的replace方法会抛出一个错误,当我在片段页面中扩展fragmentActivity时,需要(此)上下文的方法不起作用。我不确定这方面的工作是什么。如有任何协助/指导,将不胜感激
代码: 主要活动。爪哇
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
case R.id.dance:
Toast.makeText(getApplicationContext(),"Dance Selected",Toast.LENGTH_SHORT).show();
DANCE fragment_dance = new Dance();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction_dance = fragmentManager.beginTransaction();
fragmentTransaction_dancce.replace(R.id.frame,fragment_aed);
fragmentTransaction_dance.commit();
return true;
碎片。爪哇
public class DANCE extends Fragment implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
AdapterAed danceobjects;
danceobjects= new AdapterDance (this, 0, dancelisting);
}
在fragment类中存在错误,当我让它扩展fragment时引用了“this”,但当它扩展FragmentActivity时,没有错误,但是mainActivity在replace方法中存在错误,无法将该片段识别为片段。谢谢你的时间
共 (0) 个答案