我有一个具有多个活动的应用程序。由
创建的向上按钮getSupportActionBar().setDisplayHomeAsUpEnabled(true);
除一项活动外,还从事所有活动。我已经检查了清单,可以看到父母的设置正确。
来自活动按钮不起作用的活动的代码:
package com.icenibees.apiarymanager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.icenibees.apiarymanager.sample.SampleDataProvider;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.UUID;
public class ApiaryDetails extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
public static final String LOGTAG="APIARYMANAGER";
public String apiaryIDDelete = "";
AMDBDataSource mDataSource;
EditText updateApiaryNameInput;
EditText updateApiaryLocationInput;
EditText updateApiaryDescriptionInput;
EditText updateApiarySetupDateInput;
EditText updateApiaryNotesInput;
String updateApiaryidInput;
private TextView tvName, tvDescription, tvLocation, tvSetupDate, tvNotes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apiary_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDataSource = new AMDBDataSource(this);
mDataSource.open();
final AMClasses.Apiary updateApiary = new AMClasses.Apiary();
// String apiaryId = getIntent().getExtras().getString(ApiaryItemAdapter.APIARY_ID_KEY);
// AMClasses.Apiary item = SampleDataProvider.apiaryItemMap.get(apiaryId);
final AMClasses.Apiary item = getIntent().getExtras().getParcelable(ApiaryItemAdapter.APIARY_RECORD);
if (item == null) {
throw new AssertionError("No data record received!?!");
}
tvName = (TextView) findViewById(R.id.tvApiaryName);
tvLocation = (TextView) findViewById(R.id.tvApiaryLocation);
tvSetupDate = (TextView) findViewById(R.id.tvApiarySetupDate);
tvDescription = (TextView) findViewById(R.id.tvApiaryDescription);
tvNotes = (TextView) findViewById(R.id.tvApiaryNotes);
tvName.setText(item.getApiaryname());
tvLocation.setText(item.getApiarylocation());
tvSetupDate.setText(item.getApiarysetupdate());
tvDescription.setText(item.getApiarydescription());
tvNotes.setText(item.getApiarynotes());
apiaryIDDelete = item.getApiaryid();
Button updateApiaryButton = (Button) findViewById(R.id.btnUpdateApiary);
updateApiaryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
updateApiaryidInput = item.getApiaryid();
updateApiaryNameInput = (EditText) findViewById(R.id.tvApiaryName);
updateApiaryLocationInput = (EditText) findViewById(R.id.tvApiaryLocation);
updateApiaryDescriptionInput = (EditText) findViewById(R.id.tvApiaryDescription);
updateApiarySetupDateInput = (EditText) findViewById(R.id.tvApiarySetupDate);
updateApiaryNotesInput = (EditText) findViewById(R.id.tvApiaryNotes);
//updateApiary.setApiaryid(UUID.randomUUID().toString());
updateApiary.setApiaryid(updateApiaryidInput);
updateApiary.setApiaryname(updateApiaryNameInput.getText().toString());
updateApiary.setApiarylocation(updateApiaryLocationInput.getText().toString());
updateApiary.setApiarydescription(updateApiaryDescriptionInput.getText().toString());
updateApiary.setApiarysetupdate(updateApiarySetupDateInput.getText().toString());
updateApiary.setApiarynotes(updateApiaryNotesInput.getText().toString());
Log.i(LOGTAG, "Apiary ID to be updated: " + updateApiary.getApiaryid());
Log.i(LOGTAG, "Update Apiary Name: " + updateApiary.getApiaryname());
Log.i(LOGTAG, "Update Apiary Location: " + updateApiary.getApiarylocation());
Log.i(LOGTAG, "Update Apiary Description: " + updateApiary.getApiarydescription());
Log.i(LOGTAG, "Update Apiary Setup Date: " + updateApiary.getApiarysetupdate());
Log.i(LOGTAG, "Update Apiary Notes: " + updateApiary.getApiarynotes());
mDataSource.updateApiary2DB(updateApiary);
Snackbar.make(findViewById(android.R.id.content),
"Apiary updated", Snackbar.LENGTH_LONG)
.setAction("Action",null).show();
thread.start();
}
}
);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//ACTIVITY TIMER CLOSER
Thread thread = new Thread(){
@Override
public void run() {
try {
Thread.sleep(2500); // As I am using LENGTH_LONG in Toast
ApiaryDetails.this.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
//DELETE MENU
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_edit_apiary, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// SETTINGS MENU CASE STATEMENTS
int id = item.getItemId();
switch (id) {
case R.id.action_apiary_delete:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.app_name);
builder.setMessage("Please confirm you want to delete the apiary?");
builder.setIcon(R.drawable.ic_action_delete);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
mDataSource.deleteApiary(apiaryIDDelete); // Delete Apiary Row
Snackbar.make(findViewById(android.R.id.content),
"Apiary deleted", Snackbar.LENGTH_LONG)
.setAction("Action",null).show();
thread.start();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
break;
}
return true;
}
//DATE PICKER CODE
public void datePickerEditApiary(View view){
NewApiary.DatePickerFragment fragment = new NewApiary.DatePickerFragment();
fragment.show(getSupportFragmentManager(), "date");
}
public void setDate(final Calendar calendar){
final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK);
((TextView) findViewById(R.id.tvApiarySetupDate)).setText(dateFormat.format(calendar.getTime()));
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar cal = new GregorianCalendar(year, month, dayOfMonth);
setDate(cal);
}
public static class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),
(DatePickerDialog.OnDateSetListener) getActivity(),year, month, day);
}
}
}
清单:
<activity
android:name=".EditApiary"
android:label="@string/title_activity_edit__apiary"
android:parentActivityName=".ApiarySetup"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.icenibees.apiarymanager.ApiarySetup" />
</activity>
<activity
android:name=".ApiaryDetails"
android:label="@string/title_activity_apiary_details"
android:parentActivityName=".EditApiary"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.icenibees.apiarymanager.EditApiary" />
</activity>
<activity
android:name=".EditHive"
android:label="@string/title_activity_edit_hive"
android:parentActivityName=".ApiarySetup"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.icenibees.apiarymanager.ApiarySetup" />
</activity>
我看到
getsupportactionbar((
所以,您正在使用AppCompatactivity实例
可能,您应该覆盖Onoptionsitemsected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
finish();
return true;
//Or another code here
}
return false;
}
另外,您可以在AndroidManifest.xml中添加父活动: API 16
<activity
....
android:parentActivityName="activity_class_name" />