0

I managed to make it work, but when I switch the fragments the app crashes.

another thing that I want, Is to make the fragment show exactly in the state the user left it.

What I mean is that I don't want to make a new instance of the fragment every single time the user clicks the button that switches to it.

the 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();
    }

}

The Logcat:

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

What's the problem here?

4

1 回答 1

0

我有重复的GoogleMap片段。

我只是在其中一种布局中重命名了 id MapFragment,这样它们就会有所不同。

于 2013-09-30T17:08:56.517 回答