NPE at adding Drawer Listener [duplicate] - android

This question already has answers here:
Can not find a View with findViewById()
(4 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am currently trying to program a small app, now I am trying to use a Navigation Drawer in my app. This does not work with the reason that I get a NPE at drawer.addDrawerListener(toggle);. Everything else seems to be fine as far as I can say and the app worked correctly till adding the drawer. Thx in advance.
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import de.nocompany.gotthold.gw2companion.Tools.GwApiAccess;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private Toolbar toolbar;
private ActionBarDrawerToggle toggle;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
setContentView(R.layout.activity_main);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
//getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
drawer.addDrawerListener(toggle);
navigationView.setNavigationItemSelectedListener(this);
ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
View navHeader = navigationView.getHeaderView(0);
if (networkInfo != null && networkInfo.isConnected()) {
TextView connectionStatus = (TextView) navHeader.findViewById(R.id.connection_status);
connectionStatus.setText(R.string.common_online);
} else {
TextView connectionStatus = (TextView) navHeader.findViewById(R.id.connection_status);
connectionStatus.setText(R.string.common_offline);
}
toggle.syncState();
GwApiAccess gwApiAccess = new GwApiAccess(this,"https://api.guildwars2.com/v2/items/30704?lang=de");
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
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.main, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = drawer.isDrawerOpen(GravityCompat.START);
return super.onPrepareOptionsMenu(menu);
}
#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.nav_profile) {
Intent intent = new Intent(this,profile.class);
startActivity(intent);
} else if (id == R.id.nav_itemDB) {
Intent intent = new Intent(this,items.class);
startActivity(intent);
} else if (id == R.id.nav_guilds) {
Intent intent = new Intent(this,guilds.class);
startActivity(intent);
} else if (id == R.id.nav_maps) {
Intent intent = new Intent(this,map.class);
startActivity (intent);
} else if (id == R.id.settings) {
Intent intent = new Intent(this,SettingsActivity.class);
startActivity (intent);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

First change this part
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
setContentView(R.layout.activity_main);
to this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
Because you are not set layout to Actiivty before that you have to access it inner element so that the Studio can't find it and it give you NullpointerExeception.

Related

Application Crashes when using Intent

My application crashes after trying to login.
i created navigationActivity as CHome which i want my user to be directed to. But it keeps crashing :
Unfortnately your app has stopped working.
But when i disabled this lines below it shows the Toast and no crash.
Help please
Toast.makeText(LoginActivity.this, "CUSTOMER",Toast.LENGTH_SHORT).show();
Intent cHome = new Intent(LoginActivity.this, CHome.class);
Common.currentUser = user;
startActivity(cHome);
finish();
and i have my manifest here
my CHome is as follows
package com.example.oracle.eliteafro;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
//import com.example.oracle.eliteafro.Common.Common;
//import com.google.firebase.database.DatabaseReference;
//import com.google.firebase.database.FirebaseDatabase;
public class CHome extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//Init Firebase
//FirebaseDatabase database;
// DatabaseReference salons;
TextView loginName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
//Init Firebase
// database = FirebaseDatabase.getInstance();
//salons = database.getReference("el_af_salon");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
//set name for user
View headerView = navigationView.getHeaderView(0);
loginName = (TextView) findViewById(R.id.loginName);
loginName.setText("Beedy");
//loginName.setText(Common.currentUser.getFullname());
}
#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.chome, 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.nav_camera) {
// // Handle the camera action
// } else if (id == R.id.nav_gallery) {
//
// } else if (id == R.id.nav_slideshow) {
//
// } else if (id == R.id.nav_manage) {
//
// } else if (id == R.id.nav_share) {
//
// } else if (id == R.id.nav_send) {
//
// }
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Declare CHome as activity in your AndroidManifest.xml
UPDATE
I saw your updated question, so the problem is on your CHome activity, you are not getting the reference of your TextView before set the text.
TextView yourTextView = findViewById(R.id.your_text_view_id);
yourTextView.setText("your text");
UPDATE 2
Ok, I think that you need to pass parameters, you could pass parameters using extras.
Intent cHome = new Intent(LoginActivity.this, CHome.class);
cHome.putExtra("user", user);
startActivity(cHome);
finish();
Then on your CHome activity class you need to receive the extra :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
User user = (User) getIntent().getSerializableExtra("user");
//now you can use your user object
TextView yourTextView = findViewById(R.id.your_text_view_id);
yourTextView.setText(user.getName()); // for example
}
Don't forget your User class has to implements Serializable
The problem is inside of your onCreate method for CHome, you are references a null TextView. So either, you forgot to call
TextView textView = findViewById(R.id.your_text_view_id);
Or, maybe you did call that method but you passed in a id to a view that doensn't exist in your layout because findViewById will fail silently and return null instead of throughing an exception. So when you call setText() on the TextView you get a NullPointerexception.

How would I convert this activity to a Fragment?

