Android 获取空指针异常?
我试图在一个数组中获取联系人的名称,在另一个数组中获取联系人的类型,但无法通过空指针异常。这是我的代码。我已经指出了获取空指针异常的行。请帮助..提前感谢Android 获取空指针异常?,android,nullpointerexception,Android,Nullpointerexception,我试图在一个数组中获取联系人的名称,在另一个数组中获取联系人的类型,但无法通过空指针异常。这是我的代码。我已经指出了获取空指针异常的行。请帮助..提前感谢 package application.test; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.database.SQLException; import
package application.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts.Data;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
public final class TestActivity extends Activity {
String[] name;
String[] phoneType;
ListView lv;
ListViewAdapter lva;
public static final String TAG = "ContactManager";
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout mainLayout=new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater layoutInflater = getLayoutInflater();
mainLayout.addView(layoutInflater.inflate(R.layout.main,null));
mainLayout.addView(layoutInflater.inflate(R.layout.extra,null));
this.addContentView(mainLayout, params);
lv = (ListView)findViewById(android.R.id.list);
lva = new ListViewAdapter(this,name,phoneType);
lv.setAdapter(lva);
testGetContacts();
}
private void testGetContacts() {
ContentResolver cr = getContentResolver();
String[] projection = new String[] { Data._ID,
ContactsContract.Contacts.DISPLAY_NAME, Phone.TYPE};
Cursor cur = cr.query(ContactsContract.Data.CONTENT_URI,
projection, null, null, null);
if (cur != null && cur.moveToFirst()) {
try {
int indexID = cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
int indexName = cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int indexPhoneType = cur.getColumnIndexOrThrow(Phone.TYPE);
while (cur.moveToNext()) {
int i=1;
String id = cur.getString(indexID);
//HERE LIES NULL POINTER EXCEPTION name[i] = cur.getString(indexName);
//HERE TOO phoneType[i] = cur.getString(indexPhoneType);
i++;
System.out.println(id + "\n");
System.out.println(name + "\n");
System.out.println(phoneType + "\n");
}
} catch (SQLException sqle) {
//handling exception
} finally {
if (!cur.isClosed()) {
cur.close();
}
}
}
}
}
您没有初始化字符串[]名称,因此当您尝试访问它时,会出现空指针异常。我建议使用更有意义的变量名。”“名称”非常模糊。您的姓名和电话阵列尚未初始化
String[] name = new String[EXPECTED_SIZE];
String[] phoneType = new String[EXPECTED_SIZE];
任何合适的IDE在运行之前都应该告诉您这一点。您使用的是Eclipse还是IntelliJ?你应该 使用前初始化
字符串[]
名称。
你可以这样做:
name=new String[cur.getCount()];
String s="";
while (cur.moveToNext()) {
int i=1;
String id = cur.getString(indexID);
name[i] = cur.getString(indexName);
phoneType[i] = cur.getString(indexPhoneType);
//System.out.println(id + "\n");
//System.out.println(name + "\n");
//System.out.println(phoneType + "\n");
s=s+"id="+id+" name="+name[i]+" phoneType="+phoneType[i]+"\n";
i++;
}
Toast.makeText(getApplicationContext(),i+" - "+s).show();
编辑:
在布局文件夹中创建xml文件
main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listview"
android:cacheColorHint="#0000"
/>
</LinearLayout>
现在在您的测试活动中。类:
public final class TestActivity extends Activity {
String[] name;
String[] phoneType;
ListView lv;
String s[];
public static final String TAG = "ContactManager";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.main);
testGetContacts();
lv = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> sa=new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}
private void testGetContacts() {
ContentResolver cr = getContentResolver();
String[] projection = new String[] { Data._ID,
ContactsContract.Contacts.DISPLAY_NAME, Phone.TYPE};
Cursor cur = cr.query(ContactsContract.Data.CONTENT_URI,
projection, null, null, null);
if (cur != null && cur.moveToFirst()) {
try {
int indexID = cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
int indexName = cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int indexPhoneType = cur.getColumnIndexOrThrow(Phone.TYPE);
name=new String[cur.getCount()];
s=new String[cur.getCount()];
while (cur.moveToNext()) {
int i=1;
String id = cur.getString(indexID);
name[i-1] = cur.getString(indexName);
phoneType[i-1] = cur.getString(indexPhoneType);
String temp="id="+id+"-name="+name[i-1]+"-phoneType="+phoneType[i-1];
s[i-1]=temp;
i++;
}
公共最终类测试活动扩展活动{
字符串[]名称;
字符串[]电话类型;
ListView lv;
字符串s[];
公共静态最终字符串标记=“ContactManager”;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.main);
testGetContacts();
lv=(ListView)findViewById(R.id.ListView);
ArrayAdapter sa=新的ArrayAdapter(上下文,android.R.layout.simple_list_item_1,s);
低压设置适配器(sa);
}
私有void testGetContacts(){
ContentResolver cr=getContentResolver();
字符串[]投影=新字符串[]{Data.\u ID,
Contacts contract.Contacts.DISPLAY_NAME,Phone.TYPE};
游标cur=cr.query(contacts contract.Data.CONTENT\u URI,
投影,空,空,空);
如果(cur!=null&&cur.moveToFirst()){
试一试{
int indexID=cur.getColumnIndexOrThrow(ContactsContract.Contacts.\u ID);
int indexName=cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY\u NAME);
int indexPhoneType=cur.getColumnIndexOrThrow(Phone.TYPE);
name=新字符串[cur.getCount()];
s=新字符串[cur.getCount()];
while(cur.moveToNext()){
int i=1;
字符串id=cur.getString(indexID);
名称[i-1]=当前getString(indexName);
phoneType[i-1]=cur.getString(indexPhoneType);
String temp=“id=“+id+”-name=“+name[i-1]+”-phoneType=“+phoneType[i-1]”;
s[i-1]=温度;
i++;
}
}由于您访问了错误的索引,所以得到了空指针 试试这个:
s=new String[cur.getCount()];
int i=1;
while (cur.moveToNext()) {
String id = cur.getString(indexID);
name[i-1] = cur.getString(indexName);
phoneType[i-1] = cur.getString(indexPhoneType);
String temp="id="+id+"-name="+name[i-1]+"-phoneType="+phoneType[i-1];
s[i-1]=temp;
i++;
}
还有lv=(ListView)findviewbyd(R.id.ListView);
而不是lv=(ListView)findviewbyd(android.R.id.list);
您应该在此处添加一个条件:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testGetContacts();
if(s.length()>0)
{
lv = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> sa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}
else
{
Toast.makeText(getApplicationContext(),"Nothing Found",Toast.LENGTH_SHORT).show();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testGetContacts();
如果(s.长度()>0)
{
lv=(ListView)findViewById(R.id.ListView);
ArrayAdapter sa=新的ArrayAdapter(getApplicationContext(),android.R.layout.simple\u list\u item\u 1,s);
低压设置适配器(sa);
}
其他的
{
Toast.makeText(getApplicationContext(),“找不到”,Toast.LENGTH_SHORT.show();
}
试试这个,告诉我它有帮助:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.listview);
testGetContacts();
ArrayAdapter<String> sa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}//method
private void testGetContacts() {
ContentResolver cr = getContentResolver();
String[] projection = new String[] { Data._ID,
ContactsContract.Contacts.DISPLAY_NAME, Phone.TYPE};
Cursor cur = cr.query(ContactsContract.Data.CONTENT_URI,
projection, null, null, null);
if (cur != null && cur.moveToFirst()) {
try {
int indexID = cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
int indexName = cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int indexPhoneType = cur.getColumnIndexOrThrow(Phone.TYPE);
name=new String[cur.getCount()];
phoneType=new String[cur.getCount()];
s=new String[cur.getCount()];
int i=1;
while (cur.moveToNext()) {
String id = cur.getString(indexID);
name[i] = cur.getString(indexName);
phoneType[i] = cur.getString(indexPhoneType);
String temp="id="+id+"-name="+name[i]+"-phoneType="+phoneType[i];
s[i-1]=temp;
i++;
}//while
ArrayAdapter<String> sa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}catch(Exception e){
e.printStackTrace();
}//catch
}//if
}//method
@覆盖
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(ListView)findViewById(R.id.ListView);
testGetContacts();
ArrayAdapter sa=新的ArrayAdapter(getApplicationContext(),android.R.layout.simple\u list\u item\u 1,s);
低压设置适配器(sa);
}//方法
私有void testGetContacts(){
ContentResolver cr=getContentResolver();
字符串[]投影=新字符串[]{Data.\u ID,
Contacts contract.Contacts.DISPLAY_NAME,Phone.TYPE};
游标cur=cr.query(contacts contract.Data.CONTENT\u URI,
投影,空,空,空);
如果(cur!=null&&cur.moveToFirst()){
试一试{
int indexID=cur.getColumnIndexOrThrow(ContactsContract.Contacts.\u ID);
int indexName=cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY\u NAME);
int indexPhoneType=cur.getColumnIndexOrThrow(Phone.TYPE);
name=新字符串[cur.getCount()];
phoneType=新字符串[cur.getCount()];
s=新字符串[cur.getCount()];
int i=1;
while(cur.moveToNext()){
字符串id=cur.getString(indexID);
name[i]=cur.getString(indexName);
phoneType[i]=cur.getString(indexPhoneType);
String temp=“id=“+id+”-name=“+name[i]+”-phoneType=“+phoneType[i]”;
s[i-1]=温度;
i++;
}//当
ArrayAdapter sa=新的ArrayAdapter(getApplicationContext(),android.R.layout.simple\u list\u item\u 1,s);
低压设置适配器(sa);
}捕获(例外e){
e、 printStackTrace();
}//抓住
}//如果
}//方法
忽略这一点,您的姓名
和电话类型
未初始化,因此它们为空。请将您的listview设置为:lv=(listview)findViewById(R.id.listview);嗯,程序已运行,但无法检索名称和类型..我得到的是空白屏幕。任何想法都是因为您没有在屏幕上显示内容。您将使用System.out.println(“”)看到此打印内容;在控制台中。尝试使用Toast或TextView在屏幕上查看这些内容。请查看答案中的编辑。我已在listview中显示了项。我尝试运行您的代码,但在(lv.setadapter(sa))处出现空指针异常…我关注这件事已经一周了,已经筋疲力尽了