1
I’m creating an app with 2 tabs in Swipe, loading Fragments. Since the first is a Listfragment (which is ok), the second should load a map, but, after I tried to insert, crashes the application and displays the error:
06-10 16:16:47.312 30209-30209/br.com.toyansk.swipeabletabs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: br.com.toyansk.swipeabletabs, PID: 30209
java.lang.NullPointerException
at br.com.toyansk.swipeabletabs.MapaFragment$HttpGetTask.onPostExecute(MapaFragment.java:93)
at br.com.toyansk.swipeabletabs.MapaFragment$HttpGetTask.onPostExecute(MapaFragment.java:62)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
06-10 16:16:47.316 1037-11772/? W/ActivityManager﹕ Force finishing activity br.com.toyansk.swipeabletabs/.MainActivity
Mainactivity is like this:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
List<Fragment> mFragments;
// Tab titles
private String[] tabs = { "Lista", "Mapa" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), mFragments);
//mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
mFragments = new ArrayList<Fragment>();
mFragments.add(new List_Fragment());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
final int[] ICON = new int[] {
R.drawable.ic1,
R.drawable.ic2
};
// Adding Tabs
for (int i=0; i < tabs.length; i++) {
actionBar.addTab(actionBar.newTab()
.setIcon(getResources().getDrawable(ICON[i]))
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
Mapafragment, which should load the map, also has functions to insert markers into the map through data from a JSON, and so on:
public class MapaFragment extends Fragment {
Context mContext;
private GoogleMap mMap;
private Location location;
private final static String URL = "url da api que retorna em JSON os locais no mapa";
public static final String TAG = "MapaFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mapa, container, false);
new HttpGetTask().execute(URL);
return rootView;
}
private class HttpGetTask extends AsyncTask<String, Void, List<PDVRec>> {
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
@Override
protected List<PDVRec> doInBackground(String... params) {
HttpGet request = new HttpGet(params[0]);
JSONResponseHandler responseHandler = new JSONResponseHandler();
try {
// Get data in JSON format
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException e) {
Log.i(TAG, "ClientProtocolException");
} catch (IOException e) {
Log.i(TAG, "IOException");
}
return null;
}
@Override
protected void onPostExecute(List<PDVRec> result) {
// Get Map Object
mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (null != mMap) {
// Add a marker for every earthquake
for (PDVRec rec : result) {
// Add a new marker for this earthquake
mMap.addMarker(new MarkerOptions()
// Set the Marker's position
.position(new LatLng(rec.getLat(), rec.getLng()))
// Set the title of the Marker's information window
.title(String.valueOf(rec.getNome_PDV()))
// Set the icon for the Marker
.icon(BitmapDescriptorFactory.fromResource(R.drawable.currentlocation_icon)));
}
LocationManager locationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 17));
CameraPosition cameraPosition = new CameraPosition.Builder()
// Sets the center of the map to location user
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.zoom(17) // Sets the zoom
//.bearing(90) // Sets the orientation of the camera to east
//.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
}
if (null != mClient)
mClient.close();
}
}
}
And the layout for the map is like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:id="@+id/container">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
What could be wrong?
It may be by
result
wasnull
.– ramaral
But the result is not null... When I enter the url of the api (String URL), it returns normally. Here I omitted this data and a few more classes for JSON loading.
– Atoyansk
Error indicates that you are using an object that is null in the method
onPostExecute()
class Httpgettask, you’ll have to find out what he is.– ramaral
This error happened when I tried to "adapt" these methods into a Fragment. In a previous test, using an Activity, it worked normal... Any idea?
– Atoyansk
If posted all the code of Fragment, the
mContext
is not being initialized.– ramaral
Yeah, I posted the whole code. The mContext I have doubts if I did correct... It was an attempt to adapt to the Fragment, since the getSystemService was making a mistake.
– Atoyansk