XML : including map view layout to my fragment has an NullPointerException

i created a xml for my map and a class to handle it, and im including that view with the tag in another fragment that i have, the thing its when i try to start that part of my proyect i got a NullPointerException, ill paste you the code so you can check:

xml map:

  <fragment 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:id="@+id/map"  tools:context=".MapsActivity"  android:name="com.google.android.gms.maps.SupportMapFragment" />    

map class to handle it:

  public class MapaPrincipal extends FragmentActivity implements OnMapReadyCallback {  @Override  protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.mapa_principal);      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()              .findFragmentById(R.id.map);      mapFragment.getMapAsync(this);  }    @Override  public void onMapReady(GoogleMap map) {      // Add a marker in Sydney, Australia, and move the camera.      LatLng sydney = new LatLng(-34, 151);      map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));      map.moveCamera(CameraUpdateFactory.newLatLng(sydney));  }    

}

xml inluding the map:

  <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=".FragmentoPrincipalUsuario">    <!-- TODO: Update blank fragment layout -->      <LinearLayout      android:orientation="horizontal"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:layout_gravity="center_horizontal|top">        <include layout="@layout/mapa_principal"/>    </LinearLayout>    <LinearLayout      android:orientation="vertical"      android:layout_width="match_parent"      android:layout_height="149dp"      android:layout_gravity="center_horizontal|bottom">        <Button          android:layout_width="117dp"          android:layout_height="wrap_content"          android:text="pedir taxi"          android:id="@+id/pedirTaxi"          android:layout_gravity="center_horizontal" />    </LinearLayout>    

fragment class including the map:

public class FragmentoPrincipalUsuario extends Fragment {

  private OnFragmentInteractionListener mListener;  private List<ParseUser> mChoferes;  private ParseUser       mUsuarioActual;  private Button          mPedido;        public FragmentoPrincipalUsuario() {      // Required empty public constructor  }      @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container,                           Bundle savedInstanceState) {        //conseguir usuario actual      // mUsuarioActual = ParseUser.getCurrentUser();          View x = inflater.inflate(R.layout.fragmento_principal_usuario, container, false);              //evento boton pedir taxi      mPedido.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {                ParseQuery<ParseUser> query = ParseUser.getQuery();              query.whereEqualTo("tipoUsuario", "chofer");              query.findInBackground(new FindCallback<ParseUser>() {                  public void done(List<ParseUser> choferes, com.parse.ParseException e) {                      if (e == null) {                            mChoferes = choferes;                          String nombreChofer;                            for (ParseUser chofer : mChoferes) {                                nombreChofer = chofer.getUsername();                                ParseObject gameScore = new ParseObject("Viaje");                              gameScore.put("cliente", "hola");                              gameScore.put("chofer", nombreChofer);                              gameScore.put("tomado", false);                              gameScore.saveInBackground();                              }                      } else {                          // Something went wrong.                      }                  }              });            }      });            // Inflate the layout for this fragment      return x;    }      // TODO: Rename method, update argument and hook method into UI event  public void onButtonPressed(Uri uri) {      if (mListener != null) {          mListener.onFragmentInteraction(uri);        }  }    @Override  public void onAttach(Context context) {      super.onAttach(context);      if (context instanceof OnFragmentInteractionListener) {          mListener = (OnFragmentInteractionListener) context;      } else {          throw new RuntimeException(context.toString()                  + " must implement OnFragmentInteractionListener");      }  }    @Override  public void onDetach() {      super.onDetach();      mListener = null;  }          /**   * This interface must be implemented by activities that contain this   * fragment to allow an interaction in this fragment to be communicated   * to the activity and potentially other fragments contained in that   * activity.   * <p/>   * See the Android Training lesson <a href=   * "http://developer.android.com/training/basics/fragments/communicating.html"   * >Communicating with Other Fragments</a> for more information.   */  public interface OnFragmentInteractionListener {      // TODO: Update argument type and name      void onFragmentInteraction(Uri uri);  }    

}

Main Drawer wich show the fragment:

  public class DrawerPrincipal extends AppCompatActivity      implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {    @Override  protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_drawer_principal);      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);      setSupportActionBar(toolbar);        //cargar fragment principal usuario          Fragment fragmento= null;      fragmento = new FragmentoPrincipalUsuario();     getSupportFragmentManager().beginTransaction()             .replace(R.id.content_main, fragmento)             .commit();            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(              this, 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(this);      }    @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.pantalla_principal_usuario, 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);  }    @SuppressWarnings("StatementWithEmptyBody")  @Override  public boolean onNavigationItemSelected(MenuItem item) {      // Handle navigation view item clicks here.      int id = item.getItemId();        if (id == R.id.cuenta) {          // Handle the camera action      } else if (id == R.id.historial_viajes) {        } else if (id == R.id.contacto) {        } else if (id == R.id.compartir) {        } else if (id == R.id.version) {        } else if (id == R.id.salir) {        }        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);      drawer.closeDrawer(GravityCompat.START);      return true;  }    @Override  public void onFragmentInteraction(Uri uri) {    }    

}

No comments:

Post a Comment