2011-06-30 61 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

如果这是在模拟器中工作,我会非常惊讶。

相关问题