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

时间:2016-05-11 01:18:53

标签: java android

我正在通过一个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工作室正在忙着说Intent说预期表达式,而非静态方法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);

}

}

2 个答案:

答案 0 :(得分:1)

尝试

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

此外,您的原始密钥字符串中没有MESSAGE。改为匹配。

答案 1 :(得分:1)

Method invocation 'activityButton.setOnClickListener(new View.OnClickListener() { public void onClick(View... may produce java.lang.NullPointerException

这意味着如果在这一行:

Button activityButton = (Button) findViewById(R.id.Button);

操作findViewById(R.id.Button)返回null,下一行将抛出NullPointerException,因为activityButton值为null。如果在id中找不到传递给findViewById的{​​{1}},则可能会发生这种情况。

您可以检查layout是否不是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);
            }
        });
    }

您需要命名一个变量,然后在下一行中使用它:

 Intent = getIntent();