如何将带有额外内容的意图发送到图书馆项目活动

问题描述:

我创建了一个包含一项活动的库项目.我已经将这个库项目引用到我的主项目中.我试图通过意图调用带有一些额外内容的图书馆项目活动.但是在库项目中检索 getIntent.getExtras 时出现空指针异常.知道如何做到这一点吗?

I have created a library project with one activity. I have given reference of this library project to my Main Project. I am trying to call Activity of Library Project with some extras through intent. But I am getting null pointer exception while retrieving getIntent.getExtras in Library Project. Any idea how to do this?

我正在开始图书馆项目的活动,如下所示:

I am starting activity of Library Project like below:

    Intent intent = new Intent(activity,com.***.***.LibActivity.class);
    intent.putExtra("key", "abcds");
    activity.startActivity(intent);

我正在尝试检索 LibActivity 中的密钥(在库项目中),如下所示:

And I am trying to retrieve the key in LibActivity (Which is in library project) like below:

getIntent().getExtras().getString("key") -->在这一行抛出空指针异常

getIntent().getExtras().getString("key") -->Throwing null pointer exception at this line

我已将此活动添加到我的主要项目清单中.

I have added this activity to my Main Projects manifest.

你必须在你的主项目中传递这个

You have to pass this on your main project

Intent myIntent= new Intent(this,YourActivity.class);
myIntent.putExtra("shopId", shopId);
this.startActivity(myIntent);

在你的图书馆活动中你必须通过这个

And in your Library activity You have to pass just this

String intentdata = this.getIntent().getStringExtra("shopId");

然后你的工作就完成了!

Then boom your work is done!