So today I'm making a project using listview but I want to pass all the selected values with parcelable in android java. But I'm kinda confuse since I am using array to store the data. Here's my MainActivity.java looks like:
package com.example.lee;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
import static android.provider.Contacts.SettingsColumns.KEY;
public class MainActivity extends AppCompatActivity{
ListView list;
String[] title;
int[] image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = new int[]{R.drawable.poster_a_star, R.drawable.poster_aquaman, R.drawable.poster_avengerinfinity, R.drawable.poster_birdbox,
R.drawable.poster_bohemian};
title = new String[]{"A Star is Born", "Aquaman", "Avengers Infinity War", "Bird Box", "Bohemian Rhapsody"};
ListViewAdapters adapters = new ListViewAdapters(MainActivity.this, image, title, release_info, languages, genres);
list = (ListView)findViewById(R.id.listview_movie);
// Binds the Adapter to the ListView
list.setAdapter(adapters);
// Capture ListView item click
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ArrayList<Movie> movies = new ArrayList<>();
Movie movie = new Movie();
Intent i = new Intent(MainActivity.this, ObjectReceived.class);
i.putExtra("title",title);
// Pass listview item click position
i.putExtra("position", position);
//Pass image item click image
i.putExtra("image", image);
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
}
This one in my Movie.java class:
package com.example.lee;
import android.os.Parcel;
import android.os.Parcelable;
public class Movie implements Parcelable {
private String title;
private int image;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
protected Movie(Parcel in){
title = in.toString();
image = in.readInt();
}
public Movie(){
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeInt(image);
}
public static final Creator<Movie> CREATOR = new Creator<Movie>() {
@Override
public Movie createFromParcel(Parcel source) {
return new Movie(source);
}
@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};
}
And here's my ObjectReceived.java class:
package com.example.made_lee;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
import static android.provider.Contacts.SettingsColumns.KEY;
public class ObjectReceived extends AppCompatActivity {
public static final String EXTRA_MOVIE = "extra_person";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_received);
TextView tvObject = findViewById(R.id.object_received);
ArrayList<Movie> movies = getIntent().getParcelableArrayListExtra(EXTRA_MOVIE);
}
}
Here's the error messages:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.made_lee, PID: 25122
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.made_lee/com.example.made_lee.ObjectReceived}:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String[]
com.example.made_lee.Movie.getTitle()' on a null
object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[]
com.example.made_lee.Movie.getTitle()' on a null
object reference
at com.example.made_lee.ObjectReceived.onCreate(ObjectReceived.java:16)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
The List View is successfully show up, but whenever I clicked one of the item the app will directly stop running. I think it's because I don't pass the value correctly from MainActivity.java to ObjectReceived.java but in here I confuse about how to pass the value with array items. Please help me :D Thank You