Sign in to follow this  
Followers 0

[Android] array list issue.

11 posts in this topic

Posted

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);




	    }

	 }

}

Share this post


Link to post
Share on other sites

Posted

You never initialized sArray.

Share this post


Link to post
Share on other sites

Posted

You never initialized sArray.

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

it still comes up with the error

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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 :)

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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);

	 }

}

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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

1 person likes this

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.