Below is my Main Activity with Navigation drawer. However I've realised that for my Fragment Transaction, this cant be an activity it has to be a fragment. Is it possible to convert to a fragment or shall I start again..
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.main, 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);
}
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Fragment fragment = null;
int id = item.getItemId();
String get_title = item.getTitle().toString();
if (id == R.id.my_account) {
fragment = new my_account_fragment();
} else if (id == R.id.nav_media) {
fragment = new media_photo_fragment();
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.MainActivity, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

Cannot resolve Method 'replace (int)' for Fragment

im using the Navigation Drawer Activity and im stucked at one problem. I get this error message
Cannot resolve Method 'replace (int)
I was watching a tutorial but the guy doesnt get the error message somehow. I was also trying to solve the problem by reading other posts but it confused me even more.
My Code looks like this:
MainActivity.java
package androfenix.mycvapp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.addDrawerListener(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.main, 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.nav_aboutme) {
FragmentAboutme fragmentAboutme = new FragmentAboutme();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relativelayout_for_fragment).commit(fragmentAboutme);
} else if (id == R.id.nav_cv) {
} else if (id == R.id.nav_skills) {
} else if (id == R.id.nav_projects) {
} else if (id == R.id.nav_download) {
} else if (id == R.id.nav_website) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
And the layout fragment which i want to replace is this one:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativelayout_for_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="androfenix.mycvapp.MainActivity"
tools:showIn="#layout/app_bar_main">
</android.support.constraint.ConstraintLayout>
Please Study about fragments
replace(R.id.relativelayout_for_fragment,"your about me fragment").commit();
why would you even pass fragment object in commit() method there is no such method;
Here FragmentTransaction.Commit() does not Require an Arguement
so instead of:
manager.beginTransaction().replace(R.id.relativelayout_for_fragment).commit(fragmentAboutme);
use:
manager.beginTransaction().replace(R.id.relativelayout_for_fragment, fragmentAboutme).commit();
Also make sure your FragmentAboutme is extending the support library
import android.support.v4.app.Fragment;

add listview in navigationDrawerActivity

i am new in android. I have android studio 2.2.3.I create a project and select navigationDrawerActivity. when the Activity was opened, I could not see mDrawerListView.setAdapter() on the main activity. How can I add my custom adapter and listView instead menu activity main drawer layout?
package com.example.manifest.navigationdrawerexample;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.main, 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.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I solved my problem by this link.

Using custom tiles in Google Maps

I am pretty new to the Google Maps API v2 and would like to integrate a map available via this base link: https://tiles.guildwars2.com/{continent_id}/{floor}/{zoom}/{x}/{y}.jpg. My problem is, well everything, but let's get started with the following:
1) How can I set the map sizes that there is no grey space at any side?
2) How can I move the map correctly because I have obviously 4 parameters while I can change only 3 at a time with getTileUrl?
3) Why are the tiles mixed and not shown in their correct order?
The map documentation is available here:
http://wiki.guildwars2.com/wiki/API:2/maps
http://wiki.guildwars2.com/wiki/API:Maps
Working example link: 0.jpg
My current code is:
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
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.*;
import java.net.MalformedURLException;
import java.net.URL;
public class mapsContainer extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback {
SupportMapFragment mapFragment;
private DrawerLayout drawer;
private Toolbar toolbar;
private ActionBarDrawerToggle toggle;
private NavigationView navigationView;
private GoogleMap mMap;
private TileOverlay mSelectedTileOverlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapFragment = SupportMapFragment.newInstance();
setContentView(R.layout.activity_maps_container);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
drawer.addDrawerListener(toggle);
navigationView.setNavigationItemSelectedListener(this);
ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
View navHeader = navigationView.getHeaderView(0);
if (networkInfo != null && networkInfo.isConnected()) {
TextView connectionStatus = (TextView) navHeader.findViewById(R.id.connection_status);
connectionStatus.setText(R.string.common_online);
} else {
TextView connectionStatus = (TextView) navHeader.findViewById(R.id.connection_status);
connectionStatus.setText(R.string.common_offline);
}
toggle.syncState();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.mapsFragement, mapFragment).commit();
mapFragment.getMapAsync(this);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
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.main, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = drawer.isDrawerOpen(GravityCompat.START);
return super.onPrepareOptionsMenu(menu);
}
#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.syncProfile) {
//String res = gwApiAccess.apiQueryAuthorized("https://api.guildwars2.com/v2/account/dyes");
//Log.d(DEBUG_TAG,"res: " + res);
}
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.nav_home) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} else if (id == R.id.nav_profile) {
Intent intent = new Intent(this, profile.class);
startActivity(intent);
} else if (id == R.id.nav_itemDB) {
Intent intent = new Intent(this, items.class);
startActivity(intent);
} else if (id == R.id.nav_guilds) {
Intent intent = new Intent(this, guilds.class);
startActivity(intent);
} else if (id == R.id.nav_maps) {
//Do nothing
} else if (id == R.id.settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
protected void onStop() {
super.onStop();
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
CustomUrlTileProvider mTileProvider = new CustomUrlTileProvider(256, 256, "https://tiles.guildwars2.com/{continent_id}/{floor}/{zoom}/{x}/{y}.jpg");
mSelectedTileOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mTileProvider).zIndex(-1));
mMap.getUiSettings().setTiltGesturesEnabled(false);
mMap.getUiSettings().setRotateGesturesEnabled(false);
}
private static class CustomUrlTileProvider extends UrlTileProvider {
private String baseUrl;
CustomUrlTileProvider(int width, int height, String url) {
super(width, height);
this.baseUrl = url;
}
#Override
public URL getTileUrl(int zoom, int x, int y) {
try {
return new URL(baseUrl.replace("{continent_id}", "" + 1).replace("{floor}", "" + 0).replace("{zoom}", "" + zoom).replace("{x}", "" + x).replace("{y}", "" + y));
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
private boolean checkTileExists(int x, int y, int zoom) {
int minZoom = 0;
int maxZoom = 0;
return !(zoom < minZoom || zoom > maxZoom);
}
}
}
Ok, after having a looooong break because of school I figured everything out except the first thing.
My fault with the wrong tiles was that I did not have the correct order of params.
The other thing with the to much params, I will program myself a workaround in the near future.
So I will close the thread and open a new one just before I smash the display because sth doesn't work.

Categories

Resources