作为练习,我在https://developer.android.com/training/basics/firstapp/index.html上创建示例“构建您的第一个应用程序”
现在我有意图的问题。 Eclipse说“ Unreachable Code”,我已经导入了Intent类(import android.content.Intent;)

Intent intent = getIntent();


这是代码:

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


}

最佳答案

无法达到,因为您在前面的行中return

return true;
// Get the message from the intent
**Intent intent = getIntent();**


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

关于java - Eclipse中的Android开发无法达到代码 Intent ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21254250/

10-12 00:29
查看更多