edit_text无法解析或不是字段

时间:2014-03-02 19:36:54

标签: android eclipse

我在Eclipse上的Android.com上执行教程时点击“运行”,我得到“edit_text无法解析或不是字段”。这是我的MainActivity'

代码
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.R;

public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.Tutorial.MESSAGE";

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


/** Called when the user clicks the Send button */
public void sendMessage (View view){
    // Do something in response to button
    Intent intent=new Intent (this, DisplayMessageActivityNewNew.class);
    EditText editText = (EditText) findViewById(R.id.edit_text);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}}

我的第二项活动:

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivityNewNew extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);

}

Main_Activity.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context="MainActivity" >
<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message"/>
<Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" 
        android:onClick="sendMessage"/>
</LinearLayout>

不知道问题是什么,但如果您有任何帮助,我们将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:1)

删除以下导入

import android.R;

答案 1 :(得分:0)

删除@Raghunandan提到的导入,清理项目。检查ui xmls,在创建项目的Resource文件时,某些组件可能会干扰ide。 编辑:我看到你使用了android.R.id.home,它强制你导入android.R,它只是对R引用任何引用作为对android.R的引用,无论你在哪里使用R只需使用YOURPACKAGENAME.R.Resourcename