0

I have bug I don't know how to fix.
I need to add a Fragment, but it won't work.
Everything happens in void firstTime() and the error shows that it's on line setContentView(R.layout.activity_main);.

I really don't know how to fix this.
I tried a lot of things, but it just does not work.

Please, help me.

MainActivity class:

    public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private MapFragment mapsFragment;
    static MainActivity can;
    static FloatingActionButton fab;
    static FloatingActionButton show;
    private String encoded_string;
    private Bitmap bitmap;
    private String picturePath;
    View rootView;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    private void initializeMapsFragment() {
        FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
        mapsFragment = new MapFragment();
        SupportMapFragment supportMapFragment = mapsFragment;
        mTransaction.add(R.id.map, supportMapFragment);
        mTransaction.commitNow();
    }

    @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        can = this;
        Log.d("--***** MAP  ", "::Loading Map");
        setContentView(R.layout.activity_main);
        initializeMapsFragment();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        show = (FloatingActionButton) findViewById(R.id.show);
        show.hide();
        fab.hide();
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callPopup();

            }
        });

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stats();

            }
        });
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                can, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(can);

        Button searchButton = (Button) findViewById(R.id.searchButton);

        searchButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                EditText searchView = (EditText) findViewById(R.id.searchView1);
                String text = searchView.getText().toString();

                Geocoder geocoder = new Geocoder(getBaseContext());
                List<Address> addresses = null;

                try {
                    // Getting a maximum of 3 Address that matches the input
                    // text
                    addresses = geocoder.getFromLocationName(text, 3);
                    if (addresses != null && !addresses.equals(""))
                        search(addresses);

                } catch (Exception e) {

                }

            }
        });

        client = new GoogleApiClient.Builder(can).addApi(AppIndex.API).build();
    }


    public void start(){
        setContentView(R.layout.activity_main);
        initializeMapsFragment();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        show = (FloatingActionButton) findViewById(R.id.show);
        show.hide();
        fab.hide();
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callPopup();

            }
        });

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stats();

            }
        });
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                can, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(can);


        Button searchButton = (Button) findViewById(R.id.searchButton);

        searchButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                EditText searchView = (EditText) findViewById(R.id.searchView1);
                String text = searchView.getText().toString();

                Geocoder geocoder = new Geocoder(getBaseContext());
                List<Address> addresses = null;

                try {
                    // Getting a maximum of 3 Address that matches the input
                    // text
                    addresses = geocoder.getFromLocationName(text, 3);
                    if (addresses != null && !addresses.equals(""))
                        search(addresses);

                } catch (Exception e) {

                }

            }
        });

        client = new GoogleApiClient.Builder(can).addApi(AppIndex.API).build();

    }

    protected void search(List<Address> addresses) {

        Address address = (Address) addresses.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());

        MapFragment.mapView.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        MapFragment.mapView.animateCamera(CameraUpdateFactory.zoomTo(15));


    }

    @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        FragmentManager fm = getFragmentManager();
        android.support.v4.app.FragmentManager sFm = getSupportFragmentManager();

        int id = item.getItemId();


        if (id == R.id.nav_camera) {

            if (!mapsFragment.isAdded())
                sFm.beginTransaction().add(R.id.map, mapsFragment).commit();
            else
                sFm.beginTransaction().show(mapsFragment).commit();
        } else if (id == R.id.nav_share) {

            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "Check this app out --> link.kys";
            sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Best Free Parking app");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    public void firstTime() {


        setContentView(R.layout.firsttime);

        (findViewById(R.id.cancelBut))
                .setOnClickListener(new View.OnClickListener() {

                    public void onClick(View arg0) {

                        //View view = can.findViewById(android.R.id.content);
                        //Log.d("a", "" + ((ViewGroup)view).getChildCount() );
                        //((ViewGroup) view).removeView(view);
                        android.support.v4.app.FragmentManager sFm = getSupportFragmentManager();
                        sFm.beginTransaction().remove(mapsFragment).commitNow();
                        if(mapsFragment.isAdded()){

                            Log.d("--***** FRAGMENT  ", "::Still On");

                        }
                        start();
                    }

                });


    }
    public static void load(){

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(can);
        if (!prefs.getBoolean("firstTime", false)) {

            can.firstTime();

            //SharedPreferences.Editor editor = prefs.edit();
            //editor.putBoolean("firstTime", true);
            //editor.commit();
        }

    }

    private void stats() {

        setContentView(R.layout.stats);



        RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingBar);
        ratingbar.setRating((float) 2.0);
        ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating,
                                        boolean fromUser) {

                ratingBar.setRating((float) 2.0);

            }
        });


        ((Button) findViewById(R.id.cancBut)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.content_main);
            }
        });
    }

}

Xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.robertas.parking.bestfreeparking.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.robertas.parking.test.MapsActivity"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_weight="0.68" />

    <EditText
        android:id="@+id/searchView1"
        android:inputType="textPersonName"
        android:layout_width="250dp"
        android:layout_height="34dp"
        android:layout_marginTop="14dp"
        android:hint="Search Location"
        android:ems="10"
        android:textColor="@android:color/black"
        android:background="@android:color/white"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
    </EditText>

    <Button
        android:id="@+id/searchButton"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:background="@drawable/places_ic_search"
        android:layout_alignBottom="@+id/searchView1"
        android:layout_alignRight="@+id/searchView1"
        android:layout_alignEnd="@+id/searchView1"
        android:layout_alignTop="@+id/searchView1"
        android:layout_marginLeft="216dp"
        android:layout_marginStart="216dp"
        android:layout_alignLeft="@+id/searchView1"
        android:layout_alignStart="@+id/searchView1" />

</RelativeLayout>

Error:

android.view.InflateException: Binary XML file line #12: Error inflating class fragment

Log:

03-10 20:21:45.036 31937-31937/com.robertas.parking.bestfreeparking D/AndroidRuntime: Shutting down VM
03-10 20:21:45.036 31937-31937/com.robertas.parking.bestfreeparking E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.robertas.parking.bestfreeparking, PID: 31937
                                                                                      android.view.InflateException: Binary XML file line #12: Error inflating class fragment
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
                                                                                          at com.robertas.parking.bestfreeparking.MainActivity.start(MainActivity.java:161)
                                                                                          at com.robertas.parking.bestfreeparking.MainActivity$8.onClick(MainActivity.java:356)
                                                                                          at android.view.View.performClick(View.java:4780)
                                                                                          at android.view.View$PerformClick.run(View.java:19866)
                                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:135)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                       Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f0f00e8, tag null, or parent id 0x7f0f00e7 with another fragment for com.google.android.gms.maps.SupportMapFragment
                                                                                          at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2427)
                                                                                          at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                                          at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
                                                                                          at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
                                                                                          at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:77)
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) 
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) 
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                                          at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) 
                                                                                          at com.robertas.parking.bestfreeparking.MainActivity.start(MainActivity.java:161) 
                                                                                          at com.robertas.parking.bestfreeparking.MainActivity$8.onClick(MainActivity.java:356) 
                                                                                          at android.view.View.performClick(View.java:4780) 
                                                                                          at android.view.View$PerformClick.run(View.java:19866) 
                                                                                          at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                          at android.os.Looper.loop(Looper.java:135) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
03-10 20:21:45.039 1499-1878/system_process W/ActivityManager:   Force finishing activity 1 com.robertas.parking.bestfreeparking/.MainActivity

                                                               [ 03-10 20:21:45.046  1499: 1878 D/         ]
                                                               HostConnection::get() New Host Connection established 0x7ffc98084ec0, tid 1878
