单击列表项时,我试图将图像从列表视图传递到另一个活动。我设法弄清楚了如何从列表项中获取文本数据,但是一直停留在如何将图像从列表传递到预期活动的过程中。数据存储在包含图像作为资源的数组中。我知道我可能缺少一些琐碎的东西,但是我试图使它以多种方式工作,并查看了多个教程,但未能解决问题。
这是我的活动代码,其中声明了数组,声明了on item click监听器。
package com.example.android.musicplayer69;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
public class ArtistActivity extends AppCompatActivity {
public static final String KEY_SONG = "KEY_SONG";
public static final String KEY_ARTIST = "KEY_ARTIST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
ArrayList<Song> song = new ArrayList<Song>();
/** words.add("one")*/
song.add(new Song("Last Night", "The Strokes", "Is this
It",R.drawable.albumcover));
song.add(new Song("Golden Fleeces", "Israel Nash",
"Lifted",R.drawable.play));
song.add(new Song("Heavy, California", "Jungle", "Heavy
California",R.drawable.albumcover));
song.add(new Song("Grow Up", "Boiler", "Grow
Up",R.drawable.albumcover));
song.add(new Song("The Ballad of the Costa Concordia", "Car Seat
Head Rest", "Teens of Denial",R.drawable.albumcover));
song.add(new Song("Leaving", "Bass Drum of Death", "Just
Business",R.drawable.albumcover));
song.add(new Song("Creature Comfort", "Woods", "City Sun Eater in
the River of Light",R.drawable.albumcover));
song.add(new Song("Summer with Phill", "Dead Ghost", "Can't Get
No",R.drawable.albumcover));
song.add(new Song("Night Ride", "The Growlers", "Night
Ride",R.drawable.albumcover));
final SongAdapter adapter = new SongAdapter(this, song);
ListView listView = findViewById(R.id.list);
listView.setAdapter(adapter);
// When listView item is clicked, item data is sent to new activity
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Song item = adapter.getItem(position);
Intent nowPlaying = new Intent(getApplicationContext(),
MainActivity.class);
nowPlaying.putExtra(KEY_SONG, item.getmSong());
nowPlaying.putExtra(KEY_ARTIST, item.getmArtist());
nowPlaying.putExtra("IMAGE_RES",item.getAlbumArt());
startActivity(nowPlaying);}
});
}
}
,这是我试图将信息传递给该活动的代码。
package com.example.android.musicplayer69;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import static com.example.android.musicplayer69.ArtistActivity.KEY_ARTIST;
import static com.example.android.musicplayer69.ArtistActivity.KEY_SONG;
public class MainActivity extends AppCompatActivity {
private ImageSwitcher v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String playingSong = "";
String playingArtist = "";
Intent intent = getIntent();
if (null != intent) {
playingSong = intent.getStringExtra(KEY_SONG);
playingArtist = intent.getStringExtra(KEY_ARTIST);
{
}
}
Bundle bundle = this.getIntent().getExtras();
int nowPlayingArt = bundle.getInt("IMAGE_RES");
// Sets data to appropriate TextViews
TextView playingSongText = (TextView) findViewById(R.id.Song);
playingSongText.setText(playingSong);
TextView playingArtistText = (TextView) findViewById(R.id.Artist);
playingArtistText.setText(playingArtist);
ImageView nowPlayingAlbumArt = findViewById(R.id.AlbumCover);
nowPlayingAlbumArt.setImageResource(nowPlayingArt);
// Sets activity_main as the layout file
ImageView menu = findViewById(R.id.home_button);
// creates button/ sets on click listener for Artist View
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent menuIntent = new Intent(MainActivity.this,
menuActivity.class);`
startActivity(menuIntent);
}
});
}
}
这是我从控制台收到的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.musicplayer69, PID: 11885
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.musicplayer69/com.example.android.musicplayer69.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
at com.example.android.musicplayer69.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
答案 0 :(得分:0)
尝试将图像转换为字节数组。看一个例子。
示例(将图像转换为字节数组):
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
示例(传递您的字节数组):
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
示例(获取传递的数据并转换为位图):
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);