Source
Source

Reputation: 109

I get blank activity opened when starting new activity

I am using this library for multiple image selection. After the user finish selecting the photos, another activity (current_location.class) should start.

Here is my code to do that:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OPEN_MEDIA_PICKER) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK && data != null) {
                selectionResult = data.getStringArrayListExtra("result");
                Intent intent = new Intent(getActivity(), currentLocation.class);
                intent.putExtra("selectedMedia", selectionResult);
                startActivity(intent);
            }
        }
    }

new activity is successfully started but its blank!

Below is my currentLocation.java Activity:

public class currentLocation extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.current_location);
    }
}

and here is the current_location.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_view">
    
</android.support.constraint.ConstraintLayout>

it should have background color, I tried adding buttons as well, but I always get blank activity started

is the problem from the way I am starting new activity? or from the way I am setting the xml layout to the currentLocation class?

Upvotes: 2

Views: 1929

Answers (3)

Joseph Mathew
Joseph Mathew

Reputation: 1419

Replace Intent intent = new Intent(getActivity(), currentLocation.class);
with
Intent intent = new Intent(this.getActivity(), currentLocation.class);


Also Check if <activity android:name=".currentLocation"> is there in manifest.

Upvotes: 0

Behrouz Riahi
Behrouz Riahi

Reputation: 1791

try changing your onCreate method. use the following instead:

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

Upvotes: 13

CodeRedDev
CodeRedDev

Reputation: 529

Maybe it doesn't matter but I would've tried two things to lower the chance of an error.

  1. I would use putStringArrayListExtra() on the intent if you get an StringArrayListExtra from the activity result data intent

  2. I would work with the specific activity/fragment context when creating the new intent.
    E.g. new Intent(className.this, currentLocation.class);
    OR new Intent(className.this.getActivity(), currentLocation.class);

Upvotes: 0

Related Questions