以下活动在我的模拟器中启动良好但显示错误消息我尝试在真正的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);
}
}
答案 0 :(得分:1)
在cursor
开始使用getsms()
之前,您尚未初始化NullPointerException
,这将引发{{1}}。
如果这在模拟器中工作,我会非常惊讶。