New to Android Studio here.
I am trying to make it so when I click on an image from a ViewHolder in a RecyclerView, it would open up another activity that opens up another layout.
However, I am getting this error even though I already declared the activity in my Manifest.
The code that is trying to create an Intent to start a new activity is a class is called StaggeredView
inside app/core/StaggeredView
. While the activity handler is called ImageView
inside app/ImageView
.
Error
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.image_chan/android.widget.ImageView}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2065)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5320)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
at android.app.Activity.startActivityForResult(Activity.java:5278)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:662)
at android.app.Activity.startActivity(Activity.java:5664)
at android.app.Activity.startActivity(Activity.java:5617)
at com.example.image_chan.core.StaggeredAdapter$1.onClick(StaggeredAdapter.java:63)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Code
onBindViewHolder (which overrides the function from class StaggeredAdapter which extends RecyclerView.Adapter<StaggeredAdapter.ViewHolder>
)
public class StaggeredAdapter extends RecyclerView.Adapter<StaggeredAdapter.ViewHolder> {
/*...*/
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
RequestOptions requestOptions = new RequestOptions()
.error(R.drawable.error)
.placeholder(R.drawable.loader);
Glide.with(Context)
.load(urls.get(position))
.apply(requestOptions)
.into(holder.image);
holder.name.setText("SCORE: " + imgNames.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Context viewContext = (Context) view.getContext();
Intent toView = new Intent(viewContext, ImageView.class);
viewContext.startActivity(toView);
}
});
}
/*...*/
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.image_chan">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:networkSecurityConfig="@xml/netconfig"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Image_chan">
<provider android:name=".core.Suggestions"
android:authorities="com.example.image_chan.core.Suggestions"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Image_chan.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ImageView"
android:label="image_chan"
android:theme="@style/Theme.Image_chan.NoActionBar">
</activity>
<activity
android:name=".Main"
android:label="image_chan"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
</manifest>
Please follow these steps.
1.You should rename ImageView class to something else like ImageViewActivity or any thing.
2.Make sure this activity extends Activity or AppCompatActivity.
3.Make sure this activity is under directory app\src\main.
This should resolve your problem.