I am working on an application in which I am using android phone contacts in my application.So, for that first of all fetch contact details and store it into ArrayList. After that I serialized that arrayList using gson library and when I deserialize it getting error.
I have used gson-2.1.jar for serializing and deserializing contact details. I am getting following error log.
AndroidRuntime﹕ FATAL EXCEPTION: main
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 8
at com.google.gson.Gson.fromJson(Gson.java:766)
at com.google.gson.Gson.fromJson(Gson.java:721)
at com.google.gson.Gson.fromJson(Gson.java:670)
at com.google.gson.Gson.fromJson(Gson.java:642)
at com.braintechnix.autoloveapp.util.MyGson.deserializer(MyGson.java:28)
at com.braintechnix.autoloveapp.fragment.ContactListFragment.onStart(ContactListFragment.java:69)
at android.support.v4.app.Fragment.performStart(Fragment.java:1986)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 8
at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:306)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at com.google.gson.Gson.fromJson(Gson.java:755)
at com.google.gson.Gson.fromJson(Gson.java:721)
at com.google.gson.Gson.fromJson(Gson.java:670)
at com.google.gson.Gson.fromJson(Gson.java:642)
at com.braintechnix.autoloveapp.util.MyGson.deserializer(MyGson.java:28)
at com.braintechnix.autoloveapp.fragment.ContactListFragment.onStart(ContactListFragment.java:69)
at android.support.v4.app.Fragment.performStart(Fragment.java:1986)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Following is the code which I am using:
public ArrayList<Contact> deserializer(String jsonString){
//Type collectionType = new TypeToken<Collection<Contact>>(){}.getType();
// Collection<Contact> enums = gson.fromJson(jsonString, collectionType);
Contact[] contacts = gson.fromJson(jsonString, Contact[].class);
ArrayList<Contact> contactsList = new ArrayList<Contact>();
for(int i = 0; i< contacts.length; i++){
contactsList.add(contacts[i]);
}
return contactsList;
}
it is really hard to answer without the json response you are getting but from the error you have i can tell the following code will help you
Updated the answer
public ArrayList<Contact> deserializer(String jsonString){
//Type collectionType = new TypeToken<Collection<Contact>>(){}.getType();
// Collection<Contact> enums = gson.fromJson(jsonString, collectionType);
ArrayList<Contact> contactsList = (new Gson()).fromJson(jsonString,new TypeToken<ArrayList<Contact>>() {}.getType());
return contactsList;
}
following are some example that might help you with Gson library
ExampleOne
ExampleTwo
Related
I have an array of pictures which i extract the longitude and latitude details from using exif and put them on a marker on the GPS map. Currently i am using static array which i plan to change to a dynamic array in the near future. The current code crashes my app everytime i try to set my marker, i have no idea whats wrong.
public void setMarker() {
double latitude, longitude;
String[] filepath = {"/storage/emulated/0/Pictures/CapturyGallery/CapImage1451887532716.jpg", "/storage/emulated/0/Pictures/CapturyGallery/CapImage1452411474153.jpg"};
ExifInterface intf = null;
for (int y = 0; y < filepath.length; y++) {
try {
intf = new ExifInterface(filepath[y]);
} catch (IOException e) {
e.printStackTrace();
}
if (intf != null) {
latitude = Double.parseDouble(intf.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
longitude = Double.parseDouble(intf.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));
LatLng latLngCurrent = new LatLng(latitude, longitude);
MarkerOptions options = new MarkerOptions()
.position(latLngCurrent)
.title("test");
mMap.addMarker(options);
}
}
}
LOGCAT
`
01-11 15:19:30.251 32226-32226/com.sp.fyp.captury E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sp.fyp.captury, PID: 32226
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4298)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6843)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4293)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6843)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NumberFormatException: Invalid double: "1/1,18/1,30/1"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at com.sp.fyp.captury.GPS.setMarker(GPS.java:181)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4293)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6843)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)`
According to the Standard Exif Tags (as defined in the Exif 2.3 standard)
The latitude/longitude is expressed as three RATIONAL values giving
the degrees, minutes, and seconds, respectively. When degrees, minutes
and seconds are expressed, the format is dd/1,mm/1,ss/1. When degrees
and minutes are used and, for example, fractions of minutes are given
up to two decimal places, the format is dd/1,mmmm/100,0/1
Thus, when you get the latitude and logitude from the ExifInterface, you are getting values like 1/1,18/1,30/1. Of course this String values cannot be converted to double using Double.parseDouble and throw a NumberFormatException.
You can take a look at How do I convert Exif long/lat to real values? to find a converter.
I have to save my custom class in save state bundle. So, I made it Parcelable and it looks good. When I rotate screen everything is goind well. But, when I minimalize my app, open some other, rotate screen and I try to go back to my app (from recent apps) it crashes.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.game/com.example.game.MainActivity}: java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = com.example.game.Player)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = com.example.game.Player)
at android.os.Parcel.readSerializable(Parcel.java:2378)
at android.os.Parcel.readValue(Parcel.java:2197)
at android.os.Parcel.readListInternal(Parcel.java:2520)
at android.os.Parcel.readList(Parcel.java:1655)
at com.example.game.Players.<init>(Players.java:106)
at com.example.game.Players$1.createFromParcel(Players.java:96)
at com.example.game.Players$1.createFromParcel(Players.java:93)
at android.os.Parcel.readParcelable(Parcel.java:2246)
at android.os.Parcel.readValue(Parcel.java:2146)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2479)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:871)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1864)
at android.app.Activity.onCreate(Activity.java:949)
at com.example.game.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.ClassNotFoundException: com.example.game.Player
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:308)
at android.os.Parcel$2.resolveClass(Parcel.java:2364)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1641)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
at android.os.Parcel.readSerializable(Parcel.java:2372)
at android.os.Parcel.readValue(Parcel.java:2197)
at android.os.Parcel.readListInternal(Parcel.java:2520)
at android.os.Parcel.readList(Parcel.java:1655)
at com.example.game.Players.<init>(Players.java:106)
at com.example.game.Players$1.createFromParcel(Players.java:96)
at com.example.game.Players$1.createFromParcel(Players.java:93)
at android.os.Parcel.readParcelable(Parcel.java:2246)
at android.os.Parcel.readValue(Parcel.java:2146)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2479)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:871)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1864)
at android.app.Activity.onCreate(Activity.java:949)
at com.example.game.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.ClassNotFoundException: com.example.game.Player
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:308)
at android.os.Parcel$2.resolveClass(Parcel.java:2364)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1641)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
at android.os.Parcel.readSerializable(Parcel.java:2372)
at android.os.Parcel.readValue(Parcel.java:2197)
at android.os.Parcel.readListInternal(Parcel.java:2520)
at android.os.Parcel.readList(Parcel.java:1655)
at com.example.game.Players.<init>(Players.java:106)
at com.example.game.Players$1.createFromParcel(Players.java:96)
at com.example.game.Players$1.createFromParcel(Players.java:93)
at android.os.Parcel.readParcelable(Parcel.java:2246)
at android.os.Parcel.readValue(Parcel.java:2146)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2479)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:871)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1864)
at android.app.Activity.onCreate(Activity.java:949)
at com.example.game.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
So, players is parcelable class. It is basically a List. Player is another class, serializable.
Player.java
import java.io.Serializable;
public class Player implements Serializable{
private String name;
private int points = 0;
// getters and setters
}
Players.java
public class Players implements Parcelable {
private List<Player> players;
//constructor, getters, setters
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeList(players);
}
public static final Parcelable.Creator<Players> CREATOR = new Parcelable.Creator<Players>() {
public Players createFromParcel(Parcel in) {
return new Players(in);
}
public Players[] newArray(int size) {
return new Players[size];
}
};
public Players(Parcel in) {
in.readList(players, List.class.getClassLoader());
}
}
At this point I don't know why my app crash. When I rotate it it's ok, when I minimize and maximize is ok, when I minimize, do some staff (open another app, rotate screen etc) and try to maximize it from recents it crash.
I'll be very gratefull for any kind of help ;) !
Have a good afternoon!
Parcelable class nest parcelable objects only.
Here in your case Player is not parcelable and is used in Parcelable class Players. Try to implement Parcelable in Player class too.
Your problem with minimising and opening the application is because Parcelable object is saved but it is not retrieved properly.
try to make following changes in your code :
public class Player implements Parcelable{
String name;
int points;
// getter setter
public static final Creator<Player> CREATOR = new Parcelable.Creator<Player>() {
#Override
public Player createFromParcel(Parcel in) {
return new Player(in);
}
#Override
public Player[] newArray(int size) {
return new Player[size];
}
};
public Player(Parcel in) {
this.name = in.readString();
this.points = in.readInt();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(points);
}
}
And in Players class while reading the list replace:
in.readList(players, List.class.getClassLoader());
with :
players = new ArrayList<>();
in.readList(players, getClass().getClassLoader());
Your code should work fine after these changes.
I was trying to make a ListView of reminder notes. But when I try to save it and display it in a ListView, app crashes with this error/exception. saveNote method is called when you press save button. Any help is appreciated.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case R.id.add_reminder : {
et = (EditText)findViewById(R.id.new_note);
et.setVisibility(View.VISIBLE);
note = et.getText().toString();
i++;
Button save_button = (Button) findViewById(R.id.button_save);
save_button.setVisibility(View.VISIBLE);
}
default : return super.onOptionsItemSelected(item);
}
}
public void saveNote(View view) {
String[] notes = new String[20];
notes[i] = note;
listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,notes);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Clicked item index
int itemPosition = position;
//Clicked item value
String itemNote = (String) listView.getItemAtPosition(position);
//Alert
Toast.makeText(getApplicationContext(), "Position :" + itemPosition + " ListItem : " + itemNote, Toast.LENGTH_LONG).show();
}
});
Logcat
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.cokosofts.pramith.reminder.MainActivity.saveNote(MainActivity.java:69)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
The main cause which I found from the Logs is ListView is null. Please check one more time if the listView is empty or not? Also what is notes[i] = note ??
I think in each Save button click you are finding ListView and the setting Adapter each time. This is not a good approach from my point of view. You should set the adapter very first time and next time just update your adapter data and call notifyDataSetChanged();
Debug and find out why your ListView was not found on your layout (was null).
Problem is with following line:
listView = (ListView) findViewById(R.id.list);
Please check whether your xml has ListView widget with id as 'list' .
If id is different in XML it will result in a NULL object reference. When you call
listView.setAdapter(adapter)
It will give a NULL pointer exception.
when I tried to add some values to my List inside my onResume() method i got this error:
java.lang.RuntimeException: Unable to resume activity {.../...MainActivity}: java.lang.UnsupportedOperationException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3096)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3127)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2482)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5415)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at de.toeducate.tograde.MainActivity.onResume(MainActivity.java:151)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
at android.app.Activity.performResume(Activity.java:6237) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3085)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3127)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2482)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5415)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
and here is the code I wrote:
List<Integer> marks = Collections.emptyList();
for (int i = 0; i < dataBase.getDataMark().size(); i++) {
marks.add(dataBase.getDataMark().get(i).getValue());
}
What does this mean?
Collections.emptyList(); returns a List that you cannot add to (hence the UnsupportedOperationException).
Use
List<Integer> marks = new ArrayList<Integer>();
instead.
Collections.emptyList() returns an instance of AbstractList. This is meant by the line
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
in your exception's stack trace. This said you cannot call add on your marks list as add is not implemented.
Instead instantiate marks with a class that allows for adding elements, such as ArrayList:
List<Integer> marks = new ArrayList<Integer>();
Besides ArrayList, LinkedList is another implementation of AbstractList that supports adding elements. Most typically programmers use an ArrayList.
I am trying to locate a missing onError() in a project. It means that the app crashes because a subscription does not handle the throwables so I want to locate that subcription and add the onError method.
Unfortunatelly the stacktrace is not really helpful here and it only shows the line of the throw new IOException but nothing more:
FATAL EXCEPTION: main
Process: my.app.example.dev, PID: 20309
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:54)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:201)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
at rx.android.app.OperatorConditionalBinding$1.onError(OperatorConditionalBinding.java:69)
at rx.internal.operators.NotificationLite.accept(NotificationLite.java:147)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:177)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.access$000(OperatorObserveOn.java:65)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:153)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: rx.exceptions.CompositeException: 2 exceptions occurred.
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:201)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
at rx.android.app.OperatorConditionalBinding$1.onError(OperatorConditionalBinding.java:69)
at rx.internal.operators.NotificationLite.accept(NotificationLite.java:147)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:177)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.access$000(OperatorObserveOn.java:65)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:153)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
at com.splunk.mint.ExceptionHandler.uncaughtException(ExceptionHandler.java:42)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:58)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: retrofit.RetrofitError: java.io.IOException: No connectivity
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:385)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:221)
at retrofit.RestAdapter$RestHandler$1.call(RestAdapter.java:271)
at retrofit.RestAdapter$RestHandler$1.call(RestAdapter.java:269)
at retrofit.RxSupport$2.run(RxSupport.java:46)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.io.IOException: No connectivity
at my.app.example.manager.ApiManager$NetworkAwareOKClient.execute(ApiManager.java:1071)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:322)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:221)
at retrofit.RestAdapter$RestHandler$1.call(RestAdapter.java:271)
at retrofit.RestAdapter$RestHandler$1.call(RestAdapter.java:269)
at retrofit.RxSupport$2.run(RxSupport.java:46)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Any ideas to locate the subscription (line in code) that does not have the onError() ?
This can be a pain to debug. Funnily enough I had an error inside the Action1<Throwable> [facepalm].
The best was to debug this is to register a global error handler. This will saw you the truth in your code:
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
#Override
public void handleError(Throwable e) {
Log.w("Error",e);
}
});
A discussion on github: https://github.com/ReactiveX/RxJava/issues/2293
If the onError is not implemented, then RxJava throw a OnErrorNotImplementedException exception. It's seems that RxJava didn't succed to call the onError method ("Error occurred when trying to propagate error to Observer.onError")
You can try to register an error handler to find the root exception.
static {
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
#Override
public void handleError(Throwable e) {
e.printStackTrace();
}
});
}
Use this one:
RxJavaHooks.setOnError(throwable -> {
if (throwable != null && throwable.getMessage() != null) {
Log.e("Error", throwable.getMessage());
}
});
Instead of deprecated RxJavaPlugins.getInstance()