1. Are you ready for the Galaxy S20? Here is everything we know so far!

tried this but I get the first image --- when try to get the next image App in emulator closes with

Discussion in 'Android Development' started by Lsepolis123, Jun 7, 2020.

  1. Lsepolis123

    Lsepolis123 Lurker
    Thread Starter

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    android:id="@+id/rootContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:eek:rientation="vertical">

    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:text="Change Image"/>

    </LinearLayout>

    MainActivity .java

    ...
    ...
    public class MainActivity extends AppCompatActivity {
    ...
    button.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View view) {
    FetchJsonData process = new FetchJsonData(linearLayout, imageView);
    process.execute();
    }
    });

    ====
    FetchJsonData .java
    ....
    public class FetchJsonData extends AsyncTask<Void, Void, Void> {

    TextView data;
    LinearLayout sourceText;
    String dataParsed = "";
    String singleParsed ="";
    public ImageView imageView;
    public Integer imageId = 0;
    URL[] imgUrl = new URL[10];

    public FetchJsonData(LinearLayout linearLayout, ImageView imageView) {
    // loop


    loop
    {

    imgUrl[j] = new URL(attractionPhotosImagesLarge.getString("url"));


    }


    imageId = imageId + 1;

    URL req = imgUrl[imageId];

    Bitmap mIcon_val = BitmapFactory.decodeStream(req.openConnection().getInputStream());

    this.imageView.setImageBitmap(mIcon_val); // line 119

    setting an imageView programmatically... --- in my code I want every time calling the class FetchJsonData to get the next image on an array... I tried this but I get the first image --- when try to get the next image App in emulator closes with error... what to do to delete/remove imageView to appear next image in imageView?????

    2020-06-07 07:18:03.866 23338-23497/com.leonidassavvides.tripadvisorapi E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
    Process: com.leonidassavvides.tripadvisorapi, PID: 23338
    java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:318)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
    Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6855)
    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1075)
    at android.view.ViewGroup.invalidateChild(ViewGroup.java:5242)
    at android.view.View.invalidateInternal(View.java:13574)
    at android.view.View.invalidate(View.java:13538)
    at android.view.View.invalidate(View.java:13522)
    at android.widget.ImageView.setImageDrawable(ImageView.java:514)
    at android.widget.ImageView.setImageBitmap(ImageView.java:635)
    at com.leonidassavvides.tripadvisorapi.FetchJsonData.doInBackground(FetchJsonData.java:119)
    at com.leonidassavvides.tripadvisorapi.FetchJsonData.doInBackground(FetchJsonData.java:27)

    at android.os.AsyncTask$2.call(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
     


  2. wseng92

    wseng92 Android Enthusiast

    Use this

    Code (Java):
    1. runOnUiThread(new Runnable() {
    2.     @Override
    3.     public void run() {
    4.         // Stuff that updates the UI
    5.     }
    6. });
     
Loading...
Similar Threads - tried image try
  1. loader
    Replies:
    10
    Views:
    445
  2. Davis k890
    Replies:
    20
    Views:
    637
  3. Helpjason
    Replies:
    2
    Views:
    492
  4. DianaF59
    Replies:
    10
    Views:
    356
  5. MoodyBlues
    Replies:
    7
    Views:
    709
  6. Rob
    Replies:
    0
    Views:
    487
  7. Rob
    Replies:
    3
    Views:
    359
  8. derekhhh
    Replies:
    1
    Views:
    452
  9. nagamothu
    Replies:
    0
    Views:
    644
  10. nagamothu
    Replies:
    6
    Views:
    2,839

Share This Page

Loading...