R.id.无法解析符号'消息'

时间:2017-01-04 14:29:49

标签: java android

这是代码,这是错误无法解决行上的符号'message'

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

以前申请表现良好 这可能是一个愚蠢的错误

MainActivity.java

package com.example.antonio.newsbooklite2;

/**
 * Created by antonio on 30/12/16.
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
//import android.support.design.widget.*;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.*;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Called when the user clicks the Send button */
    }

    /** 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.message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);

        // Do something in response to button
    }
}

的strings.xml

<resources>
    <string name="app_name">newsbooklite2</string>

    <string name="message">Scrivi messaggio: </string>
    <string name="button_send">Send</string>
    enter code here
    <string name="nome_stringa">Testo_visualizzato</string>

</resources>

2 个答案:

答案 0 :(得分:0)

你可能会混合这些东西

findViewById(R.id.message);
如果你想要你可以使用的字符串,

从布局中返回视图:

getResources().getString(R.string.message);

答案 1 :(得分:0)

您正在使用string.xml文件中定义的消息ID。 EditText editText =(EditText)findViewById(R.id.message); 将消息更改为布局文件中定义的id。