Note that there are some explanatory texts on larger screens.

plurals
  1. POEntering Google Maps Fragment from a Action Bar
    primarykey
    data
    text
    <p>I've been stuck with the following issue for the last 3 weeks. I'm building an Android app which has a starting page, then with a Action Bar displaying several options. All static Android Activities work fine, but the button that's supposed to point to the Fragment containing my Google Maps activity just doesn't seem to work. I can't refer to it through the menu. Any help or tips are appreciated, I don't really know what to do.</p> <p>The HomeActivity contains the following code;</p> <pre><code>package com.team14.solocal; import java.util.ArrayList; import com.team14.solocal.RegisterActivity.UserRegisterTask; import com.team14.solocal.data.DataManager; import android.app.ActionBar; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import com.google.android.gms.maps.SupportMapFragment; public class HomeActivity extends FragmentActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); ActionBar ab = getActionBar(); ab.setTitle(getString(R.string.app_name)); ab.setSubtitle(getString(R.string.Sub_title)); Fragment newFragment = new SearchProductFragment(); FragmentTransaction ft1 = getSupportFragmentManager() .beginTransaction(); ft1.replace(R.id.content_layout, newFragment).addToBackStack(null) .commit(); } public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.home, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { Fragment newFragment; FragmentTransaction ft1; Fragment mapsFragment; if(item.getItemId() == R.id.Search){ newFragment = new SearchProductFragment(); ft1 = getSupportFragmentManager().beginTransaction(); ft1.replace(R.id.content_layout, newFragment).addToBackStack(null) .commit(); } else if(item.getItemId() == R.id.Messages){ newFragment = (Fragment) new MessagesMenuFragment(); ft1 = getSupportFragmentManager().beginTransaction(); ft1.replace(R.id.content_layout, newFragment).addToBackStack(null) .commit(); } else if(item.getItemId() == R.id.Maps){ mapsFragment = new MapsFragment(); ft1 = getSupportFragmentManager().beginTransaction(); ft1.replace(R.id.content_layout, mapsFragment).addToBackStack(null) .commit(); } else if(item.getItemId() == R.id.Settings){ newFragment = new SettingsFragment(); ft1 = getSupportFragmentManager().beginTransaction(); ft1.replace(R.id.content_layout, newFragment).addToBackStack(null) .commit(); } return false; } } </code></pre> <p>I'd like to make the button activate my Google Maps Activity, while the menu ofcourse remains visible.</p> <p>The Google Maps activity contains the following code.</p> <pre><code>import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; // MapsFragment, used to initialize the Maps rendering and possible finding of not-installed Play Services public class MapsFragment extends android.support.v4.app.FragmentActivity { // Setup Google Map value private GoogleMap mMap; // Main OnCreateView code // Executes when Maps button in SherlockBar is pressed public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_maps); setUpMapIfNeeded(); } // Rest of code is for initializing private void setUpMap() { mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title( "Marker")); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the // map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { // If yes, use the final setUpMap command. setUpMap(); } } } } </code></pre> <p>I'm a beginner to Android programming so I feel like I'm missing something simple here. Maybe mixing up Fragments and Activities?</p> <p>Thanks,</p> <p>Mike</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload