Reputation: 1082
I am working on an app and am getting a strange error. Two out of the three devices that have been used so far to test the app work perfectly when the log in button is tapped and a new activity is started. The two phones that work are both running on Ice Cream Sandwhich and the third phone (the one that is giving me trouble) is running on JellyBean.
When the log in button is tapped using the JellyBean phone this is what happens.
Java (Register Page)
// Register button
TextView main_body_register_button = new TextView(this);
main_body_register_button.setBackgroundResource(R.drawable.register_btn);
main_body_register_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Post to the MySQL database
class sendtask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... url) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://website.com/register.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("number", ""+main_body_phone_input.getText().toString()));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
protected void onPostExecute(String result) {
if (main_body_phone_input.getText().toString().compareTo("012-345-6789")==0) {
// Phone number: Empty
Intent base_account_register = new Intent(base_account_register.this,base_account_register.class);
startActivity(base_account_register);
} else if (main_body_phone_input.getText().toString().compareTo("012-345-6789")!=0) {
// Switch to main page
Intent TellATextActivity = new Intent(base_account_register.this,TellATextActivity.class);
ownAddress = ""+main_body_phone_input.getText().toString();
startActivity(TellATextActivity);
}
}
}
new sendtask().execute("Registration form");
// End
}
});
// END
Java (OnCreate TellATextActivity)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Global variables
Bundle bundle = getIntent().getExtras();
final String ownAddress = bundle.getString("session_number");
// MAIN
TextView main_message = new TextView(this);
// Select and parse the most recent message
Uri uriSMS = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(uriSMS, null, null, null, null);
// EDIT IN
if (cur != null && cur.moveToFirst()) {
do {
// Inflate your data
} while (cur.moveToNext());
}
cur.close()
// END
// EDIT OUT
cur.moveToNext();
cur.moveToNext();
// END
String body = cur.getString(cur.getColumnIndex("body"));
String add = cur.getString(cur.getColumnIndex("address"));
reply_address = "" +add;
String time = cur.getString(cur.getColumnIndex("date"));
String protocol = cur.getString(cur.getColumnIndex("protocol"));
String contactName = "";
Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(add));
Cursor c = getContentResolver().query(personUri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null );
if( c.moveToFirst() ) {
int nameIndex = c.getColumnIndex(PhoneLookup.DISPLAY_NAME);
contactName = c.getString(nameIndex);
}
c.close();
cur.close();
String out = "";
Date d = new Date(Long.valueOf(time));
// End
// Determine status of message
if (protocol == null)
out = ""+body;
else
out = ""+body;
// End
// Create output
main_message.setHeight(400);
main_message.setWidth(510);
main_message.setPadding(10,15,10,0);
main_message.setTextSize(23);
main_message.setTextColor(Color.rgb(100,100,100));
main_message.setText(out);
// End
// END
TextView bottom_reply = new TextView(this);
bottom_reply.setBackgroundResource(R.drawable.reply);
bottom_reply.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Compose a reply
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" +reply_address));
intent.putExtra("compose_mode", true);
startActivity(intent);
}
});
// END
}
AndroidManifest.xml
<activity
android:name=".TellATextActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android-dir/mms-sms" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Crash Report
java.lang.RuntimeException: Unable to start activity
ComponentInfo{tellatext.sms.app/tellatext.sms.app.TellATextActivity}:
android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4697)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:407)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at tellatext.sms.app.TellATextActivity.onCreate(TellATextActivity.java:82)
at android.app.Activity.performCreate(Activity.java:4539)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2013)
... 11 more
Would anyone be able to point me in the right direction here? I have exhausted all of my options and need someone else's perspective on the issue.
Thank you.
Upvotes: 0
Views: 859
Reputation: 1082
The problem was occuring when someone was using the app with less than 3 text messages in their inbox. By moving the inbox cursor to a spot without a text message the app as crashing because the spot didn't exist.
Upvotes: 0
Reputation: 11439
Somewhere in your code you are requesting a cursor item from slot 1 thinking that it is the first slot. Incorrect. Cusors are 0 indexed and as such, 0 is the first slot. Try changing your start index to 0.
Edit example:
Cursor cur = null; // Your query not null
if (cur != null && cur.moveToFirst()) {
do {
// Inflate your data
} while (cur.moveToNext());
}
cur.close()
Upvotes: 1
Reputation: 3453
It isn't in this part. It's actually your onCreate method, at line 82. If you go there and change your 1 to a 0 as suggested by Aedon, then all will work.
Upvotes: 0