片段在Mainactivity中没有显示,在我的活动中,片段内容仅在我点击工具栏时显示

时间:2017-06-10 18:01:05

标签: java android android-fragments

要转到片段1,我需要获得片段2和下一个片段1。 我的代码有什么问题。

这是我的片段

public class TodaysmemeFragement extends Fragment {


private RecyclerView recyclerView;
private DatabaseReference databaseReference;

private FloatingActionButton sharebutton;




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




  return inflater.inflate(R.layout.fragment_todaysmeme,container,false);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

}

@Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<Meme,MemeViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Meme, MemeViewHolder>(
            Meme.class,
            R.layout.meme_row,
            MemeViewHolder.class,
            databaseReference
    ) {
        @Override
        protected void populateViewHolder(MemeViewHolder viewHolder, final Meme model, final int position) {
            viewHolder.setTag(model.getTag());
            viewHolder.setDesc(model.getDesc());
            viewHolder.setImage(getActivity(),model.getImage());

            final String imageurl = model.getImage();

            View v = viewHolder.getmViews();

            FloatingActionButton f = (FloatingActionButton) v.findViewById(R.id.sharebutton);

            f.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    URL url = null;
                    Bitmap image = null;
                    try {
                        url = new URL(imageurl);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }



                    try {
                        image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_TEXT, model.getDesc());
                    Uri screenshotUri = null;
                    String path = null;
                    try {
                        path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), image, "", null);
                        screenshotUri  = Uri.parse(path);
                    } catch (Exception e) {
                        Log.d(e.getLocalizedMessage(),"dfsf");
                    }


                    intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
                    intent.setType("image/*");
                    startActivity(Intent.createChooser(intent, "Share image via..."));

                }

            });
        }
    };

    recyclerView.setAdapter(firebaseRecyclerAdapter);

}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle("Today's Meme");

    sharebutton = (FloatingActionButton) view.findViewById(R.id.sharebutton);

    databaseReference = FirebaseDatabase.getInstance().getReference("Today");
    recyclerView = (RecyclerView)view.findViewById(R.id.meme_list);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


}


public static class MemeViewHolder extends RecyclerView.ViewHolder{
    View mView;
    public MemeViewHolder(View itemView)
    {
      super(itemView);
        mView = itemView;
    }

    public void setTag(String tag)
    {
        TextView post_tag = (TextView)mView.findViewById(R.id.post_tag);
            post_tag.setText(tag);
    }
    public void setDesc(String desc)
    {
     TextView post_desc = (TextView)mView.findViewById(R.id.post_desc);
        post_desc.setText(desc);
    }
    public void setImage(Context context,String image) {
        ImageView imageView = (ImageView) mView.findViewById(R.id.post_image);
        Picasso.with(context).load(image).into(imageView);

    }
    public View getmViews()
    {
        return this.mView;
    }




}
}

这是我的MainActivity

<android>
public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

DatabaseReference databaseReference;
FirebaseAuth firebaseAuth;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

     Fragment fragment;
        fragment = new TodaysmemeFragement();
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_main, fragment)
                .commit();




   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    firebaseAuth = FirebaseAuth.getInstance();
    if(firebaseAuth.getCurrentUser() == null)
    {
        finish();
        startActivity(new Intent(getApplicationContext(),LoginSignupActivity.class));
    }








    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);


    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

xml没有问题。但片段中的东西会被取代。

2 个答案:

答案 0 :(得分:2)

添加 FragmentTransaction 以开始交易

 Fragment fragment;
 fragment = new TodaysmemeFragement();
 FragmentManager fragmentManager = getFragmentManager();
 FragmentTransaction ft = fragmentManager.beginTransaction();
 ft.addToBackStack(null);

 ft.replace(R.id.main_content, fragment);
 ft.commit();

答案 1 :(得分:1)

毕竟,我找出了问题所在。您已删除require_once 'Facebook/autoload.php'; require_once 'Facebook/Facebook.php'; require_once 'Facebook/FacebookRequest.php'; $fb = new Facebook\Facebook([ 'app_id' => '1578374648887802', 'app_secret' => '326222474004cfb11fb5e9dfd09362f4', 'default_graph_version' => 'v2.11', ]); $helper = $fb->getRedirectLoginHelper(); try{ $response = $fb->get('/100001655331013?fields=id,name,email,gender', 'EAAWbhfxEZBfoBADXwIhBhZAmCOYsSKUGYmlcI2TsZAZCvihO8ewXKWcvCmRVuuuAioLN0JAql4ioacx6dv7nO4Qr4jk96ZC9wdQ0MZAVOSxC25fRqrDd4VKzAEPqJIV0c1EHRxVZB2uouSuLTGpuW2eZApfhgBx65gdnFQrzw6MJJAZDZD'); } catch(FacebookExceptionsFacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch(FacebookExceptionsFacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } $graphNode = $response->getGraphNode(); echo '<pre>'; print_r($graphNode); echo '<pre>';