Android第一个应用String无法解析或不是字段错误

标签 android android-intent

我正在浏览这个 URL - http://developer.android.com/training/basics/firstapp/creating-project.html 并收到此错误 EXTRA_MESSAGE 无法解析或不是字段:

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>

</manifest>

主 Activity .java

package com.example.myfirstapp;

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

public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.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;
}


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);

    }

显示消息 Activity

package com.example.myfirstapp;

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

import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;

public class DisplayMessageActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    setContentView(R.layout.activity_display_message);
    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Show the Up button in the action bar.
    setupActionBar();

}

/**
 * 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 onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_message, menu);
return 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:
        //

        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

请指导。

最佳答案

将其包含在您的主要 Activity 类(class)中

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

感谢 https://www.androidpit.com/forum/632071/error-cannot-find-symbol-variable-extra-message-in-android-studio 这对我有用

关于Android第一个应用String无法解析或不是字段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19481408/

相关文章:

android - 使用 AngularJS 从服务器重定向时 Android 上的无限重定向循环

android - 如何使通知 Intent 恢复而不是新的 Intent ?

android - 如何使用 ACTION_SEND 分隔电子邮件中不同行的文本?

java - 没有在路径 : DexPathList. 上找到 .MyApp 类 仅当直接运行调试 apk 时

android - fragment 管理?

android - 在我的视频播放时添加 "ok glass",

java - 有意传递文件,我该如何检索它

android - 使用 FileProvider uri 打开 PDF 文件会打开一个空白屏幕

android - actionBarSherlock 字幕 textSize

android - com.google.android.maps