gson.fromJson StackOverFlowError - java

I don't know why it doesn't work, I think I'm doing it correctly.
Error
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: FATAL EXCEPTION: main
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: Process: com.imaginarv.launcher, PID: 11957
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: java.lang.StackOverflowError
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
apps.json
This file is stored on assets/apps.json.
I checked it on http://jsonviewer.stack.hu/.
[
{
"name": "Carpeta 1",
"packages": [
"com.mobilendo.app1",
"com.mobilendo.app2"
]
},
{
"name": "Carpeta 2",
"packages": [
"com.mobilendo.app1",
"com.mobilendo.app2"
]
}
]
Functions
Functions to read the JSon from apps.json and parse it.
private ArrayList<FolderModel> getFolders() {
String jsonString = loadJSONFromAsset();
Gson gson = new Gson();
ArrayList<FolderModel> foldersList;
foldersList = gson.fromJson(jsonString, new TypeToken<ArrayList<FolderModel>>(){}.getType());
return foldersList;
}
public String loadJSONFromAsset() {
StringBuilder buf = new StringBuilder();
try {
InputStream json = getAssets().open("apps.json");
BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return buf.toString();
}
FolderModel
public class FolderModel {
#SerializedName("name")
private String name;
#SerializedName("packages")
private ArrayList<String> packages;
#SerializedName("apps")
private ArrayList<AppModel> apps;
#SerializedName("icon")
private Drawable icon;
public FolderModel(String name, ArrayList<String> packages) {
this.name = name;
this.packages = packages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getPackages() {
return packages;
}
public void setPackages(ArrayList<String> packages) {
this.packages = packages;
}
public ArrayList<AppModel> getApps() {
return apps;
}
public void setApps(ArrayList<AppModel> apps) {
this.apps = apps;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
}
AppModel
public class AppModel implements Serializable {
private final Context mContext;
private final ApplicationInfo mInfo;
private String mAppLabel;
private Drawable mIcon;
private boolean mMounted;
private final File mApkFile;
public AppModel(Context context, ApplicationInfo info) {
mContext = context;
mInfo = info;
mApkFile = new File(info.sourceDir);
}
public ApplicationInfo getAppInfo() {
return mInfo;
}
public String getApplicationPackageName() {
return getAppInfo().packageName;
}
public String getLabel() {
return mAppLabel;
}
public Drawable getIcon() {
if (mIcon == null) {
if (mApkFile.exists()) {
mIcon = mInfo.loadIcon(mContext.getPackageManager());
return mIcon;
} else {
mMounted = false;
}
} else if (!mMounted) {
// If the app wasn't mounted but is now mounted, reload
// its icon.
if (mApkFile.exists()) {
mMounted = true;
mIcon = mInfo.loadIcon(mContext.getPackageManager());
return mIcon;
}
} else {
return mIcon;
}
return mContext.getResources().getDrawable(android.R.drawable.sym_def_app_icon);
}
public void loadLabel(Context context) {
if (mAppLabel == null || !mMounted) {
if (!mApkFile.exists()) {
mMounted = false;
mAppLabel = mInfo.packageName;
} else {
mMounted = true;
CharSequence label = mInfo.loadLabel(context.getPackageManager());
mAppLabel = label != null ? label.toString() : mInfo.packageName;
}
}
}
}

Remove the Drawable object from FolderModel and from AppModel and save it as an id or image url.
The Drawables are not serializable clases and it's expensive to keep them in memory.

Try something like this:
JSONArray list;
list = new JSONArray(jsonString);
foldersList = new ArrayList<FolderModel>();
FolderModel f;
for (int i = 1; i < list.length(); ++i) {
f=new FolderModel();
JSONObject obj = list.getJSONObject(i);
f.setName(obj.getString("name").toString());
}

I usually use another way to parse:
FolderModel[] arrayModels = gson.fromJson(jsonString, FolderModel[].class);

Related

java.lang.NumberFormatException: Invalid int: "" model texture

I would like to add texture to my model but I keep getting this error . Any help would be appreciated. Thanks
for (String i : faces) {
for (String j : i.split(" ")) {
iCoords[faceIndex] = (short) faceIndex++;
String[] faceComponent = j.split("/");
String vertex = vertexes.get(Integer.parseInt(faceComponent[0]) - 1);
// this line throws NFE
String texture = textures.get(Integer.parseInt(faceComponent[1]) - 1);
String vertexComp[] = vertex.split(" ");
String textureComp[] = texture.split(" ");
for (String v : vertexComp) {
vCoords[vertexIndex++] = Float.parseFloat(v);
}
for (String t : textureComp) {
tCoords[textureIndex++] = Float.parseFloat(t);
}
}
}
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.app.glapp/com.app.glapp.MainActivity}:
java.lang.NumberFormatException: Invalid int: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:331)
at com.mingatronenterprices.glapp.mesh.(mesh.java:72)
at com.mingatronenterprices.glapp.ClearRenderer.(MainActivity.java:70)
at com.app.glapp.ClearGLSurfaceView.(MainActivity.java:54)
at com.app.glapp.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:5458)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Try to modify like this:
for (String v : vertexComp) {
try {
vCoords[vertexIndex++] = Float.parseFloat(v);
}
catch (NumberFormatException e) {
}
}
for (String t : textureComp) {
try {
tCoords[textureIndex++] = Float.parseFloat(t);
}
catch (NumberFormatException e) {
}
}

how to set a text in edit text field from JSON data

Here is the code I had used but when I enter the page, it will automatically stopped. I have used volley request object and it does not support facebook login also. I have used custom list view valley for web service .
public void reload()
{
//final AlertDialog alertDialog = new AlertDialog.Builder(Editprofile.this).create();
Log.d("url23",url23);
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url23,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
String TAG = null;
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
/* String fn= obj.getString("Fname");
String ln= obj.getString("Lname");
String em= obj.getString("email");
String ph= obj.getString("phnum");
String lk= obj.getString("live");
String wr= obj.getString("work");
String de= obj.getString("describe");
Log.d("OUTPUT IS",fn);*/
fname.setText(obj.getString("Fname"));
lname.setText(obj.getString("Lname"));
/* email.setText(em,TextView.BufferType.EDITABLE);
phone.setText(ph,TextView.BufferType.EDITABLE);
live.setText(lk,TextView.BufferType.EDITABLE);
work.setText(wr,TextView.BufferType.EDITABLE);
desc.setText(de,TextView.BufferType.EDITABLE);*/
// adding movie to movies array
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
//adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
String TAG="";
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
here is logcat error
10-29 08:21:34.953: D/(3631): [{"work":"software","id":76,"username":"admin123","phnum":"90000000000","image_src":"http:\/\/demo.cogzidel.com\/dropinn\/images\/no_avatar.jpg","email":"admin#gmail.com","Fname":"admin","describe":"developer","Lname":"admin123","live":"chennai"}]
10-29 08:21:34.953: D/OUTPUT IS(3631): admin
10-29 08:21:34.953: D/AndroidRuntime(3631): Shutting down VM
10-29 08:21:34.953: W/dalvikvm(3631): threadid=1: thread exiting with uncaught exception (group=0xb0d0cb20)
10-29 08:21:34.953: E/AndroidRuntime(3631): FATAL EXCEPTION: main
10-29 08:21:34.953: E/AndroidRuntime(3631): Process: info.androidhive.customlistviewvolley, PID: 3631
10-29 08:21:34.953: E/AndroidRuntime(3631): java.lang.NullPointerException
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.example.dropinnnow.Editprofile$6.onResponse(Editprofile.java:690)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.example.dropinnnow.Editprofile$6.onResponse(Editprofile.java:1)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
10-29 08:21:34.953: E/AndroidRuntime(3631): at android.os.Handler.handleCallback(Handler.java:733)
10-29 08:21:34.953: E/AndroidRuntime(3631): at android.os.Handler.dispatchMessage(Handler.java:95)
10-29 08:21:34.953: E/AndroidRuntime(3631): at android.os.Looper.loop(Looper.java:136)
10-29 08:21:34.953: E/AndroidRuntime(3631): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-29 08:21:34.953: E/AndroidRuntime(3631): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 08:21:34.953: E/AndroidRuntime(3631): at java.lang.reflect.Method.invoke(Method.java:515)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-29 08:21:34.953: E/AndroidRuntime(3631): at dalvik.system.NativeStart.main(Native Method)
Try this, hope it helps you,
fname.setText(obj.getString("Fname").toString());
lname.setText(obj.getString("Lname").toString());

HTML parsing JSoup img doesn't work

NEW LOGCAT
06-21 13:24:25.850: W/ActivityThread(25137): Application it.rs.esempioparsinglistview is waiting for the debugger on port 8100...
06-21 13:24:40.704: W/System.err(25137): java.net.MalformedURLException: Protocol not found: /portals/v3_2/images/background-images.jpg
06-21 13:24:40.714: W/System.err(25137): at java.net.URL.<init>(URL.java:176)
06-21 13:24:40.714: W/System.err(25137): at java.net.URL.<init>(URL.java:125)
06-21 13:24:40.724: W/System.err(25137): at it.rs.esempioparsinglistview.MainActivity$ParsingPaginaWeb.doInBackground(MainActivity.java:119)
06-21 13:24:40.724: W/System.err(25137): at it.rs.esempioparsinglistview.MainActivity$ParsingPaginaWeb.doInBackground(MainActivity.java:1)
06-21 13:24:40.724: W/System.err(25137): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-21 13:24:40.724: W/System.err(25137): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-21 13:24:40.734: W/System.err(25137): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-21 13:24:40.744: W/System.err(25137): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-21 13:24:40.744: W/System.err(25137): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-21 13:24:40.744: W/System.err(25137): at java.lang.Thread.run(Thread.java:841)
06-21 13:29:03.541: W/ActivityThread(25327): Application it.rs.esempioparsinglistview is waiting for the debugger on port 8100...
06-21 13:29:16.984: W/System.err(25327): java.net.MalformedURLException: Protocol not found: /portals/v3_2/images/background-images.jpg
06-21 13:29:17.004: W/System.err(25327): at java.net.URL.<init>(URL.java:176)
06-21 13:29:17.004: W/System.err(25327): at java.net.URL.<init>(URL.java:125)
06-21 13:29:17.014: W/System.err(25327): at it.rs.esempioparsinglistview.MainActivity$ParsingPaginaWeb.doInBackground(MainActivity.java:118)
06-21 13:29:17.014: W/System.err(25327): at it.rs.esempioparsinglistview.MainActivity$ParsingPaginaWeb.doInBackground(MainActivity.java:1)
06-21 13:29:17.014: W/System.err(25327): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-21 13:29:17.014: W/System.err(25327): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-21 13:29:17.024: W/System.err(25327): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-21 13:29:17.024: W/System.err(25327): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-21 13:29:17.034: W/System.err(25327): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-21 13:29:17.034: W/System.err(25327): at java.lang.Thread.run(Thread.java:841)
HTML code
<figure class="f-art-list">
<a href="/news/Cronaca/295507/news.aspx" id="MainContent_ListNews_rptNews_lnkToNews1_0" title="Ai domiciliari dopo atti incendiari nei confronti di imprenditore di Noci. Arrestati due santermani">
<img style="display: block;" data-original="http://cdn.livenetwork.it/news/295507/634903800413300428_arresto_carabinieri_HomeStandard_191x95.jpg" src="http://cdn.livenetwork.it/news/295507/634903800413300428_arresto_carabinieri_HomeStandard_191x95.jpg" class="lazy img-responsive" alt="Ai domiciliari dopo atti incendiari nei confronti di imprenditore di Noci. Arrestati due santermani" title="Ai domiciliari dopo atti incendiari nei confronti di imprenditore di Noci. Arrestati due santermani">
</a>
</figure>
Hi i have problem on parsing HTML JSoup, LogCat evidence problem on this part:
cache.immagineart.setImageBitmap(immaginiart.get(pos));
LogCat error messages
06-20 12:30:17.097: E/AndroidRuntime(15941): FATAL EXCEPTION: main
06-20 12:30:17.097: E/AndroidRuntime(15941): Process: it.rs.esempioparsinglistview, PID: 15941
06-20 12:30:17.097: E/AndroidRuntime(15941): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
06-20 12:30:17.097: E/AndroidRuntime(15941): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
06-20 12:30:17.097: E/AndroidRuntime(15941): at java.util.ArrayList.get(ArrayList.java:308)
06-20 12:30:17.097: E/AndroidRuntime(15941): at it.rs.esempioparsinglistview.ParsingArrayAdapter.getView(ParsingArrayAdapter.java:78)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.widget.AbsListView.obtainView(AbsListView.java:2295)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.widget.ListView.onMeasure(ListView.java:1175)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.View.measure(View.java:16521)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1231)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.View.measure(View.java:16521)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.View.measure(View.java:16521)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-20 12:30:17.097: E/AndroidRuntime(15941): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.View.measure(View.java:16521)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-20 12:30:17.097: E/AndroidRuntime(15941): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2553)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.View.measure(View.java:16521)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.Choreographer.doFrame(Choreographer.java:544)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.os.Handler.handleCallback(Handler.java:733)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.os.Handler.dispatchMessage(Handler.java:95)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.os.Looper.loop(Looper.java:136)
06-20 12:30:17.097: E/AndroidRuntime(15941): at android.app.ActivityThread.main(ActivityThread.java:5142)
06-20 12:30:17.097: E/AndroidRuntime(15941): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 12:30:17.097: E/AndroidRuntime(15941): at java.lang.reflect.Method.invoke(Method.java:515)
06-20 12:30:17.097: E/AndroidRuntime(15941): at dalvik.system.NativeStart.main(Native Method)
ParsingArrayAdapter.java:78 is this: cache.immagineart.setImageBitmap(immaginiart.get(pos));
This Part is for insert Bitmap in ArrayList for img
The URL parsing is: http://www.santeramolive.it/news/tutte-le-news.aspx
Mainactivity.java
public class MainActivity extends Activity {
ListView lista;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lista = (ListView)this.findViewById(R.id.main_lista);//list id
//asynctask
ParsingPaginaWeb parsing = new ParsingPaginaWeb();
parsing.execute("");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class ParsingPaginaWeb extends AsyncTask<String,String,String> {
ArrayList<String> titoli; //list title
ArrayList<String> descrizioni; //list description
ArrayList<String> date; // list date
ArrayList<String> categorie; // list category
ArrayList<Bitmap> immaginiart; // lista img
#Override
protected void onPreExecute()
{
titoli = new ArrayList<String>();
descrizioni = new ArrayList<String>();
date= new ArrayList<String>();
categorie= new ArrayList<String>();
immaginiart = new ArrayList<Bitmap>();
}
#Override
protected String doInBackground(String... params) {
Bitmap bitmap;
try {
Document doc = Jsoup.connect("http://www.santeramolive.it/news/tutte-le-news.aspx")
.userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22")
.timeout(30000).get();
Element listarticoli = doc.getElementsByClass("list-articles").first();
Elements articoli = listarticoli.getElementsByClass("art-list");
for(Element articolo : articoli)
{
// title
String titolo = articolo.getElementsByClass("t-art-list").first().text();
// description
String descrizione = articolo.getElementsByTag("p").first().text();
// insert in arraylist
titoli.add(titolo);
descrizioni.add(descrizione);
Elements dcs = articolo.getElementsByClass("date-tag-art-list");
for(Element dc : dcs)
{
String data = dc.getElementsByClass("date-art-list").first().text();
String categoria = dc.getElementsByClass("tag-art-list").first().text();
date.add(data); // add date
categorie.add(categoria);// add category
}
//img part
Elements imgdivs = articolo.getElementsByClass("f-art-list");
for(Element imgsdiv : imgdivs)
{
Element divimg = imgsdiv.getElementsByClass("lazy img-responsive").first();
String imgSrc = divimg.attr("src");
InputStream input = new java.net.URL(imgSrc).openStream();
bitmap = BitmapFactory.decodeStream(input);
immaginiart.add(bitmap);
}
}
} catch (Exception e) {
Log.e("ESEMPIO", "ERRORE NEL PARSING");
}
return null;
}
#Override
protected void onPostExecute(String result)
{
ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli, descrizioni, date, categorie, immaginiart);
lista.setAdapter(adapter);
}
}
}
ParsingArrayAdapter.java
public class ParsingArrayAdapter extends ArrayAdapter<String>{
//riferimenti statici alle risorse e agli id
private final static int LAYOUT = R.layout.riga_listview;
private final static int TITOLO = R.id.riga_listview_titolo;
private final static int DESCRIZIONE = R.id.riga_listview_descrizione;
private final static int DATA = R.id.riga_listview_data;
private final static int CATEGORIA = R.id.riga_listview_categoria;
private final static int IMMAGINE= R.id.riga_listview_immagine;
ArrayList<String> titoli;
ArrayList<String> descrizioni;
ArrayList<String> date;
ArrayList<String> categorie;
ArrayList<Bitmap> immaginiart;
Context c; //context
LayoutInflater inflater; //layout inflater
public ParsingArrayAdapter(Context context,ArrayList<String> titoli,ArrayList<String> descrizioni, ArrayList<String> date, ArrayList<String> categorie, ArrayList<Bitmap> immaginiart)
{
super(context,TITOLO);
this.c = context;
this.titoli = titoli;
this.descrizioni = descrizioni;
this.date= date;
this.categorie= categorie;
this.immaginiart= immaginiart;
this.inflater = LayoutInflater.from(c);
}
#Override
public int getCount()
{
return titoli.size();
}
#Override
public View getView(int pos,View view,ViewGroup parent)
{
CacheRiga cache; //cache
if(view==null)
{
// creo la view ma non l'attacco alla lista in quanto devo ancora modificare
// i testi delle textview
view = inflater.inflate(LAYOUT, parent,false);
cache = new CacheRiga();
cache.titolo = (TextView) view.findViewById(TITOLO);
cache.descrizione = (TextView) view.findViewById(DESCRIZIONE);//collego descrizione
cache.data = (TextView) view.findViewById(DATA);
cache.categoria = (TextView) view.findViewById(CATEGORIA);
cache.immagineart = (ImageView) view.findViewById(IMMAGINE);
view.setTag(cache);
}
else
{
cache = (CacheRiga) view.getTag(); dalla view
}
cache.titolo.setText(titoli.get(pos));
cache.descrizione.setText(descrizioni.get(pos));
cache.data.setText(date.get(pos));
cache.categoria.setText(categorie.get(pos));
cache.immagineart.setImageBitmap(immaginiart.get(pos));
return view;
}
private class CacheRiga {
public TextView titolo;
public TextView descrizione;
public TextView data;
public TextView categoria;
public ImageView immagineart;
}
}
Your problem is in
String imgSrc = divimg.attr("src");
because divimg== null. The stacktrace is clearly showing this. Further the app crashes. Why didn't you tell? Why didn't you post more of the logcat/stacktrace? You would have been helped sooner.
Change your code to:
Element divimg = imgsdiv.getElementsByClass("lazy img-responsive").first();
if ( divimg==null)
{
Log.e(TAG, "divimg==null: " + titolo);
immaginiart.add(null);
}
else
{
String imgSrc = divimg.attr("src");
InputStream input = new java.net.URL(imgSrc).openStream();
bitmap = BitmapFactory.decodeStream(input);
immaginiart.add(bitmap);
}
This will not decode the image but just display an empty image.

