My test project has a main activity as follows:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
public void sendMessage(View view){
FragmentManager frManager = getFragmentManager();
FragmentTransaction transaction = frManager.beginTransaction();
Frag fr= new Frag();
transaction.add(R.id.stuuf, fr);
transaction.addToBackStack(null);
transaction.commit();
}
}
And the following main layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/stuuf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="?android:attr/actionBarSize" >
<Button android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</TableLayout>
When the user clicks the button the following fragment is displayed containing a second button:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Frag" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="180dp"
android:onClick="buttonClicked"
android:text="Button" />
</RelativeLayout>
And the class that is supposed to control it:
public class Frag extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.frag_test, null);
}
public void buttonClicked(){
System.out.println("Hello!");
}
}
Now, there is a problem that when I click the second button, the system goes to look for the buttonClicked() method in MainActivity and not in Frag, thereby giving me the following error:
java.lang.IllegalStateExcetion: Could not find method buttonClicked(View) in the activity class com.spacitron.mytestproject.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
Obviously I should be able to handle what happens in the fragment from the fragment class itself so I must be doing something totally wrong, I just can't pinpoint what.