this.C()
如何工作?方法C是非静态的,所以它必须通过实例调用。这是指当前对象,但是它是什么对象以及它是如何创建的?我没有明确创建一个对象,因此不会编译暗中创建一个幕后?...调用方法时,“this”如何在Java中工作?
public class MainActivity extends Activity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.C();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if(id ==R.id.action_search){
this.openSearch();
}
else if (id == R.id.action_settings) {
// openSettings();
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message= editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void C(){
}
public void openSearch(){
getActionBar().hide();
}
}
编译!=执行代码。 –
如果您没有创建对象,则该方法未被调用。比喻:仅仅因为建筑师制定了蓝图并不意味着建造房屋。 –
你甚至打电话给B? –