添加活动:无法解析方法'findViewByID(int)'



我正在按照 http://developer.android.com/training/basics/firstapp/starting-activity.html 的教程进行操作,并且完全按照Android Studio v.1.1.0中"构建您的第一个应用程序"下的本教程和所有先前教程中的说明完成了所有操作。

我在 MyActivity 中的代码.java:

package com.embeddedspring2015.jstrickling.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class MyActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.embeddedspring2015.jstrickling.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }

    @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_my, 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
    // Called when the user clicks the Send button
    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);
    }
}

当我尝试运行该应用程序时,出现错误"找不到符号方法查找视图ByID(int)"

所有其他文件在由Android Studio创建或按照教程的指示进行更改后保持不变。我已经搜索了遇到此问题的其他人,但似乎没有人询问过这个确切的问题。

在编写代码时,要意识到它是区分大小写的。 因此,当您编写findViewByID时,它与findViewById不同。

正确的用法是

EditText editText = (EditText) findViewById(R.id.edit_message);

相关内容

最新更新