03-10 20:21:45.051 1123-1123/? E/EGL_emulation: tid 1123: eglCreateSyncKHR(1299): error 0x3004 (EGL_BAD_ATTRIBUTE)
03-10 20:21:45.152 1499-1558/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
03-10 20:21:45.194 1499-1558/system_process W/EGL_emulation: eglSurfaceAttrib not implemented
03-10 20:21:45.194 1499-1558/system_process W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffca953fb40, error=EGL_SUCCESS
03-10 20:21:45.612 1499-1518/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{38e88b4d u0 com.robertas.parking.bestfreeparking/.MainActivity t155 f}
03-10 20:21:45.645 1756-2040/com.google.android.googlequicksearchbox W/EGL_emulation: eglSurfaceAttrib not implemented
03-10 20:21:45.645 1756-2040/com.google.android.googlequicksearchbox W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe1c93620, error=EGL_SUCCESS
03-10 20:21:46.616 1622-1622/com.android.systemui W/ResourceType: No package identifier when getting value for resource number 0x00000000
03-10 20:21:46.616 1622-1622/com.android.systemui W/PackageManager: Failure retrieving resources for com.robertas.parking.bestfreeparking: Resource ID #0x0
03-10 20:21:46.832 1756-1756/com.google.android.googlequicksearchbox I/Choreographer: Skipped 69 frames!  The application may be doing too much work on its main thread.
03-10 20:21:47.332 1756-2040/com.google.android.googlequicksearchbox W/OpenGLRenderer: Incorrectly called buildLayer on View: aep, destroying layer...
03-10 20:21:47.448 1499-1577/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
03-10 20:21:47.465 31937-31937/? I/Process: Sending signal. PID: 31937 SIG: 9
03-10 20:21:47.474 1499-14634/system_process I/WindowState: WIN DEATH: Window{394fad98 u0 com.robertas.parking.bestfreeparking/com.robertas.parking.bestfreeparking.MainActivity}
03-10 20:21:47.474 1499-1874/system_process I/ActivityManager: Process com.robertas.parking.bestfreeparking (pid 31937) has died
03-10 20:21:47.475 1499-1874/system_process W/ActivityManager: Scheduling restart of crashed service com.robertas.parking.bestfreeparking/com.android.tools.fd.runtime.InstantRunService in 1000ms
03-10 20:21:47.480 1123-1123/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
03-10 20:21:47.521 1499-1558/system_process D/OpenGLRenderer: endAllStagingAnimators on 0x7ffc9dfec000 (RippleDrawable) with handle 0x7ffca9734500
03-10 20:21:47.525 1499-1753/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 31937 uid 10058
03-10 20:21:47.537 1956-32415/com.google.android.googlequicksearchbox:search I/HotwordRecognitionRnr: Starting hotword detection.
03-10 20:21:47.537 1956-32414/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_starting gzi@c6aa4d0
03-10 20:21:47.568 1135-1912/? E/audio_hw_generic: Error opening input stream format 1, channel_mask 0010, sample_rate 16000
03-10 20:21:47.569 1135-32418/? I/AudioFlinger: AudioFlinger's thread 0xf59c4000 ready to run
03-10 20:21:47.581 2146-32417/com.google.android.gms W/IcingInternalCorpora: getNumBytesRead when not calculated.
03-10 20:21:47.584 1956-32414/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_started gzi@c6aa4d0
03-10 20:21:47.602 1956-1956/com.google.android.googlequicksearchbox:search I/HotwordWorker: onReady
03-10 20:21:47.629 2146-2248/com.google.android.gms W/Settings: Setting adb_enabled has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
03-10 20:21:47.629 2146-2248/com.google.android.gms I/Icing: Usage reports 1 indexed 1 rejected 0 imm upload true
03-10 20:21:47.634 2146-2248/com.google.android.gms W/Icing: App history upload skipped 1 0 0
03-10 20:21:48.101 1135-1535/? W/AudioFlinger: write blocked for 469 msecs, 16 delayed writes, thread 0xf58ac000
03-10 20:21:48.503 32429-32429/? I/art: Not late-enabling -Xcheck:jni (already on)
03-10 20:21:48.507 1499-1518/system_process I/ActivityManager: Start proc 32429:com.robertas.parking.bestfreeparking/u0a58 for service com.robertas.parking.bestfreeparking/com.android.tools.fd.runtime.InstantRunService
03-10 20:21:48.775 32429-32429/com.robertas.parking.bestfreeparking I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
03-10 20:21:48.777 32429-32429/com.robertas.parking.bestfreeparking I/InstantRun: Starting Instant Run Server for com.robertas.parking.bestfreeparking
03-10 20:21:48.777 1499-14635/system_process W/ActivityManager: getRunningAppProcesses: caller 10058 does not hold REAL_GET_TASKS; limiting output
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
TerZer
  • 83
  • 8

