The following activity launches well in my emulator but shows an error message wen I try and launch it on a real android phone.I dont know what the problem is. I dont know how i can get the coz the error is in the phone. Here is the code:
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.c开发者_运维技巧lass));
break;
case R.id.itemHiddenMessages:
startActivity(new Intent(sms.this,hiddenMessages.class));
break;
}
return super.onOptionsItemSelected(item);
}
}
You are not initialising cursor
before you start using it at the beginning of getsms()
, and this will throw a NullPointerException
.
I would be extremely surprised if this was working in the emulator.
精彩评论