-2

I am creating an android application in which there is a tabular view to swipe through fragments. I want to set up a card view in one of the fragments. I am able to implement in a new activity but I am not able to implement it inside a fragment. I am not able to use functions like findViewById() and setContentView()

PS: I tried setting it up using "this" and getActivity() but it still isn't working.

Prashant Negi
  • 348
  • 2
  • 14
  • 2
    Please post some code – AbhayBohra Sep 21 '16 at 06:48
  • The code is very confidential to be posted. I'm not able to access findViewbyId() i.e. application crashes – Prashant Negi Sep 21 '16 at 06:52
  • Please guys don't downvote the question, I am badly stuck at this – Prashant Negi Sep 21 '16 at 06:53
  • Take a look at this answer: http://stackoverflow.com/a/34581086 – Daniel Nugent Sep 21 '16 at 06:58
  • First read about fragment and activity communication! – Piyush Sep 21 '16 at 07:02
  • @PrashantNegi Post that code, exclude all the confidential details. we cant help you without the code. – K Neeraj Lal Sep 21 '16 at 07:07
  • This is to let you know that your question is being down-voted just because your question does not show efforts or you did not upload required code,so that SO community could help you.Please improve your question. – Mehraj Malik Sep 21 '16 at 07:15
  • @PrashantNegi read the on-hold message above, including the link, to find out why your question was downvoted. Please take the [tour] and carefully read through the [help] to learn more about the site, including [what is on-topic](http://stackoverflow.com/help/on-topic) and [what is not](http://stackoverflow.com/help/dont-ask), and how to [ask a good question](http://stackoverflow.com/help/how-to-ask). Please also follow the [question checklist](http://meta.stackoverflow.com/q/260648). – MattDMo Sep 21 '16 at 20:41

1 Answers1

0

Access your code like below in oncreateView method.

 View rootView = inflater.inflate(R.layout.YourLayout, container, false);

        cardView=(CardView)rootview.findViewById(R.id.cardView);

    return rootView;

SetContentView() is where you directly pass your view but in fragment you have to inflate layout using inflater provided in onCreateView(LayoutInflater, ViewGroup, Bundle) function. For more info read official doc.

Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23
  • Thanks Preetika it worked. But i dont get why people are diownvoting this question. Thanks for your help again – Prashant Negi Sep 21 '16 at 08:31
  • Its ok I dont knoe why but someone downvoted my answer as well...When they find something not useful they downvote it... but glad to help you – Preetika Kaur Sep 21 '16 at 10:36