getIntent 返回 null

getIntent returning null

在 android 中,当我尝试使用 getIntent(); android 中的方法,它 returns null,导致应用程序崩溃。这是我的代码:

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText textbox = (EditText) findViewById(R.id.edit_message);
        String message = textbox.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message.toString());
        startActivity(intent);
    }
}

第一个 activity 调用第二个:

public class DisplayMessageActivity extends AppCompatActivity {

    Intent intent = getIntent();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_mesage);

        String message = "";

        if (intent == null) {
            Log.d("Intent", "Intent is null :(");
        }

        try {
            message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        } catch (Exception e) {
            Log.d("Message", "Message is null");
        }


        TextView textView = new TextView(this);
        textView.setText(message);
        textView.setTextSize(40);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
        layout.addView(textView);
    }
}   

我不明白为什么它 returns 为 null,因为我自己称呼它的目的是……不为 null。

编辑:我还尝试使用除常量以外的其他键作为键,例如 "message",而不是 EXTRA_MESSAGE,它会导致相同的结果。

getIntent()方法必须在setContentView()方法

之后调用
public class DisplayMessageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_mesage);

    Intent intent = getIntent();
    String message = "";

    if (intent == null) {
        Log.d("Intent", "Intent is null :(");
    }

    try {
        message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    } catch (Exception e) {
        Log.d("Message", "Message is null");
    }


    TextView textView = new TextView(this);
    textView.setText(message);
    textView.setTextSize(40);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
    layout.addView(textView);
}

}

null 有两个原因。 1) 如果你没有在 andorid 的 manifest.xml 文件中声明 activity 。 2) 你必须在 oncreate() 方法中使用 getIntent() ,它的代码在这里 :-

public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_mesage);

        Intent intent = getIntent();


        String message = "";

        if (intent == null) {
            Log.d("Intent", "Intent is null :(");
        }

        try {
            message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        } catch (Exception e) {
            Log.d("Message", "Message is null");
        }


        TextView textView = new TextView(this);
        textView.setText(message);
        textView.setTextSize(40);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
        layout.addView(textView);
    }
}