2011-06-30 137 views
1

以下活動在我的模擬器中啓動得很好,但顯示了一條錯誤消息,我嘗試在真正的android phone上啓動它。我不知道問題出在哪裏。我不知道我怎麼能得到手機中的錯誤。下面是代碼:在模擬器上工作的代碼,但不在真實的android手機上

package com.messageHider; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.telephony.PhoneNumberUtils; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

public class sms extends Activity implements OnItemClickListener 
{ 
    ListView listViewSMS; 
    Button addMenuItem; 
    Uri smsUri=Uri.parse("content://sms"); 
    Uri contactUri=ContactsContract.Contacts.CONTENT_URI; 
    Uri phoneUri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    Cursor cursor; 
    SimpleCursorAdapter adapter; 
    public String menu_name; 
    private SharedPreferences prefs; 
    public static final String PREFERENCE_FILE="prefs"; 
    public long sms_id; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sms); 
     listViewSMS=(ListView)findViewById(R.id.listViewsms); 
     listViewSMS.setOnItemClickListener(this); 
    } 
    @Override 
    protected void onStart() { 
     super.onStart(); 
     getsms(); 
    } 
    private void getsms() 
    { 
     int count=cursor.getCount(); 
     String[]sender_number=new String[count]; 
     String[]sender_names=new String[count]; 
     String[]contact_id=new String[count]; 
     int counter=0; 
     while(cursor.moveToNext()) 
     { 
      sender_number[counter]=cursor.getString(cursor.getColumnIndex("address")); 
      counter++; 
     } 
     int mycounter=0; 
     for(int x=0;x<sender_number.length;x++) 
     { 
      Cursor mycursor=getContentResolver().query(phoneUri,null,ContactsContract.CommonDataKinds.Phone.NUMBER+"=?" ,new String[]{PhoneNumberUtils.formatNumber(sender_number[x])}, null); 
      while(mycursor.moveToNext()) 
      { 
       contact_id[mycounter]=mycursor.getString(mycursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 
       mycounter++; 
      } 
     } 
     int names_counter=0; 
     for(int i=0;i<contact_id.length;i++) 
     { 
      if(contact_id[i]!=null) 
      { 
      Cursor cursor_names=getContentResolver().query(contactUri, null, ContactsContract.Contacts._ID+"=?", new String[]{contact_id[i]}, null); 
      while(cursor_names.moveToNext()) 
      { 
       sender_names[names_counter]=cursor_names.getString(cursor_names.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       names_counter++; 
      } 
      } 
     } 
     //Populate the list with the array content 
     ArrayList<String>arrayList=new ArrayList<String>(); 
     for(int j=0;j<sender_names.length;j++) 
     { 
      if(!arrayList.contains(sender_names[j])) 
      { 
       arrayList.add(sender_names[j]); 
      } 
     } 
     ArrayAdapter<String> sendersArrayAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList); 
     listViewSMS.setAdapter(sendersArrayAdapter); 
    } 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { 
     String sender=((TextView)v).getText().toString(); 
     Toast.makeText(getApplicationContext(),sender,Toast.LENGTH_LONG).show(); 
     prefs=getSharedPreferences(PREFERENCE_FILE, 0); 
     SharedPreferences.Editor editor=prefs.edit(); 
     editor.putString("smsid", String.valueOf(id)); 
     editor.putString("smssender", sender); 
     editor.commit(); 
     Intent intent=new Intent(sms.this,contactsmses.class); 
     intent.putExtra("sender",sender); 
     startActivity(intent); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflator=getMenuInflater(); 
     inflator.inflate(R.menu.smsmenu, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()) 
     { 
     case R.id.itemAutoHide: 
      startActivity(new Intent(sms.this,autohide.class)); 
      break; 
     case R.id.itemHiddenMessages: 
      startActivity(new Intent(sms.this,hiddenMessages.class)); 
      break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 
+2

發佈手機的logcat請 – Egor

+0

您應該提供logcat跟蹤。沒有logcat就很難找出問題。 – Hasandroid

+1

你的代碼的一個顯而易見的問題是,你在UI線程上做了很多數據庫訪問,這是一個非常糟糕的主意,因爲它會讓你的應用程序無響應,甚至可能導致ANR。嘗試在AsyncTask或服務中執行數據庫內容。不過,我認爲這不是直接導致問題的原因。 –

回答

1

您還沒有初始化cursor你開始使用它在getsms()開始之前,這將拋出一個NullPointerException

如果這是在模擬器中工作,我會非常驚訝。

相關問題