1

How do I use .setOnClick in a fragment as mentioned in https://stackoverflow.com/a/7808223/1766169?

Community
  • 1
  • 1
user1766169
  • 1,932
  • 3
  • 22
  • 44
  • 4
    A suggestion: You should probably try to make your question as self-contained as possible, without having to read another question/page in detail. – doubleDown Oct 23 '12 at 16:16

1 Answers1

0

The person writing that answer presumably means calling setOnClickListener() on the Button widget:

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.main, parent, false);

    b=(Button)result.findViewById(R.id.button);
    b.setOnClickListener(this);

    return(result);
  }
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Is there any chance you can expand on this? I have a few onClicks that need updating since I found out about the Google issue, the code you provide above gives me a force close however. I'm using my onClicks to start other activities. Thanks. – RED_ Nov 12 '12 at 00:54
  • @RED_: "Is there any chance you can expand on this?" -- I don't see how. "since I found out about the Google issue" -- and what, precisely, is "the Google issue"? "the code you provide above gives me a force close however" -- and when you looked at your stack trace, what did you learn? – CommonsWare Nov 12 '12 at 01:32
  • I was told that there is something developers have wanted fixed for a while, being able to define onClicks in the XML & use them in Fragments, when defined in the XML it looks for the method in the Activity not a Fragment which is why my onClicks were unresponsive the first time around. Your code was the fix, defining onClicks in the code, however like I said I'm getting a force close. [Here is my code(Pastebin link)](http://pastebin.com/p2PYTpzN) Stack trace tells me there is a fatal exception on line 21 (25 on pastebin). The "random.setOnClickListener" line. Odd since it's quite basic code. – RED_ Nov 12 '12 at 18:45
  • @RED_: Without knowing what "fatal exception" you got (e.g., `NullPointerException`), nobody can really help you much. – CommonsWare Nov 12 '12 at 21:02
  • Fair enough, I seem to have stopped the force close by changing the onClick from a TextView to one of my ImageViews. That creates another problem... it won't start the activity when I click on the ImageView (another force close). [Here is the full thing (Code & Logcat).](http://pastebin.com/Mh8T5Yby) I'll keep on playing around with things and see what I come up with. Edit: I think I've used activity in the wrong sense here because the pages I'm trying to move between for the minute are Fragements. So the onClick here tries to start a Fragement which must be the issue. – RED_ Nov 12 '12 at 22:42
  • this is essentially what I'm trying to achieve just without tabs. I think I'm getting the jist of what I should be doing now. http://stackoverflow.com/questions/13333166/open-fragment-from-another-fragment?lq=1 – RED_ Nov 12 '12 at 23:44