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