2 Answers2

0

Make sure you compile com.google.android.gms.maps in your build.gradle file. Add this to your dependencies if it's not there:

dependencies {
        <other dependencies>
        compile 'com.google.android.gms:play-services-maps:10.2.0'
    }

And remember to import it in your java file:

import com.google.android.gms.maps.SupportMapFragment;

And on the tools:context, it should be .MainActivity

Nagas
  • 55
  • 1
  • 5
  • Everything already done except that I changed tools:context. But that did nothing. I think that I should somehow enable fragment then start() void runs, but I don't know how, because I already tested various of ways. – TerZer Mar 09 '17 at 23:34
  • Have you tried not putting things in initializeMapsFragment(); but instead expand them directly in the onCreate()? (or start()) – Nagas Mar 09 '17 at 23:40
  • Yes, but did nothing. – TerZer Mar 10 '17 at 15:57
0

Everything happens in firstTime() void and error shows that it's on line setContentView(R.layout.activity_main);.

You can't do this.

start() and firstTime() are not the entrypoints to your activity/application.
onCreate() is and that is where you can only call setContentView(R.layout.activity_main).

Even so, static methods that "load" any Activity aren't going to work.

public static void load(){

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(can);
    if (!prefs.getBoolean("firstTime", false)) {

        can.firstTime(); // This doesn't work like this

    }

}

Especially if you did MainActivity.load() somewhere else in your other classes.


Reading your error, see that start() -> setContentView() is causing the problem...

  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
  at com.robertas.parking.bestfreeparking.MainActivity.start(MainActivity.java:161)
  at com.robertas.parking.bestfreeparking.MainActivity$8.onClick(MainActivity.java:356)
  at android.view.View.performClick(View.java:4780)

And the real error message

Duplicate id 0x7f0f00e8, tag null, or parent id 0x7f0f00e7 with another fragment for com.google.android.gms.maps.SupportMapFragment

Meaning that the Fragment "@+id/maps" has already been added into the FragmentManger of the Activity class, so it has been duplicated.


Activities should ideally not call setContentView outside of onCreate.

Reason being = You call setContentView, therefore removing the MapFragment (and other Views) entirely from the root view, so 1) the FragmentManger cannot find anything previously added, 2) Any view you had used findViewById before (probably) no longer exists.

If you are wanting to "replace" the content of some Activity, then you should use Fragments + a FrameLayout + a FragmentTransaction.replace() method call.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • So.. You telling me that I can't use setContentView? Ok. So can you show me some examples how I should change content view withought calling `setContentView`? – TerZer Mar 10 '17 at 17:03
  • One `FrameLayout` in the Activity. That is it. Use the `FragmentManager` to "replace the fragment", therefore "setting the content view" – OneCricketeer Mar 10 '17 at 17:27
  • Or, you can show your full logcat in the question and I'll try to answer that – OneCricketeer Mar 10 '17 at 17:28
  • **Added log.** I really don't want to change code, because I made a lot of work. – TerZer Mar 10 '17 at 18:26
  • Updated answer. I'm sure you have made a lot of work, but the way that you've done it has lead you on the path to your error, so you need to back track and do it differently. Fragments are the way to change the views within an Activity. That is well explained within the documentation. – OneCricketeer Mar 10 '17 at 18:45
  • But the thing is that I don't know how to use Fragments.. And everything you write in answer is, I suppose, to complex for me. Like I don't know what to do and what to write.. So maybe I can just remove duplicated fragment somehow? – TerZer Mar 10 '17 at 18:55
  • Try to learn Fragments independently of Google Maps... The XML code you have **is fine**. The additional `setContentView` methods outside of `onCreate` is your issue. If you want to try to learn them, see https://guides.codepath.com/android/Creating-and-Using-Fragments – OneCricketeer Mar 10 '17 at 19:09
  • Can I contact to you for consultation? – TerZer Mar 10 '17 at 21:51
  • Maybe, though, consulting is just my day job, and not at all Android related. I think asking well-posted questions to StackOverflow would get you more targeted answers. – OneCricketeer Mar 10 '17 at 22:17