Eclipse中的Android开发不可重现的代码意图

har*_*kkk 0 java eclipse android android-intent

为了练习,我在https://developer.android.com/training/basics/firstapp/index.html上做了"构建你的第一个应用程序"的例子. 现在我对Intents有问题.Eclipse说"无法访问的代码"我已经导入了类Intent(import android.content.Intent;)

Intent intent = getIntent();
Run Code Online (Sandbox Code Playgroud)

这是代码:

package com.example.test;

import android.os.Bundle;
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;
import android.content.Intent;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_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;
    // 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);
}

@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);
}
Run Code Online (Sandbox Code Playgroud)

}

zap*_*apl 5

因为你return之前在线,所以无法达到.

return true;
// Get the message from the intent
**Intent intent = getIntent();**
Run Code Online (Sandbox Code Playgroud)

删除return语句(使它成为你在该方法中做的最后一件事)或使其成为条件.