我试图将一个数组中的联系人名称和他们的类型放在另一个数组中,但是无法通过空指针异常来完成.这是我的代码.我已经指出了我得到空指针异常的行.请帮忙..提前致谢.
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.@R_404_6818@VIEw;import androID.Widget.relativeLayout;public final class TestActivity extends Activity {String[] name;String[] phoneType;@R_404_6818@VIEw lv;@R_404_6818@VIEwAdapter lva; public static final String TAG = "ContactManager";@OverrIDepublic 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 = (@R_404_6818@VIEw)findVIEwByID(androID.R.ID.@R_404_6818@); lva = new @R_404_6818@VIEwAdapter(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[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" > <@R_404_6818@VIEw androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:ID="@+ID/@R_404_6818@vIEw" androID:cachecolorHint="#0000" /></linearLayout>
现在在你的TestActivity.class中:
public final class TestActivity extends Activity {String[] name;String[] phoneType;@R_404_6818@VIEw lv;String s[];public static final String TAG = "ContactManager";@OverrIDepublic voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.ID.main); testGetContacts(); lv = (@R_404_6818@VIEw)findVIEwByID(R.ID.@R_404_6818@vIEw); ArrayAdapter<String> sa=new ArrayAdapter<String>(context, androID.R.layout.simple_@R_404_6818@_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++;}
}
总结以上是内存溢出为你收集整理的android – 获取空指针异常?全部内容,希望文章能够帮你解决android – 获取空指针异常?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)