【问题标题】:How to send intent data from main project to library project in android?如何将意图数据从主项目发送到android中的库项目?
【发布时间】:2021-04-20 04:32:38
【问题描述】:

我一直在尝试将数据从我的主项目发送到我的库项目。 我的库项目是在主项目中定义的,所以添加 lib 项目的依赖项没有问题。我发现与我的问题类似的另一个线程,但到目前为止没有运气...thread

GetUserprofile 是主项目的活动,LockscreenActivity 是库项目的活动。

我在主项目端写过代码

Intent myIntent = null;
    try {
        myIntent = new Intent(GetUserprofile.this,LockscreenActivity.class);

       myIntent.putExtra("examiddetails",exam_id);

        startActivity(myIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }

现在我正在使用以下代码获取有关库项目的考试详细信息的数据

Intent intent = getIntent();    
    try{
        if(intent != null){
            String Examid = intent.getStringExtra("examiddetails");
        }

    }catch(Exception e){
        System.out.println("getdataFromService exception"+e.toString());
    }

我在库项目中得到了检查字符串的空值

【问题讨论】:

  • 请检查exam_id 的值。变量的签名是什么?
  • 它的字符串,所以这不是问题

标签: android nullpointerexception


【解决方案1】:

您的意思是向另一个活动发送数据吗? 我以前也做过。

MainProject 类

Intent intent= new intent(getApplication(), LockscreenActivity.class);
intent.putExtra("examiddetails",exam_id);
startActivity(intent);

LockscreenActivity 类

Bundle bundle = getIntent().getExtras();
String ExamIdDetails = bundle.getString("examiddetails");

【讨论】:

  • 我将数据发送到库项目而不是同一个项目...我想当您在主项目中有两个活动时这很好用...但是我有主项目和库项目,我必须通过意图或任何其他方式将数据从主项目发送到库项目就可以了
【解决方案2】:

从您的应用程序创建 Intent

Intent myIntent= new Intent(this,YourActivity.class)
myIntent.putExtra("data", "value");

从库中获取意图

string intentdata = ((Activity)getContext()).getIntent().getStringExtra("data");

【讨论】:

  • 没有运气,我仍然是空的
  • @Brat 你可以检查库项目中的活动是否正确。活动意图数据 = ((Activity)getContext());该活动应与您的传递活动相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 2022-10-25
  • 2013-12-17
相关资源
最近更新 更多