0

I tried to pass ArrayList of values between two Activities but I am getting error as below.Thanks in Advance

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference at com.example.android.myapplication.DetailActivity.getextras1(DetailActivity.java:35) at com.example.android.myapplication.MyService.onStartCommand(MyService.java:38) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3180) at android.app.ActivityThread.access$2200(ActivityThread.java:182) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5763) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

code which is used to Call Other activity

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id){

  String[] appdescription =  getResources().getStringArray(R.array.appdescriptionname);
  final String appdescipl = appdescription[position];
  final int imagesList =  noImagesList[position];
  final int imagesList2 = noImagesList2[position];

  Intent intent1 = new Intent(getApplicationContext(), DetailActivity.class);
  intent1.putExtra("des","mydes");
  intent1.putExtra("appdescipl" , appdescipl);
  intent1.putExtra("imagesList",imagesList);
  intent1.putExtra("imagesList2",imagesList2);
  startActivity(intent1);
     }
});

DetailActivity

public class DetailActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_activity);
    this.getextras1();
    this.getdescriptionofapp1();
    this.getImage1ofapp1();
    this.getImage2ofapp1();
    startService(new Intent(this, MyService.class));


}


public Bundle getextras1() {
    Bundle extras1 = getIntent().getExtras();
    return extras1;
}

public TextView getdescriptionofapp1() {
    TextView descriptionofapp1 = (TextView) findViewById(R.id.descriptionofapp);
    return descriptionofapp1;
}

public ImageView getImage1ofapp1() {
    ImageView image1ofapp1 = (ImageView) findViewById(R.id.image1);
    return image1ofapp1;
}

public ImageView getImage2ofapp1() {
    ImageView image2ofapp1 = (ImageView) findViewById(R.id.image2);
    return image2ofapp1;
}




}

MyService

public class MyService extends Service {


public String description;
public int image1;
public int image2;


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void onCreate() {
    super.onCreate();

}

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

    DetailActivity i ;
    i = new DetailActivity();
    Bundle extras = i.getextras1();
    TextView descriptionofapp = i.getdescriptionofapp1();
    ImageView image1ofapp  = i.getImage1ofapp1();
    ImageView image2ofapp = i.getImage2ofapp1();



    if ( extras.containsKey("des")){

        description = extras.getString("appdescipl");
        descriptionofapp.setText(description);
        image1 = extras.getInt("imagesList");
        image1ofapp.setImageResource(image1);
        image2 = extras.getInt("imagesList2");
        image2ofapp.setImageResource(image2);
    }

    else if (extras.containsKey("data")){
        description = extras.getString("appdatadescipl");
        descriptionofapp.setText(description);
        image1 = extras.getInt("imagesOfLists");
        image1ofapp.setImageResource(image1);
        image2 = extras.getInt("imagesOfLists2");
        image2ofapp.setImageResource(image2);
    }

    this.onDestroy();
    return START_STICKY;
}


@Override
public void onDestroy() {

    this.stopSelf();
}
}
Omar Dhanish
  • 885
  • 7
  • 18

3 Answers3

1

Proble is here

DetailActivity i ;
i = new DetailActivity();
Bundle extras = i.getextras1();

You are creating new instance of Activity class but you assinged your bundle in another instance of the Activity.

You can pass bundle to your service with putExtra method of intent and getExtra in the service to extract it.

use receiver to update your view from service. Your approch is wrong.

Have a look at there Android update activity UI from service

Community
  • 1
  • 1
Sadiq Md Asif
  • 882
  • 6
  • 18
0

use specific putextra for each object :

intent1.putStringExtra("des","mydes");
intent1.putStringExtra("appdescipl" , appdescipl);
intent1.putIntExtra("imagesList",imagesList);
intent1.putIntExtra("imagesList2",imagesList2);

and then use equal getExtra in DetailActivity

getIntent().getStringExtra("des");
getIntent().getStringExtra("appdescipl");
getIntent().getIntExtra("imagesList");
getIntent().getIntExtra("imagesList2");

i hope it would be helpful.

Mehdi Golzadeh
  • 2,594
  • 1
  • 16
  • 28
0

Better to use Parcelable Object for multiple parameters. Suppose you have a class Foo implements Parcelable properly , To put it into Intent in an Activity:

Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
Foo foo = new Foo();
intent.putExtra("foo ", foo);
startActivity(intent);

To get it from intent in DetailActivity:-

Foo foo = getIntent().getExtras().getParcelable("foo");
String appDisc = foo.getDisc();
int appImage1 = foo.getImage1();
int appImage2 = foo.getImage2();
....

I hope it would be helpful.

Hisham
  • 11
  • 2