我正在尝试构建一个按照floatActionButton点击动态生成按钮的应用。这方面工作正常。但是如果没有视图,则操作栏上不应该有删除菜单项。在应用程序的开头,由于没有项目,没有菜单项,但是一旦我开始添加视图,我需要显示菜单项。我尝试用if语句来处理它,但它仍然没有用。
这是我的代码:
public class MainActivity extends AppCompatActivity {
int counter = 0;
FloatingActionButton addingSemester;
Button semesterButton;
LinearLayout semesterLayout;
GridLayout semesterGridLayout;
RelativeLayout relativeLayout;
LinearLayout.LayoutParams portraitLayoutParams = new LinearLayout.LayoutParams(
AppBarLayout.LayoutParams.MATCH_PARENT,
AppBarLayout.LayoutParams.WRAP_CONTENT);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addingSemester = (FloatingActionButton) findViewById(R.id.addActionButton);
semesterLayout = (LinearLayout) findViewById(R.id.main_layout);
semesterGridLayout = (GridLayout)findViewById(R.id.semester_grid_layout);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);
semesterButton = new Button(MainActivity.this);
if (savedInstanceState != null) {
counter = savedInstanceState.getInt("counter");
for(int i = 0; i < counter; i++) {
addSemesterButton(i);
}
}
}
public void addSemesterButton(int id) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
double width = (size.x)/3;
semesterButton = new Button(MainActivity.this);
semesterButton.setId(id + 1);
semesterButton.setText("Semester " + (id + 1));
semesterButton.setBackgroundColor(getColor(R.color.colorPrimary));
semesterButton.setTextColor(Color.WHITE);
portraitLayoutParams.setMargins(24, 24, 24, 24);
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.setMargins(24, 24, 24, 24);
params.width = (int) width;
params.height = GridLayout.LayoutParams.WRAP_CONTENT;
semesterButton.setLayoutParams(params);
semesterGridLayout.addView(semesterButton);
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.addView(semesterButton);
semesterButton.setLayoutParams(portraitLayoutParams);
}
setOnLongClickListenerForSemesterButton();
}
public void addTextView(int id){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
double width = (size.x);
TextView instructionsPromptTextView = new TextView(MainActivity.this);
instructionsPromptTextView.setId(id);
instructionsPromptTextView.setText(R.string.click_the_button_to_add_semesters);
instructionsPromptTextView.setTextSize((float)width);
instructionsPromptTextView.setVisibility(View.VISIBLE);
instructionsPromptTextView.setBackgroundColor(Color.BLACK);
relativeLayout.addView(instructionsPromptTextView);
if(id == 0){
instructionsPromptTextView.setVisibility(View.INVISIBLE);
}
}
public void onFloatActionButtonClick(View view) {
if (counter < 8) {
addSemesterButton(counter);
counter++;
setOnLongClickListenerForSemesterButton();
} else if (counter == 8) {
Toast.makeText(MainActivity.this, "You cannot add more than 8 semesters", Toast.LENGTH_SHORT).show();
} else if (counter == 0){
addTextView(counter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.delete);
if(counter == 0){
item.setVisible(false);
}
if(counter > 0){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
item.setVisible(true);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.delete) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete everything?")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterGridLayout.removeAllViews();
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.removeAllViews();
}
counter = 0;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("counter", counter);
super.onSaveInstanceState(savedInstanceState);
}
private void setOnLongClickListenerForSemesterButton() {
semesterButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final Button b = (Button) v;
b.setTag(b.getText().toString());
b.setBackgroundColor(Color.RED);
b.setText("Delete");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Delete entry");
builder.setMessage("Are you sure you want to delete this entry?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterGridLayout.removeView(b);
for (int i = 0; i < semesterGridLayout.getChildCount(); i++) {
((Button) semesterGridLayout.getChildAt(i)).setText("Semester " + (i + 1));
}
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.removeView(b);
for (int i = 0; i < semesterLayout.getChildCount(); i++) {
((Button) semesterLayout.getChildAt(i)).setText("Semester " + (i + 1));
}
}
counter--;
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
b.cancelLongPress();
b.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
b.setText(b.getTag().toString());
dialog.cancel();
}
});
builder.show();
return true;
}
});
}
}
修改
这是堆栈跟踪
FATAL EXCEPTION: main
Process: myapp.onur.journeygpacalculator, PID: 22605
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4711)
at android.view.View.performClick(View.java:5623)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4706)
at android.view.View.performClick(View.java:5623)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.add(int, int, int, java.lang.CharSequence)' on a null object reference
at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:493)
at android.view.MenuInflater.parseMenu(MenuInflater.java:190)
at android.view.MenuInflater.inflate(MenuInflater.java:111)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:113)
at myapp.onur.journeygpacalculator.MainActivity.inflateMenu(MainActivity.java:182)
at myapp.onur.journeygpacalculator.MainActivity.onFloatActionButtonClick(MainActivity.java:168)
答案 0 :(得分:0)
而不是仅在onCreateOptionsMenu
中将其设置为隐藏,而不是在操作按钮中将其设置为单击,如下所示
MenuItem item;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
item = menu.findItem(R.id.delete);
item.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
public void onFloatActionButtonClick(View view) {
if (counter < 8) {
addSemesterButton(counter);
counter++;
setOnLongClickListenerForSemesterButton();
item.setVisible(true);
} else if (counter == 8) {
Toast.makeText(MainActivity.this, "You cannot add more than 8 semesters", Toast.LENGTH_SHORT).show();
item.setVisible(true);
} else if (counter == 0){
addTextView(counter);
item.setVisible(false);
}
}
答案 1 :(得分:0)
1。如下所示更新onCreateOptionsMenu()
,以便在app首次启动时隐藏delete
选项:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
itemDelete = menu.findItem(R.id.delete);
// Required to hide delete option when app starts first
itemDelete.setVisible(false);
return true;
}
2。将以下方法添加到update
delete
选项状态。
public void updateOptionMenu() {
if (counter > 0)
item.setVisible(true);
else
item.setVisible(false);
}
3. 从counter
值更改的所有地方调用updateOptionMenu()方法:onFloatActionButtonClick()
和AlertDialog's onClick()
方法。
以下是完整代码:
public class MainActivity extends AppCompatActivity {
MenuItem itemDelete;
int counter = 0;
FloatingActionButton addingSemester;
Button semesterButton;
LinearLayout semesterLayout;
GridLayout semesterGridLayout;
RelativeLayout relativeLayout;
LinearLayout.LayoutParams portraitLayoutParams = new LinearLayout.LayoutParams(
AppBarLayout.LayoutParams.MATCH_PARENT,
AppBarLayout.LayoutParams.WRAP_CONTENT);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addingSemester = (FloatingActionButton) findViewById(R.id.addActionButton);
semesterLayout = (LinearLayout) findViewById(R.id.main_layout);
semesterGridLayout = (GridLayout)findViewById(R.id.semester_grid_layout);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);
semesterButton = new Button(MainActivity.this);
if (savedInstanceState != null) {
counter = savedInstanceState.getInt("counter");
for(int i = 0; i < counter; i++) {
addSemesterButton(i);
}
}
addTextView(counter);
}
public void addSemesterButton(int id) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
double width = (size.x)/3;
semesterButton = new Button(MainActivity.this);
semesterButton.setId(id + 1);
semesterButton.setText("Semester " + (id + 1));
semesterButton.setBackgroundColor(getColor(R.color.colorPrimary));
semesterButton.setTextColor(Color.WHITE);
portraitLayoutParams.setMargins(24, 24, 24, 24);
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.setMargins(24, 24, 24, 24);
params.width = (int) width;
params.height = GridLayout.LayoutParams.WRAP_CONTENT;
semesterButton.setLayoutParams(params);
semesterGridLayout.addView(semesterButton);
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.addView(semesterButton);
semesterButton.setLayoutParams(portraitLayoutParams);
}
setOnLongClickListenerForSemesterButton();
}
public void addTextView(int id){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
double width = (size.x);
TextView instructionsPromptTextView = new TextView(MainActivity.this);
instructionsPromptTextView.setId(id);
instructionsPromptTextView.setText(R.string.click_the_button_to_add_semesters);
instructionsPromptTextView.setTextSize((float)width);
instructionsPromptTextView.setVisibility(View.VISIBLE);
instructionsPromptTextView.setBackgroundColor(Color.BLACK);
relativeLayout.addView(instructionsPromptTextView);
/*
if(id == 0){
instructionsPromptTextView.setVisibility(View.INVISIBLE);
}*/
}
public void onFloatActionButtonClick(View view) {
if (counter < 8) {
addSemesterButton(counter);
counter++;
setOnLongClickListenerForSemesterButton();
// Required to show delete option
updateOptionMenu();
} else if (counter == 8) {
Toast.makeText(MainActivity.this, "You cannot add more than 8 semesters", Toast.LENGTH_SHORT).show();
}
// Will never be called because counter == 0 will be consumed by first if condition
/*
else if (counter == 0){
addTextView(counter); // Move it to onCreate()
}*/
}
public void updateOptionMenu() {
if (counter > 0)
item.setVisible(true);
else
item.setVisible(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
itemDelete = menu.findItem(R.id.delete);
// Required to hide delete option when app starts first
itemDelete.setVisible(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.delete) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete everything?")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterGridLayout.removeAllViews();
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.removeAllViews();
}
counter = 0;
updateOptionMenu(); // Required to hide option item when all views are removed
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("counter", counter);
super.onSaveInstanceState(savedInstanceState);
}
private void setOnLongClickListenerForSemesterButton() {
semesterButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final Button b = (Button) v;
b.setTag(b.getText().toString());
b.setBackgroundColor(Color.RED);
b.setText("Delete");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Delete entry");
builder.setMessage("Are you sure you want to delete this entry?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterGridLayout.removeView(b);
for (int i = 0; i < semesterGridLayout.getChildCount(); i++) {
((Button) semesterGridLayout.getChildAt(i)).setText("Semester " + (i + 1));
}
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.removeView(b);
for (int i = 0; i < semesterLayout.getChildCount(); i++) {
((Button) semesterLayout.getChildAt(i)).setText("Semester " + (i + 1));
}
}
counter--;
updateOptionMenu(); // Required to hide delete item if this button is last button to be removed
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
b.cancelLongPress();
b.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
b.setText(b.getTag().toString());
dialog.cancel();
}
});
builder.show();
return true;
}
});
}
}
希望这会有所帮助〜