我想在一個數組中的聯繫人的名稱和他們在另一個數組中的類型,但無法通過與空指針exception.here是我的代碼。我已經指出了我得到空指針異常的行。請幫助..提前感謝。獲得空指針異常?
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();
}
}
}
}
}
請提供logcat輸出 – Vladimir
無視您的'name'和'phoneType'未初始化,所以它們爲空 – Vladimir
使您的列表視圖如下:lv =(ListView)findViewById(R.id.listview); – Hiral