3

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

清单

<?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>

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

    }

}

显示消息活动

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

}

请指导。

4

8 回答 8

12

将此包含在您的主要活动课程中

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这对我有用

于 2016-12-01T20:48:08.127 回答
4

编译器告诉你没有像EXTRA_MESSAGE你的MainActivity. 因此,您需要将其声明为static并且public为了检索您编写代码的方式。

将此添加到您的MainActivity

public final static String EXTRA_MESSAGE = "someString";

MainActivity如果这两个活动不在同一个包中,您还需要导入。

于 2013-10-20T19:01:34.520 回答
4

MainActivity.java更改

intent.putExtra(EXTRA_MESSAGE, message);

intent.putExtra("EXTRA_MESSAGE", message);

并在DisplayMessageActivity.java中替换

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

String message = intent.getStringExtra("EXTRA_MESSAGE");

这就是最终为我工作的东西,所以是的......

于 2017-02-18T20:02:38.473 回答
1

每当调用一个活动时,都会有一个调用意图,我们可以在活动内部访问该意图,现在关键是我们可以将一些键值对与意图放在一起。

现在在你的活动中

 Intent intent = getIntent();
 String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
 setContentView(R.layout.activity_display_message);

您正在此处访问意图并从该意图访问字符串值。

从 Mainnactivity 你可能会调用这个活动

使用这样的代码

Intent intent=new Intent(this,DisplayActivity.Class);
// put some msg in your intent like this
intent.putExtra(MainActivity.EXTRA_MESSAGE,"your_msg");
startActivity(intent);

您将在 DisplayActivity 中收到此消息,问题就会消失。

您必须在 MainActivity 中有一个字符串声明 EXTRA_MESSAGE

简单的说

public Static String EXTRA_MESSAGE="your_message";
于 2013-10-20T18:59:56.603 回答
1

只需更改MainActivity中的行

intent.putExtra(MainActivity.EXTRA_MESSAGE,"your_msg");

intent.putExtra("EXTRA_MESSAGE","your_msg");

因为它是

 intent.putExtra("KEY","VALUE");

DisplayMessageActivity不会有任何变化

String message = intent.getStringExtra("EXTRA_MESSAGE");
于 2016-10-19T17:52:17.260 回答
0

在您的主要活动中包含以下代码:

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
于 2017-09-06T12:06:37.077 回答
0

最好的,用这个

import static com.example.myfirstapp.MainActivity.EXTRA_MESSAGE;

--> 将 com.example.myfirstapp 更改为您创建的包的名称
--> MainActivity 是您声明消息的类,相应地更改它

于 2018-02-08T16:09:07.403 回答
0

只需更改以下行MainActivity

intent.putExtra(MainActivity.EXTRA_MESSAGE,"your_msg");

intent.putExtra("EXTRA_MESSAGE","your_msg");

因为它是

 intent.putExtra("KEY","VALUE");
于 2016-10-19T18:29:12.933 回答