简体   繁体   中英

How to send data via intent from an Activity to Service - Null Pointer Issue

I'm trying to pass info from an activity to a service.

i'm getting a null pointer exception in the logcat. I'm new to this but i remember reading on SO that it's because intent.getStringExtra is null in the service. I'm not sure that's the case. Can someone please tell me what the actual problem is because i've tried everything. we can't use Bundle extras = this.getIntent().getExtras(); so how else can we solve this problem .

below is what i have in the activity

         flashButton1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            strobeDIV++;

            try {

                Intent i1 = new Intent(LeLauncher.this, FlashServ.class);
                Bundle b = new Bundle();

                totalMS = String.valueOf(totalMilli);
                cdMS = String.valueOf(totalMilli);
                strobeDIVString =String.valueOf(strobeDIV);

                b.putString("totalTimeMilli", totalMS);
                b.putString("cdTimeMilli", cdMS);
                b.putString("strobeDIV", strobeDIVString);

                i1.putExtra("totalTimeMilli", totalMilli);
                i1.putExtra("cdTimeMilli", cdMilli);
                i1.putExtra("strobeDIV", strobeDIV);


                i1.putExtras(b);
                LeLauncher.this.startService(i1);
                flashText1.setText("Seconds Left: 1");

            } catch (Exception e1) {

                e1.printStackTrace();

            }

        }
    });

below is what i have in the service.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    //String totalTimeMilli182 = intent.getStringExtra("totalTimeMilli");
    //String cdTimeMilli182 = intent.getStringExtra("cdlTimeMilli");
    //String strobeDIVstring182 = intent.getStringExtra("strobeDIV");

     Bundle b=intent.getExtras();

     String totalTimeMilli182 =  b.getString("totalTimeMilli");
     String cdTimeMilli182 =  b.getString("cdlTimeMilli");
     String strobeDIVstring182 =  b.getString("strobeDIV");


    fooTimeTotal = Integer.parseInt(totalTimeMilli182);
    fooCDTotal = Integer.parseInt(cdTimeMilli182);
    strobeDIV = Integer.parseInt(strobeDIVstring182);

    strobeTimer182();

    return START_STICKY;

}

activity.java

Intent intent=new Intent(ServicesActivity.this,FileManagerRequest.class);         
Bundle b=new Bundle()
b.putStringArray("Array", your_array)
intent.putExtras(b);
startService(intent);
in you service

service.java

    public void onStart(Intent intent, int startid){
    super.onStart(intent, startid);
    Bundle b=intent.getExtras();
    String[] Array = b.getStringArray("Array");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM