Home > Net >  Emulator app stopped after launching second activity
Emulator app stopped after launching second activity

Time:10-08

I have a ProfileActivity:

public class ProfileActivity extends AppCompatActivity {

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

    final ImageView exampleImage = (ImageView) this.findViewById(R.id.exampleImageView);
    exampleImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // [TODO] Implement application behavior when the user clicks the profile picture
            //Toast.makeText(ProfileActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(ProfileActivity.this, GalleryActivity.class));
        }
    });
}

I also have a second activity 'GalleryActivity':

public class GalleryActivity extends AppCompatActivity {
    ImageView imageView;
    private static final int PICK_IMAGE = 100;
    Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, PICK_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
                    imageUri = data.getData();
                    imageView.setImageURI(imageUri);
            }
    }

}

however when I launch the android emulator and then click on the image, it did not bring me to the photo gallery, rather it bring me this:
why is it and how I can solve it?\

E/AndroidRuntime: FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit  
 activity class 
{pickture.GalleryActivity}; 
have you declared this activity in your AndroidManifest.xml?

CodePudding user response:

It tells you: android.content.ActivityNotFoundException: Unable to find explicit
activity class

it means that you havn't added your GalleryActivity to Android manifest file. Check it.

And also, why do you even need this activity? You can start your Intent gallery = new Intent(Intent.ACTION_PICK,

MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, PICK_IMAGE);

from ProfileActivity.

CodePudding user response:

You are getting an ActivityNotFoundException on your GalleryActivity this means that there is something wrong with the way you are declaring your activities in your AndroidManifest.xml file. The error message actually suggests this if you read all the way though.

To fix this you need to declare your GalleryActivity in side of your application tag in side of the AndroidManifest.xml file

Basic Activity declaration

<activity
     android:name=".GalleryActivity"
     android:exported="true">
</activity>

Where to put the application declaration

<application
    ...
    ...>
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    <- PUT YOUR ACTIVITY DECLARATION HERE ->

</application>
  • Related