Android first application String cannot be resolve

2020-03-27 18:02发布

问题:

I was going through this URL -http://developer.android.com/training/basics/firstapp/creating-project.html and getting this error EXTRA_MESSAGE cannot be resolved or is not a field:

Menifest

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

    }

}

DisplayMessageActivity

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

}

Please guide.

回答1:

Include this in your main activity class

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

This worked for me thanks to https://www.androidpit.com/forum/632071/error-cannot-find-symbol-variable-extra-message-in-android-studio



回答2:

The compiler is telling you that there no such variable as EXTRA_MESSAGE in your MainActivity. So you need to declare it as static and public in order to retrieve the way you writing your code.

Add this in your MainActivity:

public final static String EXTRA_MESSAGE = "someString";

and also you need to import MainActivity if these two activites are not in same package.



回答3:

in the MainActivity.java change

intent.putExtra(EXTRA_MESSAGE, message);

to

intent.putExtra("EXTRA_MESSAGE", message);

and in the DisplayMessageActivity.java replace

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

with

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

This is what ended up working for me so yeah...



回答4:

When ever a activity is called there will a calling intent we can access that intent inside activity now the point is we can put some key value pair with intent.

Now in your activity

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

you are accessing the intent here and accessing a string value from that intent.

From Mainnactivity you might be calling this activity

using code like this

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

you will receive this msg in DisplayActivity and problem will gone.

You must have a String declaration EXTRA_MESSAGE in MainActivity

Simply put

public Static String EXTRA_MESSAGE="your_message";


回答5:

just change the line in MainActivity

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

with

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

because it is

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

there will be no change in the DisplayMessageActivity

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


回答6:

just change the line in MainActivity:

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

with

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

because it is

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


回答7:

Include the following code in your main activity:

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


回答8:

Best one, use this

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

-->change the com.example.myfirstapp to the name of package you created
-->MainActivity is the class which you declared the message in change it accordingly