• 0

[Android] array list issue.


Question

hello people! i really hope someone here can help me out a bit with this android code... I can get a list array working no problems but its when i introduce the loop and text messages that it becomes an issue ... I can get the text messages to appear no problem but not in this array list... any help?

it says one of the causes is a null pointer exception or something... but I dont see any issue ;( i am probs just blind ?


import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {


String[] sArray;


private ListView smsListView;

private ArrayAdapter arrayAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


smsListView = (ListView) findViewById(R.id.listview);
sms();

arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sArray);


smsListView.setAdapter(arrayAdapter);
}

private void sms() {
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);

int i = 0;
while (cur.moveToNext() && i < cur.getColumnCount()) {
i++;
sArray[i] = "From :" + cur.getString(2);



}
}
}
[/CODE]

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

You never initialized sArray.

even when i initialise it, String[] sArray = null;

it still comes up with the error

Link to comment
Share on other sites

  • 0

even when i initialise it, String[] sArray = null;

it still comes up with the error

That's not initializing anything.

String[] sArray = new String[100]; would initialize the array.

Link to comment
Share on other sites

  • 0

That's not initializing anything.

String[] sArray = new String[100]; would initialize the array.

yes i realised that shortly after and faced palmed my self... but it wont work with a count option i placed in ..

int cou = cur.getColumnCount();

String sArray[] = new String[cou];

any idea why ?? it works when i set it to a number but not when i set it to a varible ... this all works fine in actually java (the logic)

also yes I have moved it into an appropriate place for the count to work :)

Link to comment
Share on other sites

  • 0

What do you mean by "it won't work"? What's the error you're getting?

I was getting a arrayIndexOutOfBoundsException ... but I fixed it by adding 1 to the count, I dont know why this fixed it it just did lol probably something to do with the array starting at 0 and the count matching the while loop means it had one less array set then needed :) thanks for all the help! even if i did derp a little bit!

working code for those who want to see.


import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {





private ListView smsListView;

private ArrayAdapter arrayAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


smsListView = (ListView) findViewById(R.id.listview);
sms();


}

private void sms() {
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);

int i = 0;
int cou = cur.getCount() +1;
String sArray[] = new String[cou];

while (cur.moveToNext() && i < cur.getColumnCount()) {
i++;

sArray[i] = "From :" + cur.getString(2);



}
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sArray);

smsListView.setAdapter(arrayAdapter);
}
}
[/CODE]

Link to comment
Share on other sites

  • 0
I was getting a arrayIndexOutOfBoundsException ... but I fixed it by adding 1 to the count, I dont know why this fixed it it just did lol
Don't you run your code inside a debugger and doesn't it allow you to see exactly what's happening at the time the exception occurs? This looks like a typical off-by-one error and there's really no excuse to fix that by trial-and-error rather than understanding what's going on.
Link to comment
Share on other sites

  • 0

Don't you run your code inside a debugger and doesn't it allow you to see exactly what's happening at the time the exception occurs? This looks like a typical off-by-one error and there's really no excuse to fix that by trial-and-error rather than understanding what's going on.

yes, I have a debugger within my IDE and i do use it, I understand now what the problem was but I did not know what the problem was (specifically) untill i had fixed it, I understand why it was an off by 1, I rewrote the whole code the next day and made it much better then what you see above without having to add the 1.

in all honesty I normally run here when its like 4 in the morning and im desperate to get my work done before i sleep, as soon as i woke up the next day I redid my code and made it far far better. my brain just does not function well after a certain point but I cant sleep untill i get to a checkpoint in my work

Link to comment
Share on other sites

  • 0

in all honesty I normally run here when its like 4 in the morning and im desperate to get my work done before i sleep, as soon as i woke up the next day I redid my code and made it far far better. my brain just does not function well after a certain point but I cant sleep untill i get to a checkpoint in my work

Yeah, I wouldn't recommend doing that. My code quality tends to go something like this:

post-125341-0-05828100-1368483522.png

Link to comment
Share on other sites

  • 0

Yeah, I wouldn't recommend doing that. My code quality tends to go something like this:

post-125341-0-05828100-1368483522.png

i skip straight to 2pm

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.