• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Clickable Image in Recycler View Android Studio

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm doing a project where you click an image then launches the second activity that contains the image, title and description (it changes depending on the image). I have already set the onClickListener but it doesn't work. The project is working fine and I'm not getting an error as I run it but when I'm in the actual project itself, whenever I click the image the app suddenly closes without giving error message. Any idea how to solve this? Thanks

> Main Activity

   public class MainActivity extends AppCompatActivity {


   RecyclerView mainRecycler;
   RecyclerView.Adapter adapter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       mainRecycler = findViewById(R.id.main_recycler);
       mainRecycler();
   }

   private void mainRecycler() {
       mainRecycler.setHasFixedSize(true);
       mainRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

       ArrayList<MainHelperClass> mainLocations = new ArrayList<>();
       mainLocations.add(new MainHelperClass(R.drawable.image1, "title1", "desc1"));
       mainLocations.add(new MainHelperClass(R.drawable.image2, "title2", "desc2"));
       mainLocations.add(new MainHelperClass(R.drawable.image3, "title3, "desc3"));

       adapter = new MainAdapter(mainLocations, getApplicationContext());
       mainRecycler.setAdapter(adapter);

   }

   }

> Adapter

   public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {

   ArrayList<MainHelperClass> mainLocations;
   Context context;
   public MainAdapter(ArrayList<MainHelperClass> mainLocations, Context context) {
       this.mainLocations = mainLocations;
       this.context = context;
   }


   @NonNull
   @Override
   public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_card_design,parent,false);
       MainViewHolder mainViewHolder = new MainViewHolder(view);
       return mainViewHolder;
   }

   @Override
   public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {

       final MainHelperClass mainHelperClass = mainLocations.get(position);
       holder.image.setImageResource(mainHelperClass.getImage());
       holder.title.setText(mainHelperClass.getTitle());
       holder.desc.setText(mainHelperClass.getDescription());

       holder.itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(context, MainDetails.class);
               intent.putExtra("image", mainHelperClass.getImage());
               intent.putExtra("title", mainHelperClass.getTitle());
               intent.putExtra("description", mainHelperClass.getDescription());
               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity(intent);
           }
       });
   }

   @Override
   public int getItemCount() {
       return mainLocations.size();
   }

   public static class MainViewHolder extends RecyclerView.ViewHolder{

       ImageView image;
       TextView title, desc;

       public MainViewHolder(@NonNull View itemView) {
           super(itemView);
           image = itemView.findViewById(R.id.main_image);
           title = itemView.findViewById(R.id.main_title);
           desc = itemView.findViewById(R.id.main_desc);
       }
   }

   }

> Details Activity

   public class MainDetails extends AppCompatActivity {
   ImageView image;
   TextView title, description;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main_details);

       image = (ImageView)findViewById(R.id.main_image_details);
       title = (TextView)findViewById(R.id.main_title_details);
       description = (TextView)findViewById(R.id.main_desc_details);

       image.setImageResource(getIntent().getIntExtra("image", 0));
       title.setText(getIntent().getStringExtra("title"));
       description.setText(getIntent().getStringExtra("description"));

   }

   }
 
Bartender
Posts: 7645
178
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

the app suddenly closes without giving error message.


Whenever this happens on Android, your first stop is to examine the logcat - it includes the full exception stack trace that terminated the app. Post that here in full.
 
Hachi Man
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> This is what logcat is showing

Process: com.example.galila, PID: 2293
   android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.galila/com.example.galila.MainDetails}; have you declared this activity in your AndroidManifest.xml?
       at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2056)
       at android.app.Instrumentation.execStartActivity(Instrumentation.java:1716)
       at android.app.ContextImpl.startActivity(ContextImpl.java:985)
       at android.app.ContextImpl.startActivity(ContextImpl.java:944)
       at android.content.ContextWrapper.startActivity(ContextWrapper.java:394)
       at com.example.galila.MainAdapter$1.onClick(MainAdapter.java:52)
       at android.view.View.performClick(View.java:7257)
       at android.view.View.performClickInternal(View.java:7213)
       at android.view.View.access$3800(View.java:828)
       at android.view.View$PerformClick.run(View.java:27921)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7830)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1040)
