0
It has three (3) buttons and a (1) tab with two (2) buttons on the map, like this that I add the code to create the Pageadapters I get error and I don’t know how to solve.
ERROR:
03-15 03:50:23.990 3051-3051/with.thiagosaadcorporation.servicescretin E/Androidruntime: FATAL EXCEPTION: main Process: with.thiagosaadcorporation.servicesecretaria, PID: 3051 java.lang.Runtimeexception: Unable to start Activity ComponentInfo{com.thiagosaadcorporation.atendimentosecretaria/com.thiagosaadcorporation.atendimentosecretaria.API.google.googleMaps.GoogleMapsActivity}: java.lang.Nullpointerexception at android.app.Activitythread.performLaunchActivity(Activitythread.java:2195) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2245) at android.app.Activitythread.access$800(Activitythread.java:135) at android.app.Activitythread$H.handleMessage(Activitythread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.Activitythread.main(Activitythread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:779) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:595) at Dalvik.system.Nativestart.main(Native Method) Caused by: java.lang.Nullpointerexception at com.thiagosaadcorporation.servicescretaria.API.google.googleMaps.Googlemapsactivity.setupViewPager(Googlemapsactivity.java:75) at com.thiagosaadcorporation.servicescretaria.API.google.googleMaps.Googlemapsactivity.onCreate(Googlemapsactivity.java:45) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2159) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2245) at android.app.Activitythread.access$800(Activitythread.java:135) at android.app.Activitythread$H.handleMessage(Activitythread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.Activitythread.main(Activitythread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:779) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:595) at Dalvik.system.Nativestart.main(Native Method)
Googlemapsacitivity.java
package com.thiagosaadcorporation.atendimentosecretaria.API.google.googleMaps;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.thiagosaadcorporation.atendimentosecretaria.API.fragments.SectionsPageAdapter;
import com.thiagosaadcorporation.atendimentosecretaria.API.fragments.tabs.MapInformationsFragment;
import com.thiagosaadcorporation.atendimentosecretaria.API.fragments.tabs.UserAccountFragment;
import com.thiagosaadcorporation.atendimentosecretaria.R;
public class GoogleMapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static final String TAG = "GoogleMapsActivity";
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_maps);
Log.d(TAG, "onCreate: STARTING..");
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new UserAccountFragment(), "TAB1");
adapter.addFragment(new MapInformationsFragment(), "TAB2");
viewPager.setAdapter(adapter);
}
}
Sectionspageadapter.java
package com.thiagosaadcorporation.atendimentosecretaria.API.fragments;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by saadt on 15/03/2018.
*/
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
The error is on line 75 of Googlemapsacitivity. Nullpointer probably caused by viewpager to be null.
– Max Fratane
@Maxfratane yes, the error I know where is. But I did according to the documentation and do not know what I should do.
– Thiago Saad
Already put a break points there and debugged? The viewpager is coming null, IE, findviewbyid did not find anything with the id you passed. Checks in xml if the id of the viewpager ta container.
– Max Fratane
@Maxfratane got it, it was the id that was wrong.
– Thiago Saad