なんとか動作させましたが、フラグメントを切り替えるとアプリがクラッシュします。
私が望むもう1つのことは、ユーザーが残した状態でフラグメントを正確に表示することです。
つまり、ユーザーがフラグメントに切り替えるボタンをクリックするたびにフラグメントの新しいインスタンスを作成したくないということです。
FragmentActivity
:_
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
public class Fragments extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
Add fragment = new Add();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_place, fragment);
transaction.commit();
}
public void onSelectFragment(View view) {
Fragment newFragment;
if (view == findViewById(R.id.add)) {
newFragment = new Add();
} else if (view == findViewById(R.id.map)) {
newFragment = new MainActivity();
} else {
newFragment = new Add();
}
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_place, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
ログキャット:
09-30 18:41:09.918: W/dalvikvm(18931): threadid=1: thread exiting with uncaught exception (group=0x40ed5300)
09-30 18:41:09.968: E/AndroidRuntime(18931): FATAL EXCEPTION: main
09-30 18:41:09.968: E/AndroidRuntime(18931): android.view.InflateException: Binary XML file line #255: Error inflating class fragment
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-30 18:41:09.968: E/AndroidRuntime(18931): at com.example.free.Add.onCreateView(Add.java:141)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.os.Handler.handleCallback(Handler.java:615)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.os.Looper.loop(Looper.java:137)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.app.ActivityThread.main(ActivityThread.java:4931)
09-30 18:41:09.968: E/AndroidRuntime(18931): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 18:41:09.968: E/AndroidRuntime(18931): at java.lang.reflect.Method.invoke(Method.java:511)
09-30 18:41:09.968: E/AndroidRuntime(18931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-30 18:41:09.968: E/AndroidRuntime(18931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-30 18:41:09.968: E/AndroidRuntime(18931): at dalvik.system.NativeStart.main(Native Method)
09-30 18:41:09.968: E/AndroidRuntime(18931): Caused by: java.lang.IllegalArgumentException: Binary XML file line #255: Duplicate id 0x7f07001d, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
09-30 18:41:09.968: E/AndroidRuntime(18931): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
09-30 18:41:09.968: E/AndroidRuntime(18931): ... 21 more
ここで何が問題なのですか?