简体   繁体   中英

Why i can't start activity in my android app?

If i do some test on Samsung Galaxy 2 this code works:

for(int i = 0; i < all_ids.size(); i++)
{
    if(i == position)
    {
        System.out.println("Rabotaet if");
        String topic = null;

        String message_id = null;
        Intent activity = new Intent(getApplicationContext(), MessageActivity.class);
        activity.putExtra(SESSION_ID, sess_id);
        activity.putExtra(DATE,date);
        activity.putExtra(TYPE,folder_type);
        activity.putExtra(TOPIC,all_subjects.get(i));
        activity.putExtra(MESSAGE_ID, all_ids.get(i));
        activity.putExtra(FROM, all_emails_address.get(i));
        startActivity(activity);
    }
}

But if I do this test on Samsung Tab 3, it's not working. And in debug I see this message:

04-14 12:27:10.760: W/System.err(24669): java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0

Any ideas?

After update i've code:

            for(int i = 0; i < all_ids.size(); i++)
            {
                if(i == position)
                {
                    System.out.println("Rabotaet if");
                    String topic = null;

                    String message_id = null;
                    Intent activity = new Intent(getApplicationContext(), MessageActivity.class);
                    activity.putExtra(SESSION_ID, sess_id);
                    activity.putExtra(DATE,date);
                    activity.putExtra(TYPE,folder_type);

                    if (i < all_ids.size())
                        activity.putExtra(MESSAGE_ID,all_ids.get(i));
                    if (i < all_subjects.size())
                        activity.putExtra(TOPIC,all_subjects.get(i));
                    if (i < all_emails_address.size())
                        activity.putExtra(FROM, all_emails_address.get(i));
                    startActivity(activity);    
                }   
            }

But now i've this error:

04-14 12:39:59.480: E/AndroidRuntime(26359): FATAL EXCEPTION: main
04-14 12:39:59.480: E/AndroidRuntime(26359): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gdes.earchive/com.gdes.earchive.MessageActivity}: java.lang.NullPointerException
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.app.ActivityThread.access$700(ActivityThread.java:154)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.os.Looper.loop(Looper.java:137)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.app.ActivityThread.main(ActivityThread.java:5306)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at java.lang.reflect.Method.invoke(Method.java:511)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at dalvik.system.NativeStart.main(Native Method)
04-14 12:39:59.480: E/AndroidRuntime(26359): Caused by: java.lang.NullPointerException
04-14 12:39:59.480: E/AndroidRuntime(26359):    at com.gdes.earchive.MessageActivity.onCreate(MessageActivity.java:37)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.app.Activity.performCreate(Activity.java:5255)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
04-14 12:39:59.480: E/AndroidRuntime(26359):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
04-14 12:39:59.480: E/AndroidRuntime(26359):    ... 11 more

Your loop is based on the size of all_ids . You're also using 2 other array-type of variables: all_subjects and all_emails_address . One of them has size of 0 (no elements).

You need to check their size before using them:

if (i < all_subjects.size())
    activity.putExtra(TOPIC,all_subjects.get(i));
if (i < all_emails_address.size())
    activity.putExtra(FROM, all_emails_address.get(i));

Seems you have no data for the index 4 for one (or both) lists:

all_subjects or/and all_emails_address

Please insert some further checking on all_subjects.size() and all_emails_address.size()

The error caught in the logcat is quite clear: the index provided and based on size list all_ids could not reachabele also for other lists.

So is a good practice checking as possible the index of a list before use it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM