我正在尝试显示textview
中Toolbar
中的当前位置,每当我到达该位置时,应用程序崩溃。因为它无法在TextView
的{{1}}中显示该位置。需要帮助。
这是mainactivity的代码
toolbar
}
现在我获取了当前位置的地址,但每当它试图在public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, LocationListener {
private TextView address;
private LocationManager locationManager;
private boolean gps;
private double Latitude;
private double Longitude;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
address = (TextView) findViewById(R.id.textView2);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gps = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
if(!gps){
showSettingAlert();
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
{
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET}, 10);
return;
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, (LocationListener) this);
}
}
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);
}
private void showSettingAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("GPS settings");
alertDialog.setMessage("GPS is off.Do you want to go to the Settings menu?");
// positive button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
MainActivity.this.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates((LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
Latitude = (location.getLatitude());
Longitude = (location.getLongitude());
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = new ArrayList<>();
try {
addresses = geocoder.getFromLocation(Latitude,Longitude,1);
} catch (IOException e) {
e.printStackTrace();
}
String cityName = addresses.get(0).getLocality().toString();
String sublocal = addresses.get(0).getSubLocality().toString();
String addr = sublocal + ", " +cityName;
address.setText(addr);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
@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) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
} 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;
}
中显示时,它都显示错误和应用程序崩溃。这是address = (TextView) findViewById(R.id.searchEditText);
toolbar.xml
LogCat 就在这里。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:popupTheme="@style/AppTheme.PopupOverlay"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<TextView
android:id="@+id/searchEditText"
android:text="Getting Location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
答案 0 :(得分:1)
使用此
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
address = (TextView) toolbar.findViewById(R.id.searchEditText);
而不是这个
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
address = (TextView) findViewById(R.id.searchEditText);
修改强>
List<Address> addresses = new ArrayList();
答案 1 :(得分:0)
试试这个
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
address = (TextView)toolbar.findViewById(R.id.searchEditText);