RuntimeException:无法启动活动(Android)

时间:2012-12-07 23:58:35

标签: android runtimeexception start-activity

我正在处理一个应用程序并且遇到了一个奇怪的错误。到目前为止,用于测试应用程序的三个设备中有两个在点击登录按钮并启动新活动时工作正常。这两款手机都运行在Ice Cream Sandwhich上,第三款手机(给我带来麻烦的手机)在JellyBean上运行。

当使用JellyBean手机点击登录按钮时,会发生这种情况。

Java(注册页面)

           // 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>

崩溃报告

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

有人能指出我在正确的方向吗?我已经用尽所有选择,需要别人对这个问题的看法。

谢谢。

3 个答案:

答案 0 :(得分:1)

在代码中的某处,您正在从插槽1请求一个光标项,认为它是第一个插槽。不正确。 Cusors是0索引的,因此0是第一个槽。尝试将起始索引更改为0。

编辑示例:

Cursor cur = null; // Your query not null

if (cur != null && cur.moveToFirst()) {
    do {
        // Inflate your data
    } while (cur.moveToNext());
}
cur.close()

答案 1 :(得分:0)

不属于这一部分。它实际上是你的onCreate方法,在第82行。如果你去那里并按照Aedon的建议将你的1改为0,那么一切都会有效。

答案 2 :(得分:0)

当有人在收件箱中使用少于3条短信的应用时,问题就出现了。通过将收件箱光标移动到没有文本消息的位置,应用程序崩溃,因为该点不存在。