我正在編寫此代碼並顯示錯誤非靜態方法getID()
無法從靜態上下文中引用。 該錯誤是在開關(View.getID())
錯誤:(56,21)錯誤:非靜態方法getId()不能從靜態上下文中引用
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button greetbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
greetbutton = (Button) findViewById(R.id.greetbutton);
greetbutton.setOnClickListener(this);
}
@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);
}
@Override
public void onClick(View v) {
TextView textView = (TextView) findViewById(R.id.textView);
EditText editFreindname = (EditText) findViewById(R.id.editFriendname);
String freindname = editFreindname.getText().toString();
switch (View.getId()) {
case R.id.greetbutton:
textView.setText(getString(R.string.greetstring) + freindname + "!");
break;
default:
break;
}
}
}