2013-11-02 71 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