2013-11-02 47 views
0

我收到此错误。它说“edit_message不能重新解析或不是字段”有人可以告诉我为什么我得到这个错误是我该如何解决它。edit_message无法解析或不是字段

布局主要活动组成: -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 
    <EditText android:id="@+id/edit_message" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" /> 
</LinearLayout> 

CODE主要活动的

package com.practice.firstapp1; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends Activity { 

    public final static String EXTRA_MESSAGE= "com.practice.fristapp1.MESSAGE"; 

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

    @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; 
    } 

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

} 

布局第二个活动组成: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".DisplayMessageActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

CODE第二个活动组成: -

package com.practice.firstapp1; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v4.app.NavUtils; 
import android.view.MenuItem; 

public class DisplayMessageActivity extends Activity { 

    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_message); 

     // Make sure we're running on Honeycomb or higher to use ActionBar APIs 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      // Show the Up button in the action bar. 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
     } 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

字符串资源: -

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">My First App</string> 
    <string name="edit_message">Enter a message</string> 
    <string name="button_send">Send</string> 
    <string name="action_settings">Settings</string> 
    <string name="title_activity_main">MainActivity</string> 
    <string name="title_activity_display_message">My Message</string> 
    <string name="hello_world">Hello world!</string> 

</resources> 

回答

1

它看起来像你的代码是正确的。检查以下内容:

1)将布局保存为xml,其中一个原因可能是其未保存。 2)检查你的xmls没有错误,项目正在构建。 3)如果您使用的是eclipse,请转至项目 - >清理所有项目,然后使用项目 - >自动构建。 4)打开你的活动并按“cmd + shift + o”ctrl + shift + o(在windows中)。它将导入R.java,在选项中选择com..R。

我希望它能解决错误。

+0

好的。谢谢。第3步工作,现在你能告诉我什么“清理所有项目”做了什么? – user2882662

+0

其次,你是否也可以通过检查我的项目是否构建来告诉我你的意思?建筑是否意味着编译? – user2882662

+0

是的,你可以认为它是编译。您可能想要选择项目 - >自动构建。只要你做了一些修改,它会自动在后台建立项目。 –

0

你错过了import com.practice.firstapp1.RMainActivity.java

有关更多信息,请参阅the docs on Accessing Resources

+0

我添加了导入语句,错误不会消失。 – user2882662

+0

谢谢。我之前没有见过以前的答案,并且添加了无助于错误的导入语句,但我确实学到了一些东西,所以非常感谢。 – user2882662