0

I'm still confused how to implement button handler on Fragments.

Here is my Fragment:

package com.example.myshops_diary;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater; 
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Item1_fragment extends Fragment implements OnClickListener { 

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

    View view = inflater.inflate(R.layout.item1_fragment, container, false);

    Button nextpageButton = (Button) view.findViewById(R.id.nextpageButton);
nextpageButton.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.nextpageButton:
    Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();


        break;
    }

}

private void setContentView(int item2_Fragment) {
    // TODO Auto-generated method stub



    //---Inflate the layout for this fragment---
//return inflater.inflate( R.layout.item1_fragment, container, false);



}





}

These codes are still not working at all, when I clicked the Next button the app stopped running.

** these are my LogCat :

04-14 13:00:15.899: D/dalvikvm(537): Not late-enabling CheckJNI (already on)
04-14 13:00:17.359: D/gralloc_goldfish(537): Emulator without GPU emulation detected.
04-14 13:00:28.289: D/AndroidRuntime(537): Shutting down VM
04-14 13:00:28.299: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-14 13:00:28.309: E/AndroidRuntime(537): FATAL EXCEPTION: main
04-14 13:00:28.309: E/AndroidRuntime(537): java.lang.IllegalStateException: Could not find a method nextClick(View) in the activity class com.example.myshops.MainActivity for onClick handler on view class android.widget.Button with id 'nextpageButton'
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.view.View$1.onClick(View.java:3026)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.view.View.performClick(View.java:3480)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.view.View$PerformClick.run(View.java:13983)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.os.Handler.handleCallback(Handler.java:605)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.os.Looper.loop(Looper.java:137)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-14 13:00:28.309: E/AndroidRuntime(537):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 13:00:28.309: E/AndroidRuntime(537):  at java.lang.reflect.Method.invoke(Method.java:511)
04-14 13:00:28.309: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-14 13:00:28.309: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-14 13:00:28.309: E/AndroidRuntime(537):  at dalvik.system.NativeStart.main(Native Method)
04-14 13:00:28.309: E/AndroidRuntime(537): Caused by: java.lang.NoSuchMethodException: nextClick [class android.view.View]
04-14 13:00:28.309: E/AndroidRuntime(537):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-14 13:00:28.309: E/AndroidRuntime(537):  at java.lang.Class.getMethod(Class.java:915)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.view.View$1.onClick(View.java:3019)
04-14 13:00:28.309: E/AndroidRuntime(537):  ... 11 more
user3514625
  • 21
  • 2
  • 5

3 Answers3

0

Your onClick method looks good. I don't know what you tried to do, however you cannot set a content view twice. You already inflated a view inside onCreateView method:

inflater.inflate(R.layout.item1_fragment, container, false);

Then you cannot call this code below to add a layout. This is the cause of your crash:

setContentView(R.layout.item2_fragment);  

Also make sure that the button clicked inside your layout has this id:

<Button
    android:id="@+id/nextpageButton"
    ... />  

Because of the android:onClick="nextClick" attribute, you have this error: NoSuchMethodException: nextClick. It means that the system cannot find a method called nextClick. You try to call onClickListener method and an onClick method provide by the button.

Then, remove the attribute in your layout and keep this part inside your Activity:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        // This will display a Toast (msg) when the button is clicked
        case R.id.nextpageButton:
            Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
        break;
    }
}  

See the documentation to see the two different methods.

Update

You set the click listener as follows:

// Here the inflated view (the layout) and the button are the same variable..
view = (Button) view.findViewById(R.id.nextpageButton);
view.setOnClickListener(this);    

But you should use a different variable to your button as follows:

// Here we give another name variable as "button_name" and also we set the widget "Button"
Button button_name = (Button) view.findViewById(R.id.nextpageButton);
button_name.setOnClickListener(getActivity());  

Solution

Here another method, call directly the onClickListener without the implement. Remove this:

implements OnClickListener // on your Fragment Class declaration

Then:

nextpageButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
    }
});

Solution2

Remove all the onClick method, etc. in your Activity. Add the following attribute inside the layout:

android:onClick="nextpageButton" // on your button  

Then inside your activity:

// call the "nextpageButton" inside your activity as
public void nextpageButton(View view) {
    Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
}
Blo
  • 11,903
  • 5
  • 45
  • 99
  • ok trying it now ... actually i have concerned about the button functionality itself , it suppose to direct the page to the next page but seems like from your updated code , it doesnt show any functions to make it move to (navigate) the next page at all @Fllo – user3514625 Apr 14 '14 at 04:16
  • its not working .. i think it should have changed to direct the current page to the next page (other fragments) .. @Fllo – user3514625 Apr 14 '14 at 04:17
  • @user3514625 the solution2 is inside your Activity, see [this answer](http://stackoverflow.com/a/21192511/2668136) and this will work for sure! – Blo Apr 14 '14 at 04:22
  • ... Well... I don't really know now. I tried everything to understand. I can just say one last thing: sorry and good luck ;) I really hope you will find the right way. – Blo Apr 14 '14 at 04:36
0

try this.

public static class PlaceholderFragment extends Fragment implements
        OnClickListener {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_job_seeker_home,
                container, false);
        Button bu = (Button) rootView.findViewById(R.id.next);
        bu.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.next:
        Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
            break;
        }
    }
}
Silambarasan Poonguti
  • 9,386
  • 4
  • 45
  • 38
0

Inside your MainActivity, first inflate the fragment,

MainFragment framgmentInstance=new MainFragment();
this.getSupportFragmentManager().beginTransaction().replace(R.id.content_main,framgmentInstance,"tag").commit();

Button in the fragment layout should looks like this,

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="click"
    android:id="@+id/btnOne"
/>

As I understand you want to open another activity using the button click event. Correct me if I'm wrong. However, if you want to open a new activity using a button in fragment, try the following code.

Button btnOne=(Button)view.findViewById(R.id.btnOne);
    btnOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(getActivity(),secondActivity.class);
            startActivity(i);
        }
    });

'secondActivity' is the name of the activity that you want to open.

Nipuna Dilhara
  • 424
  • 2
  • 6
  • 18