1

I have created the class textViewTable In this class i am saving data related to TextViews That I want to Pass to Next Activity.

public class TextViewTable implements Serializable {
private String FONT;
private String TEXT;
private float TEXT_SIZE;
private ColorStateList TEXT_COLOR;
private float MARGIN_TOP;
private float MARGIN_BOTTOM;
private float MARGIN_LEFT;
private float MARGIN_RIGHT;
private Boolean BoldFlag;
private Boolean ItalicFlag;
private Boolean NormalFlag;

public TextViewTable(){
}

public TextViewTable(String FONT, String TEXT, float TEXT_SIZE, ColorStateList TEXT_COLOR, float MARGIN_TOP, float MARGIN_BOTTOM, float MARGIN_LEFT, float MARGIN_RIGHT, Boolean boldFlag, Boolean italicFlag, Boolean normalFlag) {
    this.FONT = FONT;
    this.TEXT = TEXT;
    this.TEXT_SIZE = TEXT_SIZE;
    this.TEXT_COLOR = TEXT_COLOR;
    this.MARGIN_TOP = MARGIN_TOP;
    this.MARGIN_BOTTOM = MARGIN_BOTTOM;
    this.MARGIN_LEFT = MARGIN_LEFT;
    this.MARGIN_RIGHT = MARGIN_RIGHT;
    BoldFlag = boldFlag;
    ItalicFlag = italicFlag;
    NormalFlag = normalFlag;
}

}

From my activit i want to send ArrayList of Objects of TextViewTable class. I have use the below function to send the ArrayList. But every time I am getting null pointer exception. Please Help to solve this.

public void onClick(View view)
    {
        Intent intent = new Intent(getApplicationContext(), displayImage.class);            
        Bundle bundleObject = new Bundle();
        bundleObject.putSerializable("key", textViewsData);

        intent.putExtras(bundleObject);
        try {
            startActivity(intent);
        }catch (Exception e){
            System.out.println(e);
        }
    }
};

LogCat

NighterZ
  • 57
  • 10
  • You need to find where the pointer is null, and then why it is null. See: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – mins Apr 10 '15 at 06:13
  • just read your stacktrace : Caused by NotSerializableException ... – Ridcully Apr 10 '15 at 06:44
  • use some singleton class set list in one activity and read it in other activity. This will make task more easy – virendrao Apr 10 '15 at 06:45

5 Answers5

1

Currently using Bundle.putSerializable for sending TextViewTable class object ArrayList to next Activity but not implementing Serializable interface in TextViewTable class:

public class TextViewTable implement Serializable{

 ....
}
ρяσѕρєя K
  • 127,886
  • 50
  • 184
  • 206
  • its not working ... null pointer exception on this line startActivity(intent) – NighterZ Apr 10 '15 at 06:30
  • @NighterZ: Please share logcat stace – ρяσѕρєя K Apr 10 '15 at 06:31
  • null pointer exception resolved..now I am getting this exception java.lang.RuntimeException: Parcelable encountered IOException writing serializable object...wait I post logcat stace – NighterZ Apr 10 '15 at 06:34
  • @NighterZ: please share exception – ρяσѕρєя K Apr 10 '15 at 06:34
  • null pointer exception was due to some other reason, that piece of code is not in the post. and i am sorry I don't how to post logcat stace so I post the screen shot of the logcat. – NighterZ Apr 10 '15 at 06:42
  • hmm .. it complains about ColorStateList but according to developer.android.com/reference/android/content/res/ColorStateList.html it should implement Parcelable – cYrixmorten Apr 10 '15 at 07:01
  • @cYrixmorten your right problem is with ColorStateList. I am still trying to solve it. If you know some solution then please tell me. – NighterZ Apr 10 '15 at 19:56
  • 1
    @NighterZ I would either simply store a static reference to TextViewTable in another class, which then can be fetched by the second activity, or use EventBus https://github.com/greenrobot/EventBus. Then you can do something like `EventBus.getDefault().postSticky(yourTextViewTable)` => start second activity => in onCreate() `TextViewTable table = EventBus.getDefault().getSticky(TextViewTable.class)` – cYrixmorten Apr 10 '15 at 21:02
1

you can follow ρяσѕρєя K's answer OR also you can do like below code:

public class GeneralClass{

  public static ArrayList<TextViewTable> data = new ArrayList<TextViewTable>();

}

and then you can store your data in above arraylist on first activity like below:

Collections.copy(GeneralClass.data,textViewsData);

and now you can use GeneralClass.data arraylist in your second activity;

Sagar Maiyad
  • 12,164
  • 8
  • 57
  • 96
0

Sending the POJO from one activity to another acitivity:

Bundle bundle = new Bundle();
ArrayList<StatusData> passData = new ArrayList<StatusData>();
bundle.putSerializable("key", passData);
intent.putExtras(bundle);
startActivity(intent);
//then the transaction part

Getting the bundle`:

    Bundle bundle = new Bundle();
    bundle = getIntent().getExtras();
   ArrayList<StatusData> dataReceived = (ArrayList<StatusData>)bundle.getSerializable("key"));

and then do whatever you like.Hope this helps.Cheers. You can also use Parcelable.

Setu Kumar Basak
  • 9,449
  • 6
  • 37
  • 66
0

First Activity

public void shareCustomArrayListObject(ArrayList<CUSTOMOBJECT> arrayList) {
    if (arrayList != null && arrayList.size() > 0) {

         Intent intent = new Intent(getApplicationContext(), displayImage.class);            
         Bundle bundleObject = new Bundle();
         bundle.putSerializable("KEY", arrayList);

         intent.putExtras(bundleObject);
    }
}

Second activity where you want to retrieve the arraylist

 private ArrayList<CUSTOMOBJECT> arrayList;

    Bundle bundle=YOURACTIVITY.getBundle();

    if(bundle==null){
        bundle=getArguments();
    }

    if(bundle.getSerializable("KEY")!=null){
    arrayList=(ArrayList)bundle.getSerializable("KEY");
    }

and also if you have made a bean class for the arraylist you need to implement

public class CUSTOMOBJECT implements Serializable{

and you done :)

Jeffy Lazar
  • 1,763
  • 11
  • 19
0

you should use Parcelable object to pass data between activities as below:

Passing data from activity A to activity B

Intent intent=new Intent(A.this,B.class);
intent.putParcelableArrayListExtra("key", array_list);
startActivity(intent);

Getting data in Activity B from activity A

Intent intent=getIntent();
array_list = intent.getParcelableArrayListExtra("key");

So simple.

I hope this will help you.

Community
  • 1
  • 1
Amrut Bidri
  • 5,898
  • 6
  • 31
  • 76