将值传递给另一个应用程序时,Android空指针异常

时间:2017-06-01 03:46:51

标签: android

public String name, email, phone;
NotificationCompat.Builder notification1;

Button aprv;

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

    aprv=(Button)findViewById(R.id.btnapp);
    Bundle bundle=getIntent().getExtras();

    name=bundle.getString("Name");.......................##
    email=bundle.getString("Email");
    phone=bundle.getString("Phone");

    TextView textView1=(TextView)findViewById(R.id.tv1);
    TextView textView2=(TextView)findViewById(R.id.tv2);
    TextView textView3=(TextView)findViewById(R.id.tv3);
    textView1.setText(name);
    textView2.setText(email);
    textView3.setText(phone);

带有主题标签的行中显示的空指针异常。

这是另一个活动中的意图代码。我正确设置了清单。

Intent intent = new Intent("com.example.user.adminapp.Main2Activity");
intent.putExtra("Name", name);
intent.putExtra("Email", email);
intent.putExtra("Phone", phone);
startActivity(intent);

2 个答案:

答案 0 :(得分:1)

试试这个,它可能适合你,

Intent intent = new Intent(YOUR_ACTIVITY.this,Main2Activity.class);

答案 1 :(得分:0)

您通过其他应用程序打开的活动假设其名称为Main2Activity.java

第1步:

的AndroidManifest.xml

 <activity android:name="com.example.user.adminapp.Main2Activity">

            <intent-filter>
                <action android:name="com.example.user.SOMETHING_USEFUL"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

你的意图

 Intent intent = new Intent("com.example.user.SOMETHING_USEFUL");
        intent.putExtra("Name", name);
        intent.putExtra("Email", email);
        intent.putExtra("Phone", phone);
        startActivity(intent);

您的Main2Activity.java

Button aprv;
String name="";
String email="";
String phone="";

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

    aprv=(Button)findViewById(R.id.btnapp);
    if(getIntent().getStringExtra("Name")!=null)
{
   name=getIntent().getStringExtra("Name");
email=getIntent().getStringExtra("Email");
phone=getIntent().getStringExtra("Phone");
}

    TextView textView1=(TextView)findViewById(R.id.tv1);
    TextView textView2=(TextView)findViewById(R.id.tv2);
    TextView textView3=(TextView)findViewById(R.id.tv3);
    textView1.setText(name);
    textView2.setText(email);
    textView3.setText(phone);