I am trying to pass a 3D-doublearray to another activity. I don know why I get NullpointerException? What is missing?
MainActivity
Intent intent = new Intent(this, DataActivity.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable("list", output_data);
intent.putExtras(mBundle);
startActivity(intent);
DataActivity (reveiver)
Intent intent = new Intent();
double[][][] params = (double[][][]) intent.getExtras().getSerializable("list");
And I know for certain that the 3d-array already is allocated in the MainActivity. I have tested that!
Would be glad if someone has any solution to this and could answer why I get a NullPointerException.
(Edit: NullpointerException at the line double[][][] params = ...)
I'm pretty sure double[][][] doesn't implement Serializable. What you can do is make your own class like so:
public class MyArrayWrapper implements Serializable {
private double[][][] arr;
public MyArrayWrapper(double[][][] value) {
arr = value;
}
public double[][][] getArray() {
return arr;
}
}
You then instantiate that wrapper class and put it as a serializable instead. Then when getting the serializable you do it like this:
MyArrayWrapper wrap = (MyArrayWrapper) intent.getExtras().getSerializable("list");
double[][]][] myArray = wrap.getArray();
Hope this helps!
Maybe you need a class that implements serializable?
Passing data through intent using Serializable
In this example, they are using custom class and List<> to serialize in a bundle. You can create a class with the 3D array inside?
Related
I am trying to pass arraylist from one activity to other using Bundles in OnPostExecute method,i am not able to do so.I am not getting proper error also to typecast or do stuffs to remove error.I am not sure what is wrong here.
***here reminderList is List<GetReminder> reminderList;***
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
//Invoke webservice
vaildUserId=WebService.invokeAuthenticateUserWS(loginUserName, loginPassword, "AuthenticateUser");
if(vaildUserId>=0){
reminderList=WebService.invokeHelloWorldWS("GetReminder");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("reminderList", reminderList);
reminderIntent.putExtras(bundle);
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
startActivity(reminderIntent);
}
To solve your problem, you can use putParcelableArrayListExtra() and getParcelableArrayListExtra() methods which are defined in the Intent class.
1.Make sure your GetReminder class implements Parcelable .
Here is the document of Parcelable and it also contains a typical implementation of Parcelable.
Here is a website which can help you generate an Parcelable implementation of a class automotically.
2.In your onPostExecute() method put extra like this:
//Remember to declare reminderList as ArrayList here.
ArrayList<GetReminder> reminderList = ...;
Intent intent = new Intent(getApplicationContext(), ReminderActivity.class);
intent.putParcelableArrayListExtra("reminderList", reminderList);
startActivity(intent);
Then in your ReminderActivity class get the ArrayList like this:
ArrayList<GetReminder> list = getIntent().getParcelableArrayListExtra("reminderList");
BTW, there is also another way to solve your problem, you can refer my answer here.
The problem is your List<GetReminder> reminderList; is not a String List.
To pass Custom Object List you have to make your Custom class either Serializable or Parcelable. In your case make GetReminder as Serializable or Parcelable.
Then use putExtra() or putSerializable() of Intent to pass Serializable object.
Also some supercilious code I noted from your onPostExecute() as you have written
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
startActivity(reminderIntent);
will cause creating two activity instance.
so remove first one,
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
I want to do this
class A extends Activity{
private class myClass{
}
myClass obj = new myClass();
intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable(Constants.Settings, obj); //I get the error The method putParcelable(String, Parcelable) in the type Bundle is not applicable for the arguments (int, A.myClass)
i.setClass(getApplicationContext(),B.class);
startActivity(i);
}
How do I use Parcelable to pass obj to activity B?
Create your class and implements Serializable:
private class myClass implements Serializable {
}
And do like:
myClass obj = new myClass();
Intent aActivity = (A.this, B.class);
intent.putExtra("object", obj);
On Receiving side:
myClass myClassObject = getIntent().getSerializableExtra("object");
As the error suggests, you need to make your class (myClass in this case) implement Parcelable. If you look at the documentation for Bundle, all the putParcelable methods take either a Parcelable or a collection of them in some form. (This makes sense, given the name.) So if you want to use that method, you need to have a Parcelable instance to put in the bundle...
Of course you don't have to use putParcelable - you could implement Serializable instead and call putSerializable.
Parcelable is pain in writing code but more cost effective than Serializable. Have a look at the given below link -
Parcelable Vs Serializable
Im tryin to bundle an Array of Arrays but is not working. Heres a snipped of code for better understanding:
Declaring and Initializing the variable
Inversor[][] reg_equipment= new Inversor[7][5];
for(int i=0; i<7; i++)
{
for(int j=0;j<5;j++)
{
reg_equipment[i][j]= new Inversor();
}
}
//....
Putting the variable in the bundle
bundle.putSerializable("reg_equipment", reg_equipment);
Intent myIntent =new Intent(RegisterEquipmentInversor.this,RegisterEquipmentMain.class);
myIntent.putExtras(bundle);
startActivity(myIntent);
At this point, the reg_equipment is filled with Inversors [Inversor[0],Inversor[1]....,Inversor[6]] and inside those There are more Inversors.
But when I go "get" the bundle in the other class
reg_equipment = (Inversor[][]) extras.getSerializable("reg_equipment");
This is whats inside de reg_equipment - [Object[0],Object[1],...,[Object[6]] and inside those Objects there are Inversors. Why does this happens ? How can I fix it ?
The class Inversor implements Serializable
Thanks
You should try to create a Serializable class that has only one property, which should be your array of Inversor arrays and put that object in your intent.
something like
public class InversorArrays implements Serializable {
public final static int serialVersionUID = //let eclipse generate your uid
public Inversor[][] myArray = null;
public InversorArrays (Inversor[][] _myArray){
this.myArray = _myArray;
}
}
and then, in your activity, create an instance of InversorArrays an pass it to the intent
Of course, Inversor and its properties should be serializable too.
This workaround sometimes saved me a lot of time and problems with typecasting and conversion problems
I am not sure, but have you made the investor class serializable? I think if we could get a basic view of the investor class, that may lead to some light.
I would say start off by making Investor serializable. http://www.tutorialspoint.com/java/java_serialization.htm
I'm trying to pass my dbhelper instance from one activity to another using this code
private void onCategoriesClick() {
private DbAdapter db;
db = new DbAdapter(this);
Intent i = new Intent(this, CategoriesActivity.class);
Bundle b = i.getExtras();
b.putSerializable("db", db); //geting NullPointerException here
startActivityForResult(i, 0);
DbAdapter class implements Serializable
I'm geting confused, could anybody point my mistake?
Create the dbhelper in the activity that will use it. If you really must pass it between activities, it would be much simpler to store it in a static variable (in your application class for easy access)
getExtras() returns null, because you haven't put any extras into the Intent before you call it. This means that b == null, hence the NPE. Just put the serializable in the intent directly.
i.putExtra("db", (Serializable) db);
In general, though, avoid passing serializables over intents if you can help it.
I had create a class named ChannelObj that contains values like this
public class ChannelObj {
public String enable;
public String id;
public String name;
public String ptz;
public ChannelObj(Node n){
this.enable = n.getAttributes().getNamedItem("enabled").getNodeValue();
this.id = n.getAttributes().getNamedItem("id").getNodeValue();
this.name = n.getAttributes().getNamedItem("name").getNodeValue();
this.ptz = n.getAttributes().getNamedItem("ptz").getNodeValue();
}
}
and this Class can create Obj that contains what data I need;
after that,I have an ArrayList named allChannel contains all ChannelObj i have
like this
for(int i = 0;i<num_of_channel;i++)
{
allChannel.add(new ChannelObj(n1.item(i)));
}
i've checked the data in allChannel is correct
but i want pass this ArrayList to next Activity
i've tried ways like
Intent i = new Intent(this,ChannelListActivity.class);
Bundle b = new Bundle();
b.putParcelableArrayListExtra("dd", ArrayList<ChannelObj> allChannels);
i.putExtra(String name,b);
startActivity(i);
but didn't work and still wrong
what i suppose to do?
thanks for your help!
An alternative to the answer given by Benoir is to have your ChannelObj class implement the Serializable interface. You're only using simple data types, so all the (de)serializing will be automa(g)(t)ically done underwater.
If your class implements Serializable, then you can add it to a Bundle as follows:
bundle.putSerializable("CHANNELOBJ_LIST", mChannelObjList);
Note that you may need to cast to an ArrayList<ChannelObj> (or some other concrete implementation of List<T>) as the List<T> interface does not implement Serializable.
Retrieving the list of objects in the next activity is similarly easy:
List<ChannelObj> mChannelObjList = (ArrayList<ChannelObj>) bundle.getSerializable("CHANNELOBJ_LIST");
Your clas must implement Parcelable check it out here: http://developer.android.com/reference/android/os/Parcelable.html