1

All solutions to this problem here did not help, so I ask in a new question.

I need to open an activity from an OnItemClickListener method of a ListView, but it simply does nothing... New activity is neither opened nor thrown an exception.

This is the code that tries to show the activity:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView <? > parent, View v, int position, long id) {
        try {
            ImageGalleryItem item = (ImageGalleryItem) parent.getItemAtPosition(position);
            //Create intent
            Intent intent = new Intent(v.getContext(), DetailsActivity.class);
            intent.putExtra("title", item.getTitle());
            intent.putExtra("image", item.getImage());

            //Start details activity
            startActivity(intent);
        } catch (Exception e) {
            Auditing.LogError(e);
        }
    }
});

This is the Manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cl.virtualice.tdc">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-feature android:name="android.hardware.Camera" android:required="true" />

    <application
        android:label="@string/app_title"
        android:icon="@drawable/ic_tdc"
        android:theme="@style/AppTheme"
        android:allowBackup="true">

        <activity
            android:name=".activities.MainActivity"
            android:theme="@style/AppTheme"
            android:label="@string/app_title">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.FormActivity"
            android:parentActivityName=".activities.MainActivity">
        </activity>
        <activity
            android:name=".activities.GalleryActivity"
            android:parentActivityName=".activities.FormActivity">
        </activity>
        <activity
            android:name=".activities.DetailsActivity"
            android:parentActivityName=".activities.GalleryActivity">
        </activity>
    </application>

</manifest>

And this is the new Activity code:

package cl.virtualice.tdc.activities;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import cl.virtualice.tdc.R;

/**
 * Created by Jaime on 23-10-2015.
 */
public class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        String title = getIntent().getStringExtra("title");
        Bitmap bitmap = getIntent().getParcelableExtra("image");

        TextView titleTextView = (TextView) findViewById(R.id.title);
        titleTextView.setText(title);

        ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setImageBitmap(bitmap);
    }
}

Any help please?

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
jstuardo
  • 3,901
  • 14
  • 61
  • 136

3 Answers3

0

Did you try this:

Intent intent = new Intent(getApplicationContext(), DetailsActivity.class);
Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • I have found that the problem is about ths instruction: intent.putExtra("image", item.getImage());.... getImage returns a bitmap. I have commented it out and the activity is called... so, the problem is reduced to how to call an activity passing a Bitmap as the parameter – jstuardo Oct 27 '15 at 21:47
0

Your issue possibly is this point:

gridView.setOnItemClickListener

It should be:

listView.setOnItemClickListener

And make sure that your program get into the try block. Then I think you could use:

Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this, DetailsActivity.class);

YOUR_CURRENT_ACTIVITY is the activity containing the ListView

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
  • I had originally that, but after reading a lot of posts here, I changed to the way shown in my question – jstuardo Oct 27 '15 at 21:32
0

There is a chance that Activity does start, but closes immediately because it lacks for example proper intent parameter or parameter has wrong type (for example Long and Integer will be treated differently).

Like you commented in @oguzhand answer. There was indeed a problem with intent and when you removed it the activty did start. But in other cases you will want to place breakpoint in called Activity's onCreate method and see if it's running.

As for the second question "how to pass Bitmap as extra", it has been answered here by user849998: https://stackoverflow.com/a/8407644/4911442 :

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
R.drawable.ic_launcher);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

then in called Activity you will want to retrieve it like this:

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

Haven't tested it myself.

Community
  • 1
  • 1
Darek Deoniziak
  • 763
  • 8
  • 17