I have a map offline mapBox, but the map does not load because it is null. I followed the directions of the site: https://www.mapbox.com/android-sdk/
In layout
<com.mapbox.mapboxsdk.views.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
mapbox:access_token="#string/access_token"
/>
In Fragment:
import com.mapbox.mapboxsdk.MapFragment;
........
public class ConctactsFragment extends MapFragment {
.........
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity();
mRootView = inflater.inflate(R.layout.fragment_map, container, false);
mMapView =(MapView)mRootView.findViewById(R.id.mapView);
XmlPullParser parser=getResources().getXml(R.xml.attrs);
AttributeSet attrs = Xml.asAttributeSet(parser);
mMapView = new MapView(mContext, attrs);
Log.d(TAG, "The mapView in onCreateView: " + this.getMap());
//The mapView in onCreateView: null
mMapView.setStyleUrl(Style.MAPBOX_STREETS);
mMapView.setCenterCoordinate(new LatLng(40.956645, 14.304816));
mMapView.setZoomLevel(11);
return mRootView;
}
#Override
public void onStart() {
Log.d(TAG, "The mapView in OnStart: " +this.getMap());
// The mapView in OnStart: null
super.onStart();
mMapView.onStart();
}
}
The error is:
java.lang.NullPointerException
at com.mapbox.mapboxsdk.MapFragment.onStart(MapFragment.java:70)
Why are you extending MapFragment and defining your own layout? Anyway this looks like there is no R.id.mapView in your R.layout.fragment_map. Can't really say more without the full layouts and fragment loading logic.
Here is some code that works (using mapbox SDK 2.2, Android SDK 23):
MainActivity class:
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
// Load the fragment dynamically
getFragmentManager()
.beginTransaction()
.add(R.id.fragment_container_layout, MyMapFragment.create())
.commit();
}
}
Custom Fragment:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.views.MapView;
public class MyMapFragment extends Fragment {
private MapView mMapView;
public static MyMapFragment create() {
return new MyMapFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_map_fragment, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mb_map_view);
mMapView.setStyleUrl(Style.MAPBOX_STREETS);
mMapView.setCenterCoordinate(new LatLng(40.956645, 14.304816));
mMapView.setZoomLevel(11);
mMapView.onCreate(savedInstanceState); // If this line is omitted there is a native crash in the MapBox library
return rootView;
}
#Override
public void onStart() {
super.onStart();
mMapView.onStart();
}
#Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onStop() {
super.onStop();
mMapView.onStop();
}
#Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
}
activity_main.xml (layout for main activity):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/fragment_container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
my_map_fragment.xml (layout for custom map fragment):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.views.MapView
android:id="#+id/mb_map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:access_token="#string/mb_token"/>
</FrameLayout>
Related
I have made a class Navigation which extends Fragment. When I'm using onCreate() method it is giving me error saying onCreate() method of the class clashes with the onCreate() method of android.support.v4.app.Fragment.
package com.beacons.railwayaid.tablayoutapp;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.customlbs.library.IndoorsFactory;
import com.customlbs.surface.library.IndoorsSurfaceFactory;
import com.customlbs.surface.library.IndoorsSurfaceFragment;
public class Navigation extends Fragment {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IndoorsFactory.Builder indoorsBuilder = new IndoorsFactory.Builder();
IndoorsSurfaceFactory.Builder surfaceBuilder = new IndoorsSurfaceFactory.Builder();
indoorsBuilder.setContext(getContext());
indoorsBuilder.setApiKey("69af5e85-007a-4df5-8715-d1c2c22e206f");
indoorsBuilder.setBuildingId((long) 979231679);
surfaceBuilder.setIndoorsBuilder(indoorsBuilder);
IndoorsSurfaceFragment indoorsFragment = surfaceBuilder.build();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, indoorsFragment, "indoors");
transaction.commit();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2_railway_navigation, container, false);
return rootView;
}
}
Change:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
to:
#Override
public void onCreate(Bundle savedInstanceState) {
...
}
Try to move your code to the onActivityCreated method like this:
public class Navigation extends Fragment {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2_railway_navigation, container, false);
return rootView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
IndoorsFactory.Builder indoorsBuilder = new IndoorsFactory.Builder();
IndoorsSurfaceFactory.Builder surfaceBuilder = new IndoorsSurfaceFactory.Builder();
indoorsBuilder.setContext(getContext());
indoorsBuilder.setApiKey("69af5e85-007a-4df5-8715-d1c2c22e206f");
indoorsBuilder.setBuildingId((long) 979231679);
surfaceBuilder.setIndoorsBuilder(indoorsBuilder);
IndoorsSurfaceFragment indoorsFragment = surfaceBuilder.build();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, indoorsFragment, "indoors");
transaction.commit();
}
}
The onMapReady function is never called. It needs to be triggered for proper initializating of my GoogleMap.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
}
Should I inherit MapFragment instead of Fragment? Because I get another Error when I do: it returns a NullPointerException in MainActivity
import android.os.Bundle;
import android.support.v4.app.Fragment;
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.OnMapReadyCallback;
public class MapsFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_maps, container, false);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name=".fragment.MapsFragment"
android:id="#+id/maps_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>
</LinearLayout>
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.MapsFragment" />
Many thanks guys!
Update 1:
Added this to MapsActivity, but now MainActivity returns android.view.InflateException: Binary XML file line #6: Error inflating class fragment
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
}
Try This
public class MapsFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.activity_maps, container, false);
SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
return mView ;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
}
Your MapsFragment should request getMapAsync to load map then only onMapReady will be called.
public class MapsFragment extends Fragment implements OnMapReadyCallback
{
private View fragmentView;
private GoogleMap mMap;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
fragmentView = inflater.inflate(R.layout.activity_maps, container, false);
return fragmentView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
}
}
here is the correct, workings solution:
MapsFragment
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class MapsFragment extends SupportMapFragment {
private GoogleMap mapView;
private void initGoogleMap() {
getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
mapView = googleMap;
}
});
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initGoogleMap();
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private MapsFragment mapsFragment;
private void initializeMapsFragment() {
FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
mapsFragment = new MapsFragment();
SupportMapFragment supportMapFragment = mapsFragment;
mTransaction.add(R.id.google_maps_frame, supportMapFragment);
mTransaction.commit();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
initializeMapsFragment();
}
}
main_activity.xml, removed maps_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/google_maps_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="-8dp"
android:layout_weight="0.2" >
</FrameLayout>
</LinearLayout>
I have a google map app that is using a mapview in a fragment to display maps. This is working perfectly except that it doesn't display indoor maps.
I decided to create a stripped down version of my code to find the problem. It still exists :-(
In the documentation for indoor maps it says the following
SetIndoorEnabled
Sets whether indoor maps should be enabled. Currently, indoor maps can
only be shown on one map at a time and by default, this is the first map
added to your application. To enable indoor maps on another map, you
must first disable indoor maps on the original map. If you try to enable
indoor maps when it is enabled on another map, nothing will happen and
this will return false. When Indoor is not enabled for a map, all
methods related to indoor will return null, or false.
I can't see that I've enabled multiple maps but I'm wondering if somehow the use of a fragment confuses it.
Any help/advice would be greatly appeciated.
My MainActivity.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Fragment map_fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
showTheMap();
}
public void showTheMap() {
map_fragment = new MapFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.map, map_fragment);
ft.commit();
}
}
The MapFrament.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
public class MapFragment extends Fragment {
private MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final FrameLayout parent = (FrameLayout) inflater.inflate(R.layout.fragment_map, container, false);
mapView = (MapView) parent.findViewById(R.id.mapfragment);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(-35.0176402, 138.5459909), 17);
googleMap.animateCamera(cameraUpdate);
}
}
);
mapView.onCreate(savedInstanceState);
return parent;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onPause() {
super.onPause();
mapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
activity_main_2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</android.support.design.widget.CoordinatorLayout>
The fragment_map.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="#+id/mapfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</FrameLayout>
If you move the code to MainActivity it works perfectly. See code below
MainActivity.Java
public class MainActivity extends AppCompatActivity {
MapView mapView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_1);
mapView = (MapView) findViewById(R.id.map);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(-35.0176402, 138.5459909), 17);
googleMap.animateCamera(cameraUpdate);
}
}
);
mapView.onCreate(savedInstanceState);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public void onResume() {
super.onResume();
mapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
#Override
protected void onStop() {
super.onStop();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
The activity_main_1 xml for this is ....
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.program.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</android.support.design.widget.CoordinatorLayout>
The following is a solution for my problem.
Declare myGoogleMap in the MainActivity
public static GoogleMap myGoogleMap = null;
In onMapReady add these lines as the first action to do
if (MainActivity.myGoogleMap != null) {
MainActivity.myGoogleMap.setIndoorEnabled(false);
googleMap.setIndoorEnabled(true);
MainActivity.myGoogleMap = googleMap;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(-35.0176402, 138.5459909), 17);
MainActivity.myGoogleMap.animateCamera(cameraUpdate);
}
It seems as though getMapAsync is called twice and therefore 2 google maps are created, the second one is being displayed.
As indoor maps only appear on the first map you have to setIndoorEnabled to false for the first map and then setIndoorEnabled to true for the second one.
This seems dodgy to me.
Not sure if its a bug or my lack of understanding.
There is one activity MainActivity.
there are two fragmnet
FragmentOne
FragmentTwo
Q.1 - initially first fragment loaded with activity.
and all the method related to this fragment called(like onAttach, oncreate, onCreateview, onViewCreated, onActivityCreated).
but when i click on button2(see above xml). it will not call on pause of the already loaded fragment, it will again start
new fragment with start(onAttach, oncreate, onCreateview, onViewCreated, onActivityCreated). so how this work?
Q.2 - IS it necessary to always
define android:name="com.example.fragexample.FragmentOne" class name with fragment when making defining fragment tab?
This is my main Activity layout
<?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:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<Button
android:id="#+id/btn_go_to_next_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go To Home Activity"
android:onClick="goToNextActivity"
/>
<fragment
android:name="com.example.fragexample.FragmentOne"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Fragment One Code
package com.example.fragexample;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
public static String FragmentOneTag = "FragmentOne";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
//Toast.makeText(getActivity(), "FragmentOne - "+container+" bundle -- "+savedInstanceState, Toast.LENGTH_LONG).show();
Log.i(FragmentOneTag, "onCreateView: "+"inflater -- "+inflater+" container -- "+container+" bundle -- "+savedInstanceState);
//Log.i(FragmentOneTag, "inflater -- "+inflater+" container -- "+container+" bundle -- "+savedInstanceState);
return inflater.inflate(R.layout.fragment_one, container, false);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.i(FragmentOneTag, "onAttach"+" Activity:"+activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(FragmentOneTag, "onCreate: savedInstanceState:"+savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(FragmentOneTag, "onActivityCreated"+" savedInstanceState"+savedInstanceState);
}
#Override
public void onStart() {
super.onStart();
Log.i(FragmentOneTag, "onStart");
}
#Override
public void onResume() {
super.onResume();
Log.i(FragmentOneTag, "onResume");
}
#Override
public void onPause() {
super.onPause();
Log.i(FragmentOneTag, "onPause");
}
#Override
public void onStop() {
super.onStop();
Log.i(FragmentOneTag, "onStop");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i(FragmentOneTag, "onDestroy");
}
#Override
public void onDestroyView() {
super.onDestroyView();
Log.i(FragmentOneTag, "onDestroyView");
}
#Override
public void onDetach() {
super.onDetach();
Log.i(FragmentOneTag, "onDetach");
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(FragmentOneTag, "onViewCreated "+"view:"+view+" savedInstanceState:"+savedInstanceState);
}
}
Fragment Two Code
package com.example.fragexample;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment{
private static String FragmentTWOTag = "FragmentTwo";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
Log.i(FragmentTWOTag, "onCreateView");
// Toast.makeText(getActivity(),"inflater -- "+inflater+" FragmentTWo - "+container+" bundle -- "+savedInstanceState, Toast.LENGTH_LONG).show();
Log.i("FragmentTwo", "inflater -- "+inflater+" container -- "+container+" bundle -- "+savedInstanceState);
return inflater.inflate(
R.layout.fragment_two, container, false);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.i(FragmentTWOTag, "onAttach");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(FragmentTWOTag, "onCreate");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(FragmentTWOTag, "onActivityCreated");
}
#Override
public void onStart() {
super.onStart();
Log.i(FragmentTWOTag, "onStart");
}
#Override
public void onResume() {
super.onResume();
Log.i(FragmentTWOTag, "onResume");
}
#Override
public void onPause() {
super.onPause();
Log.i(FragmentTWOTag, "onPause");
}
#Override
public void onStop() {
super.onStop();
Log.i(FragmentTWOTag, "onStop");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i(FragmentTWOTag, "onDestroy");
}
#Override
public void onDestroyView() {
super.onDestroyView();
Log.i(FragmentTWOTag, "onDestroyView");
}
#Override
public void onDetach() {
super.onDetach();
Log.i(FragmentTWOTag, "onDetach");
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
Log.i(FragmentTWOTag, "onViewCreated");
}
}
i am showing intial screen with first fragment.
first screen have two options button
Fragment No.1 and Fragment No.2
click on Fragment No.1 will load first fragment
click on Fragment No.2 will load second fragment
please guide me how its working.
Thanks In advance.
I get the error
Unable to start activity ComponentInfo{de.androidbuch.activiti/de.androidbuch.activiti.task.Activity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
i read Error inflating class fragment in Android android,
Error inflating class fragment
but it does not work for me
package com.example.fragments;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
public class MainActivity extends FragmentActivity {
String LOG_TAG = "myLogs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_TAG, "MainActivity onCreate");
}
protected void onStart() {
super.onStart();
Log.d(LOG_TAG, "MainActivity onStart");
}
protected void onResume() {
super.onResume();
Log.d(LOG_TAG, "MainActivity onResume");
}
protected void onPause() {
super.onPause();
Log.d(LOG_TAG, "MainActivity onPause");
}
protected void onStop() {
super.onStop();
Log.d(LOG_TAG, "MainActivity onStop");
}
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "MainActivity onDestroy");
}
}
Fragment 1
package com.example.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag1 extends Fragment {
final String LOG_TAG = "myLogs";
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(LOG_TAG, "Fragment1 onAttach");
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "Fragment1 onCreate");
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag1, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(LOG_TAG, "Fragment1 onActivityCreated");
Log.d(LOG_TAG, "Fragment1 onCreateView");
}
public void onStart() {
super.onStart();
Log.d(LOG_TAG, "Fragment1 onStart");
}
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "Fragment1 onResume");
}
public void onPause() {
super.onPause();
Log.d(LOG_TAG, "Fragment1 onPause");
}
public void onStop() {
super.onStop();
Log.d(LOG_TAG, "Fragment1 onStop");
}
public void onDestroyView() {
super.onDestroyView();
Log.d(LOG_TAG, "Fragment1 onDestroyView");
}
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "Fragment1 onDestroy");
}
public void onDetach() {
super.onDetach();
Log.d(LOG_TAG, "Fragment1 onDetach");
}
}
Fragment 2
package com.example.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag2 extends Fragment {
final String LOG_TAG = "myLogs";
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(LOG_TAG, "Fragment1 onAttach");
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "Fragment1 onCreate");
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(LOG_TAG, "Fragment1 onCreateView");
return inflater.inflate(R.layout.frag2, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(LOG_TAG, "Fragment1 onActivityCreated");
}
public void onStart() {
super.onStart();
Log.d(LOG_TAG, "Fragment1 onStart");
}
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "Fragment1 onResume");
}
public void onPause() {
super.onPause();
Log.d(LOG_TAG, "Fragment1 onPause");
}
public void onStop() {
super.onStop();
Log.d(LOG_TAG, "Fragment1 onStop");
}
public void onDestroyView() {
super.onDestroyView();
Log.d(LOG_TAG, "Fragment1 onDestroyView");
}
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "Fragment1 onDestroy");
}
public void onDetach() {
super.onDetach();
Log.d(LOG_TAG, "Fragment1 onDetach");
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:name="ru.startandroid.develop.p1041fragmentlifecycle.Fragment1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
</fragment>
<fragment
android:name="ru.startandroid.develop.p1041fragmentlifecycle.Fragment2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
</fragment>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="frag1_text" >
</TextView>
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="frag2_text" >
</TextView>
When you use
android:name="ru.startandroid.develop.p1041fragmentlifecycle.Fragment1"
you need to provide the package name. From your frag1.java file the package name is package com.example.fragments; So for the xml file you would want to use
android:name="com.example.fragments.Frag1"
or just for short:
android:name=".Frag1"
See this for more details.