当我添加行list.add(sb.toString());
时,程序停止工作
当我删除它时,数据加载到StringBuilder,但我无法显示它
我搜索了一个解决方案,但每个代码示例都是相同的
我无法找到问题。
public ArrayList<String> list;
public ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
listView = (ListView) findViewById(R.id.contactList);
// listView.setOnItemClickListener(this);
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
StringBuilder sb = new StringBuilder();
sb.append(name).append(" ").append(phoneNumber);
list.add(sb.toString());
Log.d("Added:", sb.toString());
}
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
phones.close();
堆栈跟踪:
02-05 10:41:03.505: E/AndroidRuntime(29762): FATAL EXCEPTION: main
02-05 10:41:03.505: E/AndroidRuntime(29762): java.lang.NullPointerException
02-05 10:41:03.505: E/AndroidRuntime(29762): at com.example.anewapp.CameraPreview.surfaceCreated(CameraPreview.java:30)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.SurfaceView.updateWindow(SurfaceView.java:547)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.SurfaceView.access$000(SurfaceView.java:85)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:173)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1821)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:999)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4217)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.Choreographer.doFrame(Choreographer.java:525)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.os.Handler.handleCallback(Handler.java:615)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.os.Looper.loop(Looper.java:137)
02-05 10:41:03.505: E/AndroidRuntime(29762): at android.app.ActivityThread.main(ActivityThread.java:4802)
02-05 10:41:03.505: E/AndroidRuntime(29762): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 10:41:03.505: E/AndroidRuntime(29762): at java.lang.reflect.Method.invoke(Method.java:511)
02-05 10:41:03.505: E/AndroidRuntime(29762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
02-05 10:41:03.505: E/AndroidRuntime(29762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
02-05 10:41:03.505: E/AndroidRuntime(29762): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您需要初始化列表
list = new ArrayList<String>();
答案 1 :(得分:0)
您显示的堆栈跟踪是针对空指针异常的。 这是说你的文件CameraPreview.java的第30行取消引用空指针。
看看第30行。了解哪个指针为空以及为什么。调试代码,所以当控件在那里传递时,指针就会引用一个对象。
显然,堆栈回溯与您显示的onCreate()没有任何关联。这对你来说是另一回事。为什么你认为它在list.add()中失败了(或者,为什么你显示错误的堆栈追溯)?
顺便说一下,变量“list”在哪里初始化?