Android: 我在操作栏中设置了一些图标,但它们没有显示在我的设备上
Android: I set some icons in the actionbar, but they were not shown on my device
我想在我的操作栏中添加一些操作。在我的设计预览中显示了图标,但在我的设备上操作栏中没有显示图标。我必须更改什么才能在操作栏中显示图标?
menu_main.xml
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_mylocation"
android:title="Refresh"
app:showAsAction="always" />
<item
android:id="@+id/action_settings"
android:title="Settings">
</item>
</menu>
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java:
package com.example.myApp;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
private LocationManager locationManager = null;
private LocationListener locationListener = null;
private Button btnGetLocation = null;
private TextView v_longitude = null;
private TextView v_latitude = null;
private TextView v_location = null;
private TextView v_speed = null;
private TextView v_altitude = null;
private TextView v_sat = null;
private TextView v_gps_status = null;
private TextView v_update_status = null;
String longitude = "";
String latitude = "";
String location = "";
String speed = "";
String altitude = "";
String sat_nr = "";
String myOrt = "";
String myBeschreibung = "";
Button button3;
ImageView imageView;
private JsonPlaceHolderApi jsonPlaceHolderApi;
private Toolbar toolbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //portrait mode
v_longitude = (TextView) findViewById(R.id.Lon_view);
v_latitude = (TextView) findViewById(R.id.Lat_view);
v_location = (TextView) findViewById(R.id.Loc_view);
v_speed = (TextView) findViewById(R.id.Spd_view);
v_altitude = (TextView) findViewById(R.id.Alt_view);
v_sat = (TextView) findViewById(R.id.Sat_view);
v_gps_status = (TextView) findViewById(R.id.Gps_status);
v_update_status = (TextView) findViewById(R.id.Update_status);
//---------------------------------------------------------
// Neu: mehrere Buttons
Button button1 = findViewById(R.id.button);
Button button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button3.setVisibility(View.GONE);
Button deleteOrt = findViewById(R.id.btnDeleteOrt);
Button deleteBeschreibung = findViewById(R.id.btnDeleteBeschreibung);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
deleteOrt.setOnClickListener(this);
deleteBeschreibung.setOnClickListener(this);
//---------------------------------------------------------
//btnGetLocation = (Button) findViewById(R.id.button);
//btnGetLocation.setOnClickListener(this);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
button1.performClick();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myBaseURL.de/api/mypost.php/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
//getEntries();
//createEntry();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
//Check if GPS is enabled
locationListener = new APPLocationListener();
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
v_gps_status.setText("Wait for signal");
v_gps_status.setTextColor(Color.parseColor("#0066ff"));
locationManager.requestLocationUpdates(LocationManager
.GPS_PROVIDER, 5000, 10, locationListener);
} else {
v_gps_status.setText("No GPS-Access!!!");
v_gps_status.setTextColor(Color.parseColor("#ff0000"));
}
Toast.makeText(this, "Refresh-Button clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
//---------------------------------------------------------------------
// Ort und Beschreibung werden nach Touch auf den Send-Button eingelesen
EditText etO = (EditText)findViewById(R.id.editOrt);
myOrt = (etO.getText().toString());
//
EditText etB = (EditText)findViewById(R.id.editBeschreibung);
myBeschreibung = (etB.getText().toString());
//Log.d("DEBUG", "eingabe_click = " + myOrt );
// --------------------------------------------------------------------
createEntry();
Toast.makeText(this, "Daten werden gesendet", Toast.LENGTH_SHORT).show();
break;
case R.id.btnDeleteOrt:
//---------------------------------------------------------------------
// Eintrag in Ort zurücksetzen
EditText etO2 = (EditText)findViewById(R.id.editOrt);
etO2.setText("", TextView.BufferType.EDITABLE);
myOrt = "";
break;
case R.id.btnDeleteBeschreibung:
//---------------------------------------------------------------------
// Eintrag in Beschreibung zurücksetzen
EditText etB2 = (EditText)findViewById(R.id.editBeschreibung);
etB2.setText("", TextView.BufferType.EDITABLE);
myBeschreibung = "";
break;
case R.id.button3:
//---------------------------------------------------------------------
// Activity Entry aufrufen
openOrte();
break;
}
}
//-----------------------------------------------------------------------------------------------
// Methode, um 2. Activity "Orte" zu öffnen
private void openOrte() {
Log.d("DEBUG", "lat = " + latitude );
Log.d("DEBUG", "lon = " + longitude );
// Parameter an andere Activity übergeben: (
// In order to pass the parameters you create new intent and put a parameter map:
Intent myIntent = new Intent (this,Orte.class);
myIntent.putExtra("paramLatitude",latitude);
myIntent.putExtra("paramLongitude",longitude);
startActivity(myIntent);
// Uri gmmIntentUri = Uri.parse("google.navigation:q=46.74015166666666,17.648755");
// Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
// mapIntent.setPackage("com.google.android.apps.maps");
// startActivity(mapIntent);
//
}
//-----------------------------------------------------------------------------------------------
private class APPLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
//Try to get city name
String city_name = null;
Geocoder geocoder = new Geocoder(getBaseContext(),Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (addresses.size() > 0)
city_name = addresses.get(0).getAddressLine(0);
// Geocoder geocoder = new Geocoder(this, Locale.getDefault());
// List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
// String cityName = addresses.get(0).getAddressLine(0);
// String stateName = addresses.get(0).getAddressLine(1);
// String countryName = addresses.get(0).getAddressLine(2);
} catch (IOException e) {
city_name = "unknown";
e.printStackTrace();
}
latitude = "" + loc.getLatitude(); //Get latitude
longitude = "" + loc.getLongitude(); //Get longitude
location = "" + city_name; //Get city name
speed = "" + loc.getSpeed()*3.6 + " km/h"; //Get speed in km/h
altitude = "" + loc.getAltitude() + " m über nn"; //Get height in meters
sat_nr = "" + loc.getExtras().getInt("Satelliten"); //available satellites
v_latitude.setText(latitude);
v_longitude.setText(longitude);
v_location.setText(location);
v_speed.setText(speed);
v_altitude.setText(altitude);
v_sat.setText(sat_nr);
v_gps_status.setText("GPS working");
v_gps_status.setTextColor(Color.parseColor("#33cc33"));
v_update_status.setText("Last update: " + myDatetime());
// Button3 sichtbar machen
//button3.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
imageView = (ImageView) findViewById(R.id.action_liste);
imageView.setVisibility(View.VISIBLE);
invalidateOptionsMenu();
//createPost(1);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
private void createEntry() {
Post post;
//if (Aufruf == 1) {
post = new Post(myDatetime(), longitude, latitude, location, altitude, sat_nr, myOrt, myBeschreibung);
//} else {
// post = new Post(myDatetime(), "", "", "NEW", "", "");
//}
Call<Post> call = jsonPlaceHolderApi.createPost(post);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
// HTTP-GET-REQUEST nicht erfolgreich
if (!response.isSuccessful()) {
toastMsg("is nopt successful");
return;
}
// HTTP-GET-REQUEST nicht erfolgreich
toastMsg(response.toString());
}
// HTTP-GET-REQUEST nicht erfolgreich
@Override
public void onFailure(Call<Post> call, Throwable t) {
toastMsg("Schwerer Fehler");
}
});
}
public void toastMsg(String msg) {
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 100);
toast.show();
}
public String myDatetime() {
Calendar c = Calendar.getInstance(); //Get time on system
String myDatetime = c.get(Calendar.HOUR_OF_DAY) + ":" +
c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
return myDatetime;
}
}
您没有初始化工具栏。在 setSupportActionBar(toolbar)
上方添加以下行
toolbar = findViewById(R.id.toolbar);
您必须在附加到操作栏之前初始化工具栏
private Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
....
}
我想在我的操作栏中添加一些操作。在我的设计预览中显示了图标,但在我的设备上操作栏中没有显示图标。我必须更改什么才能在操作栏中显示图标?
menu_main.xml
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_mylocation"
android:title="Refresh"
app:showAsAction="always" />
<item
android:id="@+id/action_settings"
android:title="Settings">
</item>
</menu>
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java:
package com.example.myApp;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
private LocationManager locationManager = null;
private LocationListener locationListener = null;
private Button btnGetLocation = null;
private TextView v_longitude = null;
private TextView v_latitude = null;
private TextView v_location = null;
private TextView v_speed = null;
private TextView v_altitude = null;
private TextView v_sat = null;
private TextView v_gps_status = null;
private TextView v_update_status = null;
String longitude = "";
String latitude = "";
String location = "";
String speed = "";
String altitude = "";
String sat_nr = "";
String myOrt = "";
String myBeschreibung = "";
Button button3;
ImageView imageView;
private JsonPlaceHolderApi jsonPlaceHolderApi;
private Toolbar toolbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //portrait mode
v_longitude = (TextView) findViewById(R.id.Lon_view);
v_latitude = (TextView) findViewById(R.id.Lat_view);
v_location = (TextView) findViewById(R.id.Loc_view);
v_speed = (TextView) findViewById(R.id.Spd_view);
v_altitude = (TextView) findViewById(R.id.Alt_view);
v_sat = (TextView) findViewById(R.id.Sat_view);
v_gps_status = (TextView) findViewById(R.id.Gps_status);
v_update_status = (TextView) findViewById(R.id.Update_status);
//---------------------------------------------------------
// Neu: mehrere Buttons
Button button1 = findViewById(R.id.button);
Button button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button3.setVisibility(View.GONE);
Button deleteOrt = findViewById(R.id.btnDeleteOrt);
Button deleteBeschreibung = findViewById(R.id.btnDeleteBeschreibung);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
deleteOrt.setOnClickListener(this);
deleteBeschreibung.setOnClickListener(this);
//---------------------------------------------------------
//btnGetLocation = (Button) findViewById(R.id.button);
//btnGetLocation.setOnClickListener(this);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
button1.performClick();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myBaseURL.de/api/mypost.php/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
//getEntries();
//createEntry();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
//Check if GPS is enabled
locationListener = new APPLocationListener();
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
v_gps_status.setText("Wait for signal");
v_gps_status.setTextColor(Color.parseColor("#0066ff"));
locationManager.requestLocationUpdates(LocationManager
.GPS_PROVIDER, 5000, 10, locationListener);
} else {
v_gps_status.setText("No GPS-Access!!!");
v_gps_status.setTextColor(Color.parseColor("#ff0000"));
}
Toast.makeText(this, "Refresh-Button clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
//---------------------------------------------------------------------
// Ort und Beschreibung werden nach Touch auf den Send-Button eingelesen
EditText etO = (EditText)findViewById(R.id.editOrt);
myOrt = (etO.getText().toString());
//
EditText etB = (EditText)findViewById(R.id.editBeschreibung);
myBeschreibung = (etB.getText().toString());
//Log.d("DEBUG", "eingabe_click = " + myOrt );
// --------------------------------------------------------------------
createEntry();
Toast.makeText(this, "Daten werden gesendet", Toast.LENGTH_SHORT).show();
break;
case R.id.btnDeleteOrt:
//---------------------------------------------------------------------
// Eintrag in Ort zurücksetzen
EditText etO2 = (EditText)findViewById(R.id.editOrt);
etO2.setText("", TextView.BufferType.EDITABLE);
myOrt = "";
break;
case R.id.btnDeleteBeschreibung:
//---------------------------------------------------------------------
// Eintrag in Beschreibung zurücksetzen
EditText etB2 = (EditText)findViewById(R.id.editBeschreibung);
etB2.setText("", TextView.BufferType.EDITABLE);
myBeschreibung = "";
break;
case R.id.button3:
//---------------------------------------------------------------------
// Activity Entry aufrufen
openOrte();
break;
}
}
//-----------------------------------------------------------------------------------------------
// Methode, um 2. Activity "Orte" zu öffnen
private void openOrte() {
Log.d("DEBUG", "lat = " + latitude );
Log.d("DEBUG", "lon = " + longitude );
// Parameter an andere Activity übergeben: (
// In order to pass the parameters you create new intent and put a parameter map:
Intent myIntent = new Intent (this,Orte.class);
myIntent.putExtra("paramLatitude",latitude);
myIntent.putExtra("paramLongitude",longitude);
startActivity(myIntent);
// Uri gmmIntentUri = Uri.parse("google.navigation:q=46.74015166666666,17.648755");
// Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
// mapIntent.setPackage("com.google.android.apps.maps");
// startActivity(mapIntent);
//
}
//-----------------------------------------------------------------------------------------------
private class APPLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
//Try to get city name
String city_name = null;
Geocoder geocoder = new Geocoder(getBaseContext(),Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (addresses.size() > 0)
city_name = addresses.get(0).getAddressLine(0);
// Geocoder geocoder = new Geocoder(this, Locale.getDefault());
// List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
// String cityName = addresses.get(0).getAddressLine(0);
// String stateName = addresses.get(0).getAddressLine(1);
// String countryName = addresses.get(0).getAddressLine(2);
} catch (IOException e) {
city_name = "unknown";
e.printStackTrace();
}
latitude = "" + loc.getLatitude(); //Get latitude
longitude = "" + loc.getLongitude(); //Get longitude
location = "" + city_name; //Get city name
speed = "" + loc.getSpeed()*3.6 + " km/h"; //Get speed in km/h
altitude = "" + loc.getAltitude() + " m über nn"; //Get height in meters
sat_nr = "" + loc.getExtras().getInt("Satelliten"); //available satellites
v_latitude.setText(latitude);
v_longitude.setText(longitude);
v_location.setText(location);
v_speed.setText(speed);
v_altitude.setText(altitude);
v_sat.setText(sat_nr);
v_gps_status.setText("GPS working");
v_gps_status.setTextColor(Color.parseColor("#33cc33"));
v_update_status.setText("Last update: " + myDatetime());
// Button3 sichtbar machen
//button3.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
imageView = (ImageView) findViewById(R.id.action_liste);
imageView.setVisibility(View.VISIBLE);
invalidateOptionsMenu();
//createPost(1);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
private void createEntry() {
Post post;
//if (Aufruf == 1) {
post = new Post(myDatetime(), longitude, latitude, location, altitude, sat_nr, myOrt, myBeschreibung);
//} else {
// post = new Post(myDatetime(), "", "", "NEW", "", "");
//}
Call<Post> call = jsonPlaceHolderApi.createPost(post);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
// HTTP-GET-REQUEST nicht erfolgreich
if (!response.isSuccessful()) {
toastMsg("is nopt successful");
return;
}
// HTTP-GET-REQUEST nicht erfolgreich
toastMsg(response.toString());
}
// HTTP-GET-REQUEST nicht erfolgreich
@Override
public void onFailure(Call<Post> call, Throwable t) {
toastMsg("Schwerer Fehler");
}
});
}
public void toastMsg(String msg) {
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 100);
toast.show();
}
public String myDatetime() {
Calendar c = Calendar.getInstance(); //Get time on system
String myDatetime = c.get(Calendar.HOUR_OF_DAY) + ":" +
c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
return myDatetime;
}
}
您没有初始化工具栏。在 setSupportActionBar(toolbar)
toolbar = findViewById(R.id.toolbar);
您必须在附加到操作栏之前初始化工具栏
private Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
....
}