本文介绍了得到空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个数组中包含联系人的名称,在另一个数组中包含他们的类型,但无法通过空指针异常.这是我的代码.我已经指出了我得到空指针异常的那一行.请帮忙..提前致谢.

I am trying to have name of contacts in one array and their types in another array,but can't get through with null pointer exception.here is my code.I have pointed out the line where I am getting null pointer exception.please help..thanks in advance.

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 + "
");
              System.out.println(name + "
");
              System.out.println(phoneType + "
");


          }


        } catch (SQLException sqle) {
           //handling exception
        } finally {
         if (!cur.isClosed()) {
             cur.close();
         }
     }

        }

}
}

推荐答案

在使用之前初始化 String[] 名称.你可以这样做:

Initialize String[] name before using it.You can do that like:

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 + "
");
     //System.out.println(name + "
");
     //System.out.println(phoneType + "
");

     s=s+"id="+id+" name="+name[i]+" phoneType="+phoneType[i]+"
";
     i++;
}
Toast.makeText(getApplicationContext(),i+" - "+s).show();

在布局文件夹中创建一个 xml 文件.

Create an xml file in layout folder.

ma​​in.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>

现在在您的TestActivity.class:

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++;
}

}

这篇关于得到空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 17:39