onCreateOptionsMenu
覆盖正在给我一个错误。该代码应该以整数读取,并根据按下的按钮将其立方体或方形。在我输入逻辑之前,我没有收到错误。此外onOptionsItemSelected
给出了一个错误,指出它无法解析方法。在此先感谢您的帮助!如果您在逻辑ID中也注意到错误提示,那么。
package com.firstapp.conner.squareandcube;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static int squared, cubed;
Button square, cube;
TextView tv, inputNum;
int num1;
String numString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
square = (Button) findViewById(R.id.square_button);
cube = (Button) findViewById(R.id.cube_button);
inputNum = (TextView) findViewById(R.id.editText);
inputNum.setText(inputNum.getText());
num1 = Integer.parseInt(inputNum.toString());
tv = (TextView) findViewById(R.id.textView);
Button square = (Button)findViewById(R.id.square_button);
square.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
squared = num1 * num1;
numString = String.valueOf(squared);
tv.setText(numString);
cube.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cubed = num1 * num1 * num1;
numString = String.valueOf(cubed);
tv.setText(numString);
}
});
@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_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);
}
});}}
答案 0 :(得分:0)
只需复制并粘贴此代码即可。有大括号错误,所以我为你解决了。
package com.firstapp.conner.squareandcube;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static int squared, cubed;
Button square, cube;
TextView tv, inputNum;
int num1;
String numString;
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
square = (Button) findViewById(R.id.square_button);
cube = (Button) findViewById(R.id.cube_button);
inputNum = (TextView) findViewById(R.id.editText);
inputNum.setText(inputNum.getText());
num1 = Integer.parseInt(inputNum.toString());
tv = (TextView) findViewById(R.id.textView);
Button square = (Button)findViewById(R.id.square_button);
square.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
squared = num1 * num1;
numString = String.valueOf(squared);
tv.setText(numString);
}
});
cube.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cubed = num1 * num1 * num1;
numString = String.valueOf(cubed);
tv.setText(numString);
}
});
}
@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_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);
}
}