为什么我无法在我的Android应用程序中启动活动?

时间:2014-04-14 10:29:37

标签: java android

如果我对三星Galaxy 2进行一些测试,则此代码可以正常运行:

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

但如果我在三星Tab 3上进行此测试,它就无法正常工作。在调试中我看到了这条消息:

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

有什么想法吗?

更新后我有代码:

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

但现在我犯了这个错误:

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

2 个答案:

答案 0 :(得分:2)

您的循环基于all_ids的大小。您还使用了其他2个数组类型的变量:all_subjectsall_emails_address。其中一个的大小为0(无元素)。

在使用之前,您需要检查它们的尺寸:

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

答案 1 :(得分:0)

对于一个(或两个)列表,您似乎没有索引4的数据:

all_subjects 或/和 all_emails_address

请在 all_subjects.size() all_emails_address.size()

上插入一些进一步检查

logcat中捕获的错误非常明确:提供的索引和基于大小列表的all_ids也无法为其他列表提供。

这是一个很好的做法,在使用它之前尽可能检查列表的索引