Exception processing async thread queue java.lang.UnsupportedOperationException - android

I am having a problem when I create an array of strings, this only happens in 2.1 android api level 7 or lower and i need to install the application on a device with exactly this configuration, any idea how to solve the problem?
Below the source code, the message that pops up on screen and also logcat's message.
CODE:
private String[] fillPedidosName() {
TipoPedidoDAO tipoDAO = null;
try {
tipoDAO = new TipoPedidoDAO();
pedidosList = tipoDAO.selectAll();
String[] temp = new String[pedidosList.size()];
for (int i = 0; i < pedidosList.size(); i++) {
if (pedidosList.get(i) != null) {
temp[i] = pedidosList.get(i).getDescricao().toString();
}
}
return temp;
} catch (Exception ex) {
MyLoger.logar(ex);
return null;
} finally {
if (tipoDAO.getDB().isOpen()) {
tipoDAO.closeConnection();
}
}
}
THE MESSAGE THAT POPS UP DEBUGING:
Exception processing async thread queue
Exception processing async thread queue
java.lang.UnsupportedOperationException
lOGCAT'S MESSAGE:
03-03 17:57:57.124: ERROR/jdwp(1267): REQ: UNSUPPORTED (cmd=2/11 dataLen=8 id=0x0012ba)

You are probably not using a List that supports get(int).
Try changing your List implementation to ArrayList. When you create your list:
List myList = new ArrayList()
This is probably happening inside tipDAO.selectAll().

I had this problem. I got it fixed.
When working with Arrays of your Objects, make sure you have defined a constructor in the object file.
This piece of code was creating the error
List<Prediction> predictions = new ArrayList<Prediction>();
The fix.
Prediction class file was missing a constructor. After putting in a default constructor, the error is gone for me.
package com.thuptencho.torontotransitbus.models;
public class Prediction {
public String epochTime = "", seconds = "", minutes = "", isDeparture = "", affectedByLayover = "", branch = "",
dirTag = "", vehicle = "", block = "", tripTag = "";
//this constructor was missing..after coding this constructor. the error was gone.
public Prediction(){
super();
}
#Override
public String toString() {
return "epochTime:" + this.epochTime + " seconds:" + this.seconds + " minutes:" + this.minutes
+ " isDeparture:" + this.isDeparture + " affectedByLayover:" + this.affectedByLayover + " branch:"
+ this.branch + " dirTag:" + this.dirTag + " vehicle:" + this.vehicle + " block:" + this.block
+ " triptag:" + this.tripTag;
}
}

Related

In Realm `addchangelistener` know at which position element got inserted/updated/deleted?

In Realm addchangelistener can we know at which position list got changed, and does the element got inserted/updated/removed from the list ??
can we know at which position list got changed
Yes
private RealmResults<Obj> results;
private OrderedRealmCollectionChangeListener<RealmResults<Obj>> changeListener = new OrderedRealmCollectionChangeListener<RealmResults<Obj>>() {
#Override
public void onChange(RealmResults<Obj> results, OrderedCollectionChangeSet changeSet) {
String insertions = changeSet.getInsertions().length == 0 ? "" : "\n - Insertions: " + Arrays.toString(changeSet.getInsertions());
String deletions = changeSet.getDeletions().length == 0 ? "" : "\n - Deletions: " + Arrays.toString(changeSet.getDeletions());
String changes = changeSet.getChanges().length == 0 ? "" : "\n - Changes: " + Arrays.toString(changeSet.getChanges());
showStatus("Obj was loaded, or written to. " + insertions + deletions + changes);
}
};
public void ...() {
results = realm.where(Obj.class)...findAllAsync();
results.addChangeListener(changeListener);

How can I create Android logcat entries that provide a link to source code in Eclipse? [duplicate]

Is there any way to access automatically any Log in Logcat by a double click ?
Actually, when there is an error crashing my Android Application, I can double click on the line saying for instance
at com.myapp.mypackage$Class.function(File.java:117)
And by Double-clicking on this line, I am automatically redirected to the related line of my code.
But, when I try to generate the same line in another Log, example :
Log.e("TAG", "at com.myapp.mypackage$Class.function(File.java:117)");
The Double-Click doesn't work anymore ...
Any ideas ?
If you want to create a log in logcat that can be clicked and go to your line use the following method to create it:
Enjoy!
public static void showLogCat(String tag, String msg) {
StackTraceElement[] stackTraceElement = Thread.currentThread()
.getStackTrace();
int currentIndex = -1;
for (int i = 0; i < stackTraceElement.length; i++) {
if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
{
currentIndex = i + 1;
break;
}
}
String fullClassName = stackTraceElement[currentIndex].getClassName();
String className = fullClassName.substring(fullClassName
.lastIndexOf(".") + 1);
String methodName = stackTraceElement[currentIndex].getMethodName();
String lineNumber = String
.valueOf(stackTraceElement[currentIndex].getLineNumber());
Log.i(tag, msg);
Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
+ className + ".java:" + lineNumber + ")");
}
If you don't mind the clutter in your log, you can easily just add a new Exception() to the log message
Log.e("TAG", "Looky here see", new Exception());

How to insert a log in LogCat that when I click on it jumps to its line in code?

I want to insert a log in LogCat that when I click on it jumps to its line like some error logs that are generated by system.
Is it possible?
I found it:
public static void showLogCat(String tag, String msg) {
StackTraceElement[] stackTraceElement = Thread.currentThread()
.getStackTrace();
int currentIndex = -1;
for (int i = 0; i < stackTraceElement.length; i++) {
if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
{
currentIndex = i + 1;
break;
}
}
String fullClassName = stackTraceElement[currentIndex].getClassName();
String className = fullClassName.substring(fullClassName
.lastIndexOf(".") + 1);
String methodName = stackTraceElement[currentIndex].getMethodName();
String lineNumber = String
.valueOf(stackTraceElement[currentIndex].getLineNumber());
Log.i(tag, msg);
Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
+ className + ".java:" + lineNumber + ")");
}
Its usage:
showLogCat("tag", "message");
The important thing is to insert "(X:Y)" in your log message, while X is your desired file name and Y is your desired line number in X. (I learned it from #breceivemail's answer). So try:
public static void log(final String tag, final String msg) {
final StackTraceElement stackTrace = new Exception().getStackTrace()[1];
String fileName = stackTrace.getFileName();
if (fileName == null) fileName=""; // It is necessary if you want to use proguard obfuscation.
final String info = stackTrace.getMethodName() + " (" + fileName + ":"
+ stackTrace.getLineNumber() + ")";
Log.LEVEL(tag, info + ": " + msg);
}
Note: The LEVEL is the log level and can be v, d, i, w, e or wtf.
Now you can use log(tag, msg) instead of Log.LEVEL(tag, msg).
Example:
MainActivity.java:
...
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
log("Test Tag", "Hello World!");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
The output:
12-30 14:24:45.343 ? I/Test Tag: onCreate (MainActivity.java:10): Hello World!
And MainActivity.java:10 automatically would be a link and you can click on it!
You can also assign following value to info variable if you want more verbose log:
final String info = stackTrace.getClassName() + "." + stackTrace.getMethodName() + " ("
+ fileName + ":" + stackTrace.getLineNumber() + ")\n";
So the output of above example would be:
12-30 14:33:07.360 ? I/Test Tag: com.example.myapp.MainActivity.onCreate (MainActivity.java:11)
Hello World!
Please use this Tree with Timber.
class MyLinkingTimberTree : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String? {
return makeClickableLineNumber(element)
}
private fun makeClickableLineNumber(
element: StackTraceElement
): String {
val className = element.fileName
val methodName = element.methodName
val lineNumber = element.lineNumber
val fileName = element.fileName
val stringBuilder = StringBuilder(className)
.append(".")
.append(methodName)
.append(" (")
.append(fileName)
.append(":")
.append(lineNumber)
.append(") ")
return stringBuilder.toString()
}
}
And then just instantiate it like this:
class MyApplication: Application() {
override fun onCreate() {
super.onCreate()
if(BuildConfig.DEBUG) {
Timber.plant(MyLinkingTimberTree())
}
}
}
Then just use Timber normally:
Timber.d("Currently Signed in:")
And this is the result. Nice, isn't it? I hope you enjoy using it as much as I enjoyed making it! ;)
Yes you can do it .. Follow the example as answered on SO - logging
To answer the question in a simple way:
respecter cette règle :
{FileName}.{ext}:{LigneNumber}
e.g. MainActivity.java:10
which gives a sample as below
Log.d(TAG, "onResume: MainActivity.java:10");
I hope this will help you
This isn't exactly an answer to the question, but perhaps it's a "close enough" workaround.
Highlight the log text
Press CTRL-SHIFT-F
Double-Click on the search result.
If the text is highlighted before you press CTRL-SHIFT-F, then you don't need to type or copy/paste it in.
If your searches tend to produce too many results, you can use live templates to make unique logcat entries:
Create a live template to insert the Class, Method, and Line-Number (at the time of writing). I use "logi." Yes, the line number will become less and less accurate as you continue to write, but it can still function as a way to make your log entries more "findable."

Is there any way to access automatically any Log in Logcat by a double click?

Is there any way to access automatically any Log in Logcat by a double click ?
Actually, when there is an error crashing my Android Application, I can double click on the line saying for instance
at com.myapp.mypackage$Class.function(File.java:117)
And by Double-clicking on this line, I am automatically redirected to the related line of my code.
But, when I try to generate the same line in another Log, example :
Log.e("TAG", "at com.myapp.mypackage$Class.function(File.java:117)");
The Double-Click doesn't work anymore ...
Any ideas ?
If you want to create a log in logcat that can be clicked and go to your line use the following method to create it:
Enjoy!
public static void showLogCat(String tag, String msg) {
StackTraceElement[] stackTraceElement = Thread.currentThread()
.getStackTrace();
int currentIndex = -1;
for (int i = 0; i < stackTraceElement.length; i++) {
if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
{
currentIndex = i + 1;
break;
}
}
String fullClassName = stackTraceElement[currentIndex].getClassName();
String className = fullClassName.substring(fullClassName
.lastIndexOf(".") + 1);
String methodName = stackTraceElement[currentIndex].getMethodName();
String lineNumber = String
.valueOf(stackTraceElement[currentIndex].getLineNumber());
Log.i(tag, msg);
Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
+ className + ".java:" + lineNumber + ")");
}
If you don't mind the clutter in your log, you can easily just add a new Exception() to the log message
Log.e("TAG", "Looky here see", new Exception());

JSON data to android application

I got this part inside my json object. But I couldn't get this as a java List. It give error. So I tried to take it as a String Array. But it was same as before. So what should I use to parse this data to use with my android application ?
cuisine: {
-cuisine_names: [
"All (36)"
"Malaysian/ Singaporean (1)"
"Asian (1)"
"Australian (2)"
"Chinese (1)"
"European (3)"
"Spanish (1)"
"Greek (2)"
"Steak House (1)"
"Indian (1)"
"International (7)"
"Thai (1)"
"Italian (8)"
"Modern Australian (7)"
]
-price_ranges: [
"Any Price"
"$0-15"
"$15-30"
"$30+"
]
-times: [
"Any Time"
"05:30PM"
"06:00PM"
"06:30PM"
"07:00PM"
"07:30PM"
"08:00PM"
"08:30PM"
"09:00PM"
"09:30PM"
"10:00PM"
"10:30PM"
"11:00PM"
"11:30PM"
]
}
Thanks in advance !
To fill a list with a JSONObject, you should use a function like this (where NewsBSR is a custom object with some basic fields):
private ArrayList<NewsBSR> getListObjectsNews(JSONObject objNews)
{
ArrayList<NewsBSR> listNews = new ArrayList<NewsBSR>();
try{
for (Iterator iterator = objNews.keys(); iterator.hasNext();)
{
String cle = String.valueOf(iterator.next());
Object objet = objNews.get(String.valueOf(cle));
Log.v("V", "News: "+cle+ " : "+objet.toString());
if (cle.equals("results"))
{
JSONArray array = objNews.getJSONArray(cle);
for(int i = 0; i < array.length() ; i++)
{
Object obj = array.get(i);
Iterator it = ((JSONObject) obj).keys();
NewsBSR news = new NewsBSR();
while (it.hasNext())
{
String k = String.valueOf(it.next());
String val = ((JSONObject) obj).getString(k);
Log.v("V", "Array content : "+k+ " : "+val);
if (k.equals("tt") && val.length() > 0)
{
news.setTitle(val);
}
if (k.equals("dt") && val.length() > 0)
{
news.setDate(UtilsDate.stringToDate(val));
}
if (k.equals("num") && val.length() > 0)
{
news.setId(val);
}
}
listNews.add(news);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.v("V", "Error HOME: "+ e.getMessage());
e.printStackTrace();
}
return (listNews);
}
I think you have forgotten put commas. And you can use this
http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
The json format is not supposed to be like that.
first, there should be a root object surrounding the codes like this.
{ <---this
cuisine: {
-cuisine_names: [
"All (36)"........
} <-- and this
and then you need commas between every string in the array like so
"Australian (2)",
"Chinese (1)",
"European (3)",
"Spanish (1)",
"Greek (2)",
"Steak House (1)",
It's very simple with Gson:
public class Foo
{
static String jsonInput =
"{" +
"\"cuisine\": {" +
"\"cuisine_names\": [" +
"\"All (36)\"," +
"\"Malaysian/ Singaporean (1)\"," +
"\"Asian (1)\"," +
"\"Australian (2)\"," +
"\"Chinese (1)\"," +
"\"European (3)\"," +
"\"Spanish (1)\"," +
"\"Greek (2)\"," +
"\"Steak House (1)\"," +
"\"Indian (1)\"," +
"\"International (7)\"," +
"\"Thai (1)\"," +
"\"Italian (8)\"," +
"\"Modern Australian (7)\"" +
"]," +
"\"price_ranges\": [" +
"\"Any Price\"," +
"\"$0-15\"," +
"\"$15-30\"," +
"\"$30+\"" +
"]," +
"\"times\": [" +
"\"Any Time\"," +
"\"05:30PM\"," +
"\"06:00PM\"," +
"\"06:30PM\"," +
"\"07:00PM\"," +
"\"07:30PM\"," +
"\"08:00PM\"," +
"\"08:30PM\"," +
"\"09:00PM\"," +
"\"09:30PM\"," +
"\"10:00PM\"," +
"\"10:30PM\"," +
"\"11:00PM\"," +
"\"11:30PM\"" +
"]" +
"}" +
"}";
public static void main(String[] args)
{
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
CuisineContainer cc = gson.fromJson(jsonInput, CuisineContainer.class);
System.out.println(cc);
}
}
class CuisineContainer
{
private Cuisine cuisine;
#Override
public String toString()
{
return cuisine.toString();
}
}
class Cuisine
{
private String[] cuisine_names;
private String[] price_ranges;
private String[] times;
#Override
public String toString()
{
StringBuilder result = new StringBuilder();
result.append("cuisine_names: ");
result.append(Arrays.asList(cuisine_names));
result.append(System.getProperty("line.separator"));
result.append("price_ranges: ");
result.append(Arrays.asList(price_ranges));
result.append(System.getProperty("line.separator"));
result.append("times: ");
result.append(Arrays.asList(times));
return result.toString();
}
}
output:
cuisine_names: [All (36), Malaysian/ Singaporean (1), Asian (1), Australian (2), Chinese (1), European (3), Spanish (1), Greek (2), Steak House (1), Indian (1), International (7), Thai (1), Italian (8), Modern Australian (7)]
price_ranges: [Any Price, $0-15, $15-30, $30+]
times: [Any Time, 05:30PM, 06:00PM, 06:30PM, 07:00PM, 07:30PM, 08:00PM, 08:30PM, 09:00PM, 09:30PM, 10:00PM, 10:30PM, 11:00PM, 11:30PM]

Categories

Resources