2020-12-21 22:06:20.157 2820-2840/? E/hypnusd: submit dspfreq failed(Unknown error 524)
2020-12-21 22:06:20.157 2820-2840/? E/hypnusd: submit dspfreq failed(Unknown error 524)
2020-12-21 22:06:20.165 1503-3651/? E/Process: get_ion_cache_memory: Unable to open /d/ion/system_stats
2020-12-21 22:06:20.166 1503-3651/? E/Process: get_ion_cache_memory: Unable to open /d/ion/system_stats
2020-12-21 22:06:20.167 1503-3651/? E/Process: get_ion_cache_memory: Unable to open /d/ion/system_stats
2020-12-21 22:06:20.194 1503-1631/? E/InputDispatcher: channel 'bb1c39f com.example.galila/com.example.galila.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2020-12-21 22:06:20.198 2820-2840/? E/hypnusd: submit dspfreq failed(Unknown error 524)
2020-12-21 22:06:20.212 546-603/? E/UxUtility: notifyAppState error = NULL
2020-12-21 22:06:20.213 1503-3645/? E/HoraeUtil: Cannot connect to HoraeService
2020-12-21 22:06:20.648 2820-2840/? E/hypnusd: submit dspfreq failed(Unknown error 524)
2020-12-21 22:06:20.670 2824-2824/? E/oiface: packageSwitch pkg com.oppo.launcher, enter
2020-12-21 22:06:20.671 2824-2824/? E/oiface: current package is com.oppo.launcher
2020-12-21 22:06:20.675 1503-19085/? E/Process: get_ion_cache_memory: Unable to open /d/ion/system_stats
2020-12-21 22:06:20.679 2824-2848/? E/oiface: access /sys/devices/system/cpu/cpu4/cpufreq/stats/time_in_state failed(No such file or directory)
2020-12-21 22:06:20.679 2824-2848/? E/oiface: Average freq 0 for 0s.
2020-12-21 22:06:20.679 2824-2848/? E/oiface: access /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state failed(No such file or directory)
2020-12-21 22:06:20.679 2824-2848/? E/oiface: Average freq 0 for 0s.
2020-12-21 22:06:20.681 2824-2848/? E/oiface: get gpu freq table failed
2020-12-21 22:06:20.684 2824-2848/? E/oiface: client  not found, skipped
2020-12-21 22:06:20.691 2824-2848/? E/oiface: access /sys/devices/system/cpu/cpu4/cpufreq/stats/time_in_state failed(No such file or directory)
2020-12-21 22:06:20.691 2824-2848/? E/oiface: access /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state failed(No such file or directory)
2020-12-21 22:06:20.692 2824-2848/? E/oiface: get gpu freq table failed
2020-12-21 22:06:47.834 2237-2378/? E/memtrack: Couldn't load memtrack module
2020-12-21 22:06:58.407 3321-2379/? E/memtrack: Couldn't load memtrack module
2020-12-21 22:07:06.059 1092-1092/? E/raw_hw_default: open dev/block/platform/bootdevice/by-name/reserve2 success
2020-12-21 22:07:06.059 1092-1092/? E/raw_hw_default: item id=508,0x260096
2020-12-21 22:07:06.061 1092-1092/? E/raw_hw_default: open dev/block/platform/bootdevice/by-name/reserve2 success
2020-12-21 22:07:06.061 1092-1092/? E/raw_hw_default: item id=1532,0x784384
 
Tim Moores
Bartender
Posts: 7645
178
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Unable to find explicit activity class {com.example.galila/com.example.galila.MainDetails}; have you declared this activity in your AndroidManifest.xml?


Bingo! Is it declared? Without spelling mistakes?
 
Hachi Man
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you solved it lmao, I just need to add the second activity in the android manifest. Logcat is very helpful.
reply
    Bookmark Topic Watch Topic
  • New Topic