获得空指针异常? [英] getting null pointer exception?

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

问题描述

我想在一个阵列的联系人和他们在另一个数组类型的名称,但不能获得通过与空指针exception.here我的codeI人士指出,在那里我得到空指针行exception.please help..thanks提前。

 包application.test;
进口android.app.Activity;
进口android.content.ContentResolver;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.os.Bundle;
进口android.provider.ContactsContract;
进口android.provider.ContactsContract.CommonDataKinds.Phone;
进口android.provider.ContactsContract.Contacts.Data;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.ViewGroup.LayoutParams;
进口android.widget.LinearLayout;
进口android.widget.ListView;
进口android.widget.RelativeLayout;

公共final类TestActivity延伸活动{
的String []的名称;
的String [] phoneType;
ListView的LV;
ListViewAdapter LVA;


    公共静态最终字符串变量=的ContactManager;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
    Log.v(TAG,活动状态:的onCreate());
    super.onCreate(savedInstanceState);
    的LayoutParams PARAMS =新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    的LinearLayout mainLayout =新的LinearLayout(本);
    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 =新ListViewAdapter(本,姓名,phoneType);
    lv.setAdapter(LVA);
    testGetContacts();
}


私人无效testGetContacts(){

        ContentResolver的CR = getContentResolver();

        的String []投影=新的String [] {Data._ID,
                ContactsContract.Contacts.DISPLAY_NAME,Phone.TYPE};

        光标CUR = cr.query(ContactsContract.Data.CONTENT_URI,
                投影,NULL,NULL,NULL);


        如果(CUR = NULL和放大器;!&安培; cur.moveToFirst()){

        尝试 {

            INT IndexID为= cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
            INT INDEXNAME = cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
             INT indexPhoneType = cur.getColumnIndexOrThrow(Phone.TYPE);

          而(cur.moveToNext()){
             INT I = 1;
              字符串ID = cur.getString(IndexID为);
 //这里躺着空指针异常名[我] = cur.getString(INDEXNAME);
 //这里也phoneType [I] = cur.getString(indexPhoneType);

             我++;


              的System.out.println(ID +\ N);
              的System.out.println(姓名+\ N);
              的System.out.println(phoneType +\ N);


          }


        }赶上(的SQLException SQLE){
           //处理异常
        } 最后 {
         如果(!cur.isClosed()){
             cur.close();
         }
     }

        }

}
}
 

解决方案

初始化的String [] 在使用它之前的名字。 你可以这样做:

 名称=新的String [cur.getCount()];
字符串s =;
而(cur.moveToNext()){

     INT I = 1;
     字符串ID = cur.getString(IndexID为);
     名称[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 =+编号+NAME =+名字[我] +phoneType =+ phoneType [我] +\ N的;
     我++;
}
Toast.makeText(getApplicationContext(),I + - + S).show();
 

编辑:

创建布局文件夹中的XML文件。

的main.xml

 <的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT机器人:方向=垂直>

    <的ListView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:ID =@ + ID /列表视图
        机器人:cacheColorHint =#0000
        />
< / LinearLayout中>
 

现在在 TestActivity.class

 公开最后一类TestActivity延伸活动{

的String []的名称;
的String [] phoneType;
ListView的LV;
字符串s [];
公共静态最终字符串变量=的ContactManager;

@覆盖
公共无效的onCreate(包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);
    lv.setAdapter(SA);
}


私人无效testGetContacts(){

    ContentResolver的CR = getContentResolver();
    的String []投影=新的String [] {Data._ID,
                ContactsContract.Contacts.DISPLAY_NAME,Phone.TYPE};
    光标CUR = cr.query(ContactsContract.Data.CONTENT_URI,
                投影,NULL,NULL,NULL);

    如果(CUR = NULL和放大器;!&安培; cur.moveToFirst()){

        尝试 {

            INT IndexID为= cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
            INT INDEXNAME = cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
             INT indexPhoneType = cur.getColumnIndexOrThrow(Phone.TYPE);

          名称=新的String [cur.getCount()];
          S =新的String [cur.getCount()];

          而(cur.moveToNext()){

               INT I = 1;
               字符串ID = cur.getString(IndexID为);
               命名[I-1] = cur.getString(INDEXNAME);
               phoneType [I-1] = cur.getString(indexPhoneType);


              串临时=ID =+ ID + - 名称=+名称[I-1] + -  phoneType =+ phoneType [I-1];
              S [I-1] =温度;
              我++;
}
 

}

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


          }


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

        }

}
}

解决方案

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 + "\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();

Edit :

Create an xml file in layout folder.

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>

now in your 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++;
}    

}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