使用按钮单击

时间:2017-01-30 13:07:44

标签: java android android-fragments

我有一组片段。其中一个片段有一个自定义的AlertDialog,顶部有一个取消按钮。我希望关闭按钮关闭AlertDialog框并转到主页片段,但我不知道如何实现并收到此错误:

  

java.lang.IllegalArgumentException:找不到片段HomeFragment {e0d925e#4 id = 0x7f0c0093}

的id 0x7f0c0093(design.material.instagrammockup:id / frags)的视图

请帮忙。以下是我现在的情况:

public class CameraFragment extends Fragment  {
 TabLayout tabLayout;
 View rootView ;
 Context context;
 AlertDialog.Builder alertDialog = null;
 ImageButton closebutton;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.camera_fragment, container,false);
   /* ImageView imageView =(ImageView) rootView.findViewById(R.id.hold_Capture_image);
    imageView.setImageResource(R.drawable.beyonce);
   */ //GET THE VIEW PAGER AND SET ITS PAGER ADAPTER SO THAT IT CAN DISPLAY ITEMS
    final ViewPager camera_viewPager =(ViewPager)rootView.findViewById(R.id.camera_viewPager);
    PagerAdapter pagerAdapter = new PagerAdapter(getActivity().getSupportFragmentManager(), getActivity());
    camera_viewPager.setAdapter(pagerAdapter);
     alertDialog = new AlertDialog.Builder(getActivity(),R.style.AlertDialog);
    alertDialog.setCancelable(false);
    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    View view = layoutInflater.inflate(R.layout.camera_image_holder,null);
    ImageView imageView =(ImageView) view.findViewById(R.id.hold_Capture_image);
    closebutton =(ImageButton) view.findViewById(R.id.closeButton);
    Button gallery =(Button) view.findViewById(R.id.galleryButton);
    Button photo =(Button) view.findViewById(R.id.cameraButton);
    Button video =(Button) view.findViewById(R.id.videoButton);
    alertDialog.setView(view);
    alertDialog.show();


    closebutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment frags = new HomeFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.frags,frags);
            transaction.commit();
        }
    });

    //GIVE THE TAB LAYOUT THE VIEW PAGER
    tabLayout =(TabLayout)rootView.findViewById(R.id.camera_tabs);
    tabLayout.setupWithViewPager(camera_viewPager);
    camera_viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            camera_viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
    return rootView;
}


class PagerAdapter extends FragmentPagerAdapter {
    Context context;
    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        return null;
    }

    @Override
    public int getCount() {
        return 0;
    }
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (rootView != null) {
        ViewGroup parentViewGroup = (ViewGroup) rootView.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeAllViews();
        }
    }

}
}

自定义AlertDialog camera_image_holder.xml

的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frags">
<ImageButton
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:id="@+id/closeButton"
    android:src="@drawable/cancel"
    android:background="@android:color/transparent"
    android:clickable="true"
    />
<ImageView
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:id="@+id/hold_Capture_image"
    android:src="@drawable/beyonce"
    app:layout_collapseMode="parallax"/>
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/galleryRecycler"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GALLERY"
        android:id="@+id/galleryButton"
        android:background="@android:color/transparent"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cameraButton"
        android:text="CAMERA"
        android:layout_marginLeft="50dp"
        android:background="@android:color/transparent"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/videoButton"
        android:text="VIDEO"
        android:background="@android:color/transparent"
        android:layout_marginLeft="30dp"/>
</LinearLayout>

0 个答案:

没有答案