2017-03-04 86 views
-1

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

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

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

请任何帮助appriciated。

这是我的代码

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之外,其他所有的东西都在工作。谢谢

+0

顺便说一句,不要使用字符串数组。做一个实际的'Module'类。 https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView#using-a-custom-arrayadapter –

+0

@ cricket_007,铸造FragmentActivity为我工作。非常感谢,也感谢其他人。我也将采取此建议 – devloper2009

回答

0

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

您可以在该Adapter的构造函数中添加一个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()

+0

铸造FragmentActivity为我工作 – devloper2009

+0

它会工作,但不建议使您的适配器依赖明确的FragmentActivity –

+0

谢谢,这是一个更好的解决方案。我删除了铸件并使用这种方法。你是一个安全的生活 – devloper2009

相关问题