为什么会抛出java.lang.NullPointerException

时间:2015-03-28 15:54:33

标签: android

这是我的主要活动;

package com.example.sendsms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	public void findContact(View view ){
		
		Intent intent = new Intent(this,ReadContect.class);
		startActivity(intent);
		
	}
	
}

package com.example.sendsms;

import java.util.ArrayList;
import java.util.List;

import com.example.sendsms.domain.person;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class getContect{
		public static List<person>   getContectInfo(Context context){
			ContentResolver cr =  context.getContentResolver();
			List<person>  persons =  new ArrayList<person>();
			Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
			Uri uriData = Uri.parse("content://com.android.contacts/data");
			Cursor cursor = cr.query(uri, new String[] {"contact_id"}	, null, null, null);
			while(cursor.moveToNext()){
				String id = cursor.getString(0);
				if(id != null){
					Cursor  dataCursor = cr.query(uriData, new String[]{"data1","mimetype"}, "raw_contact_id=?", new String [] {id}, null);
					person per = new  person() ;
					while(dataCursor.moveToNext()){
						String data = dataCursor.getString(0);
						String mimetype =dataCursor.getString(1);
						if("vnd.android.cursor.item/name".equals(mimetype)){
							per.setName(data);
						}else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)){
							per.setNumber(data);
						}
						
					}
					dataCursor.close();
					persons.add(per);
				}
				
				
			}
			cursor.close();
			return persons;
		}
}
我发现这个Exception抛出ReadContect with debug, 这些人出了什么问题,它就会失效

package com.example.sendsms;

import java.util.List;

import com.example.sendsms.domain.person;
import com.example.sendsms.getContect;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ReadContect extends Activity {
	private ListView lv;
	private List<person> persons;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_read_contect);
		
		 lv = (ListView) findViewById(R.id.lv);
		
		lv.setAdapter(new ContectAdap());
		persons =  getContect.getContectInfo(this);
		
		
		 
		
		
	}
	private class ContectAdap extends BaseAdapter{
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return persons.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			person per =persons.get(arg0);
			View view = View.inflate(getApplicationContext(), R.layout.contect_view, null);
			TextView et_name = (TextView) findViewById(R.id.name);
			TextView et_number = (TextView) findViewById(R.id.number);
			
			et_name.setText(per.getName());
			et_name.setText(per.getNumber());
			return view;
		}
		
	}

	
}
这是我的catlog

03-28 11:30:52.467: W/dalvikvm(4455): threadid=1: thread exiting with uncaught exception (group=0xb1d8cb20)
03-28 11:30:54.407: E/AndroidRuntime(4455): FATAL EXCEPTION: main
03-28 11:30:54.407: E/AndroidRuntime(4455): Process: com.example.sendsms, PID: 4455
03-28 11:30:54.407: E/AndroidRuntime(4455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sendsms/com.example.sendsms.ReadContect}: java.lang.NullPointerException
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.os.Looper.loop(Looper.java:136)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.app.ActivityThread.main(ActivityThread.java:5017)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at java.lang.reflect.Method.invoke(Method.java:515)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at dalvik.system.NativeStart.main(Native Method)
03-28 11:30:54.407: E/AndroidRuntime(4455): Caused by: java.lang.NullPointerException
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at com.example.sendsms.ReadContect$ContectAdap.getCount(ReadContect.java:39)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.widget.ListView.setAdapter(ListView.java:480)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at com.example.sendsms.ReadContect.onCreate(ReadContect.java:26)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.app.Activity.performCreate(Activity.java:5231)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 11:30:54.407: E/AndroidRuntime(4455): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

也许我的表达不够清楚,谢谢你的耐心;

0 个答案:

没有答案