2012-12-07 101 views
0

我正在開發一個應用程序,並且遇到了一個奇怪的錯誤。當點擊登錄按鈕並啓動新的活動時,迄今爲止用於測試應用程序的三個設備中有兩個完美地工作。這兩款工作的電話都在冰淇淋Sandwhich上運行,第三部電話(給我麻煩的電話)在JellyBean上運行。RuntimeException:無法啓動活動(Android)

當使用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 

會有人能指出我在這裏正確的方向?我已經用盡了所有的選擇,需要別人對此問題的看法。

謝謝。

+0

我不想惹惱,但嘗試定義類不進入偵聽器:)(它不是JavaScript)。開始時你有沒有崩潰?你嘗試調試嗎? –

+1

請發佈代碼的方法onCreateTellATextActivity。並指出哪一行是82行。 – yDelouis

+0

請在「JAVA(OnCreate TellATextActivity)」下查看我的OnCreate方法。我剛剛發佈了它。任何人都可以解釋爲什麼我得到這個錯誤? – localhost

回答

0

問題發生在有人在收件箱中使用少於3條短信的應用程序時。通過將收件箱光標移動到沒有文字信息的位置,應用程序會因爲該位置不存在而崩潰。

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() 
+0

你介意看看我的修訂版嗎?我發佈了OnCreate方法。我會在哪裏對遊標索引進行更改? – localhost

+0

我不能肯定地說,但它可能是你調用'cur.moveToFirst()'兩次的部分。你的'onCreate(Bundle)'方法中的第13行和第14行。 – AedonEtLIRA

+0

如果這是問題,你會有解決方案嗎? – localhost

0

這是不是在這部分。這實際上就是你的onCreate方法,在第82行。如果你按照Aedon的建議去到那裏並將你的1改爲0,那麼所有的都可以工作。

相關問題