0

I have a horizontal adapter which has Edit option. On selecting edit it opens up new activity. After editing it comes back to horizontal adapter. I am trying to update the model class in the adapter using onActivityResult. but trace is saying

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=78, result=-1, data=Intent { (has extras) }} to activity {com.codon.masterpiece/com.codon.masterpiece.ui.home.FullScreenSelf}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4335)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4379)
        at android.app.ActivityThread.-wrap19(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1673)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)


Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
        at com.codon.masterpiece.ui.home.SelfHorizontalScroll.instantiateItem(SelfHorizontalScroll.java:109)
        at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1158)
        at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:669)
        at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:631)
        at androidx.viewpager.widget.ViewPager.dataSetChanged(ViewPager.java:1086)
        at androidx.viewpager.widget.ViewPager$PagerObserver.onChanged(ViewPager.java:3097)
        at androidx.viewpager.widget.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:291)
        at com.codon.masterpiece.ui.home.FullScreenSelf.onActivityResult(FullScreenSelf.java:237)
        at android.app.Activity.dispatchActivityResult(Activity.java:7355)

Here is my onActivityResult

 if (requestCode == 78 && data != null) {
            if (resultCode == RESULT_OK) {
                int position = data.getIntExtra("position", 0);
                Gson gson = new Gson();
                String artworkData = data.getStringExtra("ArtworkData");
                TypeToken<ArtworksData> token = new TypeToken<ArtworksData>() {
                };
                ArtworksData artworksData = gson.fromJson(artworkData, token.getType());

                arrayList.set(position, artworksData);

                selfHorizontalScroll.notifyDataSetChanged();
                //Toast.makeText(this, "Got it", Toast.LENGTH_SHORT).show();
            }
        }

Redirecting to edit activity

if (item.getItemId() == R.id.edit_menu) {
                    //call edit art activity and pass task id

                    Intent intent = new Intent(ctx, EditArtwork.class);
                    //Intent newintent = ctx.getIntent();
                    // Bundle bundle = newintent.getExtras();
                    String taskID = feed.getId() + "", imageUrl = "";

                    imageUrl = feed.getImg1() + "";
                    intent.putExtra("artwork_id", taskID);
                    intent.putExtra("image_url", imageUrl);
                    intent.putExtra("position", ((FullScreenSelf) ctx).viewPager.getCurrentItem());
                    //intent.putExtra("wishtosell", feed.getWishtosell());
                    Activity origin_new = (Activity) ctx;
                    origin_new.startActivityForResult(intent, 78);

                }

Method in edit activity

if ("False".equals(error)){
                        kProgressHUD.dismiss();
                        Toast.makeText(EditArtwork.this, "Artwork updated successfully", Toast.LENGTH_SHORT).show();
                        Log.d("Upload", jsonObject.toString());
                        Gson gson = new Gson();
                        JsonObject data = jsonObject.getAsJsonObject("data");
                        TypeToken<ArtworksData> token = new TypeToken<ArtworksData>() {
                        };
                        ArtworksData artworksData = gson.fromJson(data.toString(), token.getType());

                        //EditArtwork.this.onBackPressed();
                        Intent intent = new Intent();
                        intent.putExtra("ArtworkData", artworksData.toString());
                        setResult(RESULT_OK, intent);
                        finish();
                    }

its crashing here. at the instantiateItem method in the adapter.

 final boolean[] hasLiked = {feed.getLiked()};
    final boolean[] redirect = {feed.getBrSent()};
  • Please [edit] your question to provide the complete [stack trace](https://stackoverflow.com/a/23353174). – Mike M. Sep 06 '19 at 07:09
  • @MikeM. edited my quetion with stack trace – Jagdish Choudhary Sep 06 '19 at 07:27
  • Unless your `ArtworksData` class's `toString()` method returns its JSON representation, I really don't think you want to create an `ArtworksData` instance in the edit `Activity`. You're creating the `ArtworksData` in the first `Activity`, so you just want to pass back the JSON, right? – Mike M. Sep 06 '19 at 07:33
  • @MikeM. no mike, I am calling an api in Edit activity from which it returns a new instance of ArtworksData (Updated). Then I want to return that back to adapter and update the adapter. – Jagdish Choudhary Sep 06 '19 at 07:35
  • "it returns a new instance of ArtworksData" – It doesn't look like that. It looks like your API returns JSON, and you're creating an `ArtworksData` instance from that yourself. I'm saying that you don't want to do that in the edit `Activity`. You're doing it in the first `Activity`, so you need to pass back the JSON, not `ArtworksData#toString()`, which – unless you've overridden it – is likely returning something like `com.your.package.ArtworksData@1234567`. – Mike M. Sep 06 '19 at 07:39
  • But I am not sending the ArtworksData in the intent when directing to Edit activity. So how would I update that instance. – Jagdish Choudhary Sep 06 '19 at 07:42
  • You're not updating anything. You're creating a new `ArtworksData` instance, but currently, you're trying to do it twice – once in the edit `Activity`, and again in `onActivityResult()`. You only need to do it once, in `onActivityResult()`. To do that there, you need to pass back the JSON. – Mike M. Sep 06 '19 at 07:48
  • Okay, I passed json from Edit activity by intent.putExtra("ArtworkData", data.toString()); but still it is not updating. I am also sending back the position where it needs to be updated – Jagdish Choudhary Sep 06 '19 at 09:12
  • It doesn't look like it. In the posted code, the only extra you're putting on the returned `Intent` is `"ArtworkData"`. Did you forget to put `"position"`, too? – Mike M. Sep 06 '19 at 09:17
  • Sorry, I have added now. Still the same issue Failure delivering result ResultInfo – Jagdish Choudhary Sep 06 '19 at 09:23
  • Wait, what? Was it not crashing before you added that extra? Your last comment made it sound like it just wasn't updating, but still running. In either case, what's the current Exception message? – Mike M. Sep 06 '19 at 09:26
  • No change in the Exception message. I have edited question with the whole exception message. It's crashing now as well. – Jagdish Choudhary Sep 06 '19 at 09:37
  • No change? That's a completely different `Exception`. That `Exception` is happening in `SelfHorizontalScroll#instantiateItem()`. – Mike M. Sep 06 '19 at 09:44
  • OK, I didn't see the little snippet you added at the end. That means that either `getLiked()` or `getBrSent()` has a null field. There's not enough information here to determine why that is. – Mike M. Sep 06 '19 at 09:55
  • No but when I am coming back to this horizontal screen . It's not crashing. It has getLiked and getBrSent. Only when I am coming back from Edit activity it is getting null. Why is that? – Jagdish Choudhary Sep 06 '19 at 09:58
  • I have no idea. I don't even know what `feed` is. This is not the original error. – Mike M. Sep 06 '19 at 10:00
  • The earlier error is also coming with this. Anyway thanks for taking ur time man. I'll check my code again. Really appreciate ur efforts – Jagdish Choudhary Sep 06 '19 at 10:03
  • You'll still get the `Failure delivering result` error when anything goes wrong in `onActivityResult()`. The original `Exception` message was `Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $`. That's the message I was talking about. (You have to scroll all the way to the right to see it.) You got that one fixed, though, because now you're getting that new `NullPointerException`. Anyhoo, no problem. Glad we got one issue solved, at least. Cheers! – Mike M. Sep 06 '19 at 10:10

0 Answers0