不兼容的类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager

时间:2017-03-04 15:50:18

标签: java android android-fragments

我一直收到错误:错误:(103,92)错误:不兼容的类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager

我已经在stackoverflow上阅读了关于此主题的所有可用帖子。 问题是我试图从Fragment和Activity都没有扩展的类中打开一个片段。

这是一个我从数据库中填充listview的类。

请,任何帮助都应该得到满足。

这是我的代码

AllModuleRequestList

package ng.com.lucidminds.isense.BackgroundTasks;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import ng.com.lucidminds.isense.R;
import ng.com.lucidminds.isense.SensorFragment;



public class AllModulesRequestList extends ArrayAdapter<String> {
private String[] ModuleID;
private String[] ModuleName;
private String[] SensorNo;
public static final String SELECTED_MODULE = "SELECTED_MODULE";


private Activity context;

public AllModulesRequestList(Activity context, String[] ModuleID, String[] ModuleName, String[] SensorNo) {
    super(context, R.layout.fragment_allmodules_layout, ModuleID);
    this.context = context;
    this.ModuleID = ModuleID;
    this.ModuleName = ModuleName;
    this.SensorNo = SensorNo;


}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.fragment_allmodules_layout, null, true);


    final int PositionValue = position;
    final String UserModuleID = ModuleID[position];
    final String UserModuleName = ModuleName[position];

    TextView textModuleID = (TextView) listViewItem.findViewById(R.id.textModuleID);
    textModuleID.setText(ModuleID[position]);

    TextView textModuleName = (TextView) listViewItem.findViewById(R.id.textModuleName);
    textModuleName.setText(ModuleName[position]);

    TextView textSensorNo = (TextView) listViewItem.findViewById(R.id.textSensorNo);
    String TextValue = " Registered Sensors";
    textSensorNo.setText("( " + SensorNo[position] + TextValue + " )");


    ImageView imageview = (ImageView) listViewItem.findViewById(R.id.imageview);


    final Activity activity = (Activity) context;



    try {
        imageview.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                switch (v.getId()) {
                    case R.id.imageview:

                        PopupMenu popup = new PopupMenu(context, v);
                        popup.getMenuInflater().inflate(R.menu.menu_fragments_list,
                                popup.getMenu());
                        popup.show();
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                //  Activity activity = ((context) context()).getCurrentActivity();

                                // Bundle bundle = new Bundle();
                                //  FragmentFunctions fragmentFunctions = new FragmentFunctions();

                                // AllModules_Fragment allModules_fragment = new AllModules_Fragment();

                                switch (item.getItemId()) {
                                    case R.id.action_reportOverview:

                                        //Or Some other code you want to put here.. This is just an example.
                                        Toast.makeText(context, " Install Clicked at position " + " : " + PositionValue, Toast.LENGTH_LONG).show();
                                        //  allModules_fragment.getoFragment(UserModuleID);

                                        Bundle bundle = new Bundle();
                                        bundle.putString(SELECTED_MODULE, UserModuleID);
                                        Fragment fr;
                                        FragmentManager fm = (activity).getFragmentManager();
                                        FragmentTransaction fragmentTransaction = fm.beginTransaction();


                                        bundle.putString(SELECTED_MODULE, UserModuleID);
                                        fr = new SensorFragment();
                                        fr.setArguments(bundle);
                                        fragmentTransaction.replace(R.id.container_body, fr);
                                        fragmentTransaction.addToBackStack(null);
                                        fragmentTransaction.commit();

                                        break;
                                    case R.id.action_addSensor:

                                        Toast.makeText(context, "Add to Wish List Clicked at position " + " : " + PositionValue, Toast.LENGTH_LONG).show();

                                        break;

                                    default:
                                        break;
                                }

                                return true;
                            }
                        });

                        break;

                    default:
                        break;
                }


            }
        });

    } catch (Exception e) {

        e.printStackTrace();
    }


    return listViewItem;
}




}

请帮助。除了FragmentManager之外,其他所有工作都在工作。感谢

1 个答案:

答案 0 :(得分:0)

你不应该致电getFragmentManager()。 1)那不是支持片段2)你应该只从Activity类调用它,而不是在适配器中调用它。

您可以在该适配器的构造函数中添加android.support.v4.app.FragmentManager,这可能是获得所需内容的最简单方法。

public AllModulesRequestList(Context context, FragmentManager fm, .... ) {
    super(context, R.layout.fragment_allmodules_layout, ModuleID);
    this.context = context;
    this.fm = fm;
    .... 

从适配器的创建开始,传入getSupportFragmentManager()