2 qq 32793555 qq_32793555 于 2017.09.17 11:18 提问

android 无法转换为android.support.v4.app.Fragment

package com.example.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
    replaceFragment(new RightFragment());
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button:
            replaceFragment(new AnotherRightFragment());
            break;
        default:
            break;
    }
}
private void replaceFragment(Fragment fragment){
    FragmentManager fragmentMananger = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentMananger.beginTransaction();
    transaction.replace(R.id.right_fragment,fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

}

当导入V4包时图片说明

当导入app.Fragment包时图片说明

两者不能兼容啊,这程序好像没法执行

2个回答

caozhy
caozhy   Ds   Rxr 2017.09.17 11:26
已采纳
qq_32793555
qq_32793555 谢谢啦,我知道了,全部都改成V4包就可以
2 个月之前 回复
qq_36692282
qq_36692282   2017.09.27 14:41

继承FragmentActivity

Csdn user default icon
上传中...
上传图片
插入图片