出错有两个片段机器人工作机器人、片段、有两个、工作

2023-09-07 22:15:24 作者:乔治是猪

我是新android系统中,我坚持,同时与片段工作在任何的帮助实在是AP preciated.I正与片段平板电脑工作。有两个片段想碎裂(左片段)和FragmentB(右侧片段)都是列表片段,根据碎裂FragmentB变化。还有一个片段,FragmentC其膨胀的点击FragmentB的列表中的项目。现在充气FragmentC后,当我点击碎裂我的应用程序崩溃。有一个在logcat中显示FragmentC可以投不进FragmentB错误。我试图解决从过去的10小时这样的问题,但没有结果出来了。

这是我MainActivity类

 公共类MainActivity扩展FragmentActivity实现FragmentA.Communicator {
    FragmentManager经理;
    FragmentB frag2;
    碎裂frag1;
    @燮pressLint(NewApi)
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.frag);
        经理= getSupportFragmentManager();
        碎裂frag1 =(碎裂)manager.findFragmentById(R.id.fragment1);
    frag1.setCommunicator(本);


    }

    @燮pressLint(NewApi)
    @覆盖
    公共无效响应(INT指数){
        // TODO自动生成方法存根

        frag2 =(FragmentB)manager.findFragmentById(R.id.fragment2);
        frag2.changeData(指数);
        }
}
 

这是我的碎裂类

 公共类碎裂扩展片段实现OnItemClickListener {
      通讯通讯;
       ListView控件列表;
      的String []项目= {饮料,食品};


      @覆盖
      公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
          捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.menu_list,集装箱,假);
      名单=(ListView控件)view.findViewById(R.id.listView1);


     ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(getActivity(),android.R.layout.simple_list_item_1,项目);
      list.setAdapter(适配器);
      list.setOnItemClickListener(本);
        返回查看;
      }
    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT I,长ARG3){
        // TODO自动生成方法存根
        comm.respond(ⅰ);

    }

公共无效setCommunicator(通讯通讯){
    this.comm =沟通;
}
公共接口通讯{
    公共无效响应(INT指数);
}

    }
 
盘点各种各样的机器人

这是我FragmentB类

 公共类FragmentB扩展片段实现OnItemClickListener {
ListView控件listmenu;

的String [] listItem1 = {ITEM1,项目2,项目3,ITEM4,item5};
串[] listItem2 = {意达,itemB,itemC,itemD,itemE};

 @覆盖
  公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
      捆绑savedInstanceState){
    查看查看= inflater.inflate(R.layout.menu,集装箱,假);
    listmenu =(ListView控件)view.findViewById(R.id.listView_menu);



    返回查看;
  }


公共无效changeData(INT指数){

    如果(指数== 0){

         ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(getActivity(),android.R.layout.simple_list_item_1,listItem1);
  listmenu.setAdapter(适配器);



            }


        }
    如果(索引== 1){
     ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(getActivity(),android.R.layout.simple_list_item_1,listItem2);
  listmenu.setAdapter(适配器);



    }

    listmenu.setOnItemClickListener(本);
}


@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,长ARG3){
    // TODO自动生成方法存根
    //意向意图= NULL;

    开关(位置){
    情况下0:FragmentManager经理= getFragmentManager();
        片段frag1 =新FragmentC();
        FragmentTransaction英尺= manager.beginTransaction();
        ft.replace(R.id.fragment2,frag1,A);
        ft.addToBackStack(A);
        ft.commit();

        打破;
    }

}

}
 

解决方案

我想你需要检查哪些2片段是活着。如果片段B是积极的尝试先关闭,然后重新打开片段答:然后你就可以继续这个过程。

I am new in android i am stuck while working with fragment any help in this is really appreciated.I am working with fragment for tablet. There are two fragment suppose FragmentA(Left side fragment) and FragmentB(Right side Fragment) both are list fragment, FragmentB change according to FragmentA. There is another fragment, FragmentC which inflates on clicking the item on List of FragmentB. Now after inflating the FragmentC when I click on the FragmentA my app Crashes. There is an error in logcat showing FragmentC can't cast into FragmentB. I am trying to solve this problem from last 10 hours but no result came out.

This is my MainActivity class

public class MainActivity extends FragmentActivity implements FragmentA.Communicator{
    FragmentManager manager;
    FragmentB frag2;
    FragmentA frag1;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frag);
        manager=getSupportFragmentManager();
        FragmentA frag1=(FragmentA ) manager.findFragmentById(R.id.fragment1);
    frag1.setCommunicator(this);


    }

    @SuppressLint("NewApi")
    @Override
    public void respond(int index) {
        // TODO Auto-generated method stub

        frag2= (FragmentB) manager.findFragmentById(R.id.fragment2);
        frag2.changeData(index);
        }
} 

This is my FragmentA class

public class FragmentA extends Fragment implements OnItemClickListener{
      Communicator comm;
       ListView list;
      String[] items={"Beverages","Food"};


      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_list,container, false);
      list=(ListView) view.findViewById(R.id.listView1);


     ArrayAdapter<String> adapter= new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,items);
      list.setAdapter(adapter);
      list.setOnItemClickListener(this);
        return view;
      }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int i, long arg3) {
        // TODO Auto-generated method stub
        comm.respond(i);

    }

public void setCommunicator(Communicator communicator){
    this.comm=communicator;
}
public interface Communicator{
    public void respond(int index);
}

    }

This is my FragmentB class

public class FragmentB extends Fragment implements OnItemClickListener {
ListView listmenu;

String[] listItem1={"item1","item2","item3","item4","item5"};
String[] listItem2={"itemA","itemB","itemC","itemD","itemE"};

 @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.menu, container, false);
    listmenu=(ListView) view.findViewById(R.id.listView_menu);



    return view;
  }


public void changeData(int index){

    if(index==0){

         ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItem1);
  listmenu.setAdapter(adapter);     



            }


        }
    if(index==1){
     ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItem2);
  listmenu.setAdapter(adapter);



    }

    listmenu.setOnItemClickListener(this);
}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    // TODO Auto-generated method stub
    //Intent intent = null;

    switch(position){
    case 0: FragmentManager manager=getFragmentManager();
        Fragment frag1=new FragmentC();
        FragmentTransaction ft=manager.beginTransaction();
        ft.replace(R.id.fragment2, frag1,"A");
        ft.addToBackStack("A");
        ft.commit();

        break;
    }

}

} 

解决方案

I think you need check which of the 2 fragments is 'alive'. If Fragment B is active try to close it first and re-open Fragment A. Then you can continue the process.