2014/07/25

fragmentを使った場合、android:onClickで呼び出すメソッドはMainActivityの方へ書く。

ボタンをFragment.classのレイアウトで使用している
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickButton1"
        android:text="click" />
</LinearLayout>

Fragment.classではなくMainActivity.classに

public void onClickButton1(View view){
//処理
}

を書く。
"The specified child already has a parent. You must call removeView() on the child's parent first." というエラーが出た時の対処

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



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