3

I'm going crazy with Android programming. Nothing works properly..

Whats wrong with this?

Error: getIntent() is undefined for type View

Any ideas?

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quiz, container,
                false);

        TextView text = (TextView)rootView.findViewById(R.id.ttv);

        Bundle intentBundle = rootView.getIntent().getExtras();
        int question_cat = intentBundle.getInt("question_cat");

        text.setText(question_cat);

        return rootView;
    }
}
Philip
  • 1,068
  • 3
  • 12
  • 21

1 Answers1

1

You can just do:

Intent intentBundle = getActivity().getIntent();
        String question_cat = intentBundle.getStringExtra("question_cat");
        Log.i("Result : ", question_cat);

Even after you get the value as string, you use it as a int value later like this :

int j = Integer.valueOf(question_cat);

    Log.i("Result : ", String.valueOf(j));

For your other question, with getIntent(), the problem is that you are using it inside the fragment class and in order to use that, you have to use getActivity() to access it. If it was just a normal activity, it wasn't that complicated. Android is really fun if some concepts are clear .. :)

mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • 1
    no book seems to really work "from zero" with fragments. its hard to learn all the concepts for normal activities and then work with fragments. – Philip Mar 13 '14 at 22:59
  • 1
    True ..even the developers website is bit confusing. Whenever I have doubts I see posts on this site or I prefer to look for : http://www.vogella.com/tutorials/android.html Anyways, was this answer working for you ? – mike20132013 Mar 13 '14 at 23:04
  • 1
    it is, works just fine. although i'm not really happy with getting solutions here and not understand most of them. now i need to find out how to create a tab bar. i'm not looking forward to it – Philip Mar 13 '14 at 23:06
  • Its pretty simple if you wish to learn that. Here's my post for that : http://stackoverflow.com/questions/14042388/how-to-properly-use-fragments-with-viewpager/21801145#21801145 – mike20132013 Mar 13 '14 at 23:08
  • Lol.. Xcode is done man..no offence though..You can try the android studio and that comes with a lot of features with it. – mike20132013 Mar 13 '14 at 23:13
  • with Xcode i meant programming for iOS. but it seems like you download Xcode, get into objective c and you're ready to go. eclipse is an ***hole until you get it to run properly with AVD and all that – Philip Mar 13 '14 at 23:17
  • I know that.. but that's one thing we android developers have to do initially. – mike20132013 Mar 13 '14 at 23:21
  • mho personally i like android more. as a user. but on the programmers side.. i'm not yet convinced. probably soon. but since my ice completely changed with downloading kitkat sdk (fragment implementation) i don't have any fun learning programming for android. even getting a simple back/up button to the action bar is complicated. – Philip Mar 13 '14 at 23:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49707/discussion-between-mike20132013-and-user3385402) – mike20132013 Mar 14 '14 at 02:47