Java 在Android studio中的活动之间传递字符串

Java 在Android studio中的活动之间传递字符串,java,android,Java,Android,我正在阅读Sam的自学android应用程序开发书,我有一个主要活动,它扩展了第二个活动,应该在活动之间传递字符串数据。我在主要活动中遇到了这个错误 Method invocation 'activityButton.setOnClickListener(new View.OnClickListener() { public void onClick(View... may produce java.lang.NullPointerException 我的主要活动代码如下: package

我正在阅读Sam的自学android应用程序开发书,我有一个主要活动,它扩展了第二个活动,应该在活动之间传递字符串数据。我在主要活动中遇到了这个错误

Method invocation 'activityButton.setOnClickListener(new View.OnClickListener() { public void onClick(View... may produce java.lang.NullPointerException 
我的主要活动代码如下:

package com.example.owner.hour2application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button activityButton = (Button) findViewById(R.id.Button);
    activityButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent startIntent = new Intent(MainActivity.this, SecondaryActivity.class);
            startIntent.putExtra("com.example.owner.Message","Hello SecondaryActivity");
            startActivity(startIntent);
        }
    });
}
}
这是我的第二个活动代码,android studio在其中大惊小怪地说,表达式是预期的,非静态方法getStringExtra(java.lang.string)不能从静态上下文引用

package com.example.owner.hour2application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu; 
import android.view.MenuItem;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import static android.content.Intent.*;


public class SecondaryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_secondary);
   Intent = getIntent();
    String message = Intent.getStringExtra("com.example.owner.MESSAGE");
    TextView messageTextView = (TextView) findViewById(R.id.message);
    messageTextView.setText(message);

}

}
试一试

另外,您的原始密钥字符串没有大写的消息。换成相配的

Method invocation 'activityButton.setOnClickListener(new View.OnClickListener() { public void onClick(View... may produce java.lang.NullPointerException
这意味着,如果在该行中:

Button activityButton = (Button) findViewById(R.id.Button);
 Intent = getIntent();
操作
findViewById(R.id.Button)
返回
null
,下一行将抛出
NullPointerException
,因为
activityButton
值将为
null
。如果在
布局中找不到传递给
findViewById
id
,则可能发生这种情况

您可以检查
activityButton
是否为
null
且消息不会显示:

    Button activityButton = (Button) findViewById(R.id.Button);
    if (activityButton != null){
        activityButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent startIntent = new Intent(MainActivity.this, SecondaryActivity.class);
                startIntent.putExtra("com.example.owner.Message","Hello SecondaryActivity");
                startActivity(startIntent);
            }
        });
    }
第二个问题:您没有在此行中定义变量名:

Button activityButton = (Button) findViewById(R.id.Button);
 Intent = getIntent();
您需要命名一个变量,然后在下一行中使用它:

 Intent intent= getIntent();
 String message = intent.getStringExtra("com.example.owner.MESSAGE");

这是一个警告,不是错误!!尝试
getIntent().getStringExtra(“com.example.owner.MESSAGE”)好的,谢谢!学习。感谢您的澄清!!!!我还有一个问题…方法调用“TextView messageTextView.setText(message)”可能会产生java.lang.NullPointerException这意味着什么???