Fetching Sms Conversation in android

I am developing an android application in which I am displaying conversation List.
My code works fine in Samsung Galaxy S,Nexus one and Galaxy Nexus but the query is giving a problem in my Galaxy Grand phone.
Here is my code:
public ArrayList<String> getSMS()
{
ArrayList<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/");
Cursor cursor = getContentResolver().query(uriSMSURI, null, null, null, "date desc");
//cursor.moveToFirst();
while (cursor.moveToNext())
{
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
String read = cursor.getString(cursor.getColumnIndexOrThrow("read"));
//to fetch the contact name of the conversation
String contactName = address;
Uri Nameuri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(Nameuri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
if(cs.getCount()>0)
{
cs.moveToFirst();
contactName = cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
sms.add(contactName + "\n"+body);
}
return sms;
}
The following is the stackTrace:
06-24 17:26:21.744: E/AndroidRuntime(12764): FATAL EXCEPTION: main
06-24 17:26:21.744: E/AndroidRuntime(12764): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ssn.sms/com.ssn.sms.test}: java.lang.NullPointerException
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Looper.loop(Looper.java:137)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.main(ActivityThread.java:4935)
06-24 17:26:21.744: E/AndroidRuntime(12764): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 17:26:21.744: E/AndroidRuntime(12764): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-24 17:26:21.744: E/AndroidRuntime(12764): at dalvik.system.NativeStart.main(Native Method)
06-24 17:26:21.744: E/AndroidRuntime(12764): Caused by: java.lang.NullPointerException
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Parcel.readException(Parcel.java:1431)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentResolver.query(ContentResolver.java:372)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentResolver.query(ContentResolver.java:315)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.ssn.sms.test.getSMS(test.java:157)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.ssn.sms.test.onCreate(test.java:74)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.Activity.performCreate(Activity.java:5206)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-24 17:26:21.744: E/AndroidRuntime(12764): ... 11 more
I also referred to the below Link and used it,but still it is the same
SGS-3 bug related to SMS conversations list?
try this i have use function all sms return in json string
public JSONObject getsms()
{
JSONObject result = null;
JSONArray jarray = null;
String link[] = {"content://sms/inbox","content://sms/sent","content://sms/draft"};
try {
jarray = new JSONArray();
result = new JSONObject();
Uri uri = Uri.parse("content://sms/");
Cursor c= act.getContentResolver().query(uri, null, null ,null,null);
act.startManagingCursor(c);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
result.put("body",c.getString(c.getColumnIndexOrThrow("body")).toString());
result.put("date",c.getString(c.getColumnIndexOrThrow("date")).toString());
result.put("read",c.getString(c.getColumnIndexOrThrow("read")).toString());
result.put("type",c.getString(c.getColumnIndexOrThrow("type")).toString());
if((c.getString(c.getColumnIndexOrThrow("type")).toString()).equals("3"))
{
//Cursor cur= getContentResolver().query("", null, null ,null,null);
//startManagingCursor(cur);
String threadid = c.getString(c.getColumnIndexOrThrow("thread_id")).toString();
Cursor cur= act.getContentResolver().query(Uri.parse("content://mms-sms/conversations?simple=true"), null, "_id ="+threadid ,null,null);
act.startManagingCursor(cur);
if(cur.moveToFirst())
{
String recipientId = cur.getString(cur.getColumnIndexOrThrow("recipient_ids")).toString();
cur= act.getContentResolver().query(Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + recipientId, null, null);
act.startManagingCursor(cur);
if(cur.moveToFirst())
{
String address = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
result.put("address",address);
cur.close();
}
}
}else
{
result.put("address",c.getString(c.getColumnIndexOrThrow("address")).toString());
}
jarray.put(result);
result = new JSONObject();
c.moveToNext();
}
}
c.close();
result.put("smslist", jarray);
//result = new JSONObject(jarray.toString());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

Not able to get Text-to-Speech work from an non-activity class

I tried to use Text-to-Speech but getting an nullpointerexception.I have many textviews with text init.what i tried to do is that when user clicks on this textviews than text-to-speech speaks up the text which is on that particular textview.Following is the code of 2 of my classes which handles text-to-speech.I have also marked down the lines where i'm getting the nullpointerexception.Please tell me what am i doing wrong?! Thank you.
My TextSpeaker class:
public class TextSpeaker implements OnInitListener{
TextToSpeech tts;
public TextSpeaker(Context c,OnInitListener listener){
tts = new TextToSpeech(c,listener); //Getting NullPointerException here
}
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
}
My TextViewClick class:
public class TextViewClick implements OnClickListener{
public String textHolder;
Context c;
TextToSpeech.OnInitListener listener;
TextSpeaker tts1 = new TextSpeaker(c,listener); //Getting NullPointerException here
#Override
public void onClick(View v) {
TextView tv=(TextView) v;
tv.setTextColor(R.color.red);
textHolder = (String) tv.getText();
System.out.println(textHolder);
tts1.tts.speak(textHolder, TextToSpeech.QUEUE_FLUSH, null);
}
}
My logcat messages:
01-18 13:59:20.017: E/AndroidRuntime(323): FATAL EXCEPTION: main
01-18 13:59:20.017: E/AndroidRuntime(323): java.lang.NullPointerException
01-18 13:59:20.017: E/AndroidRuntime(323): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:407)
01-18 13:59:20.017: E/AndroidRuntime(323): at blah.blah.com.TextSpeaker.<init>(TextSpeaker.java:13)
01-18 13:59:20.017: E/AndroidRuntime(323): at independent.vervecoders.com.TextViewClick.<init>(TextViewClick.java:13)
01-18 13:59:20.017: E/AndroidRuntime(323): at independent.vervecoders.com.Independent.onClick(Independent.java:86)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.view.View.performClick(View.java:2408)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.view.View$PerformClick.run(View.java:8816)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Handler.handleCallback(Handler.java:587)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Looper.loop(Looper.java:123)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-18 13:59:20.017: E/AndroidRuntime(323): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 13:59:20.017: E/AndroidRuntime(323): at java.lang.reflect.Method.invoke(Method.java:521)
01-18 13:59:20.017: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-18 13:59:20.017: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-18 13:59:20.017: E/AndroidRuntime(323): at dalvik.system.NativeStart.main(Native Method)
It appears that your Context is never initialized. Make sure that you initialize the context before you call the constructor for TextSpeaker.
You also don't need to pass an onInitListener, your TextSpeaker is already an OnInitListener. You can try this in your TextViewClick:
public class TextViewClick implements OnClickListener
{
public String textHolder;
public TextViewClick(Context c)
{
TextSpeaker tts1 = new TextSpeaker(c);
}
#Override
public void onClick(View v)
{
TextView tv=(TextView) v;
tv.setTextColor(R.color.red);
textHolder = (String) tv.getText();
System.out.println(textHolder);
tts1.tts.speak(textHolder, TextToSpeech.QUEUE_FLUSH, null);
}
}
And your TextSpeaker would change as well:
public class TextSpeaker implements OnInitListener
{
TextToSpeech tts;
public TextSpeaker(Context c)
{
tts = new TextToSpeech(c, this);
}
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
}
In addition, make sure you have enabled TTS in your device settings.

Categories

Resources