无法理解logcat消息并找到NullPointerException

时间:2014-03-07 12:28:41

标签: android nullpointerexception contacts

我正在开发一个在实际应用程序中使用的联系人管理器,但我遇到了问题,我无法解决它。

以下是我在运行应用程序时在LogCat中获得的内容:

02-28 15:18:31.310: D/AndroidRuntime(845): Shutting down VM 02-28 15:18:31.310: W/dalvikvm(845): threadid=1: thread exiting with uncaught exception (group=0xb4a1db90)`
02-28 15:18:31.320: E/AndroidRuntime(845): FATAL EXCEPTION: main
02-28 15:18:31.320: E/AndroidRuntime(845): Process: br.com.agendadecontatos, PID: 845
02-28 15:18:31.320: E/AndroidRuntime(845): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.agendadecontatos/br.com.agendadecontatos.LerContatos}: java.lang.NullPointerException
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.os.Handler.dispatchMessage(Handler.java:102)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.os.Looper.loop(Looper.java:137)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.app.ActivityThread.main(ActivityThread.java:4998)
02-28 15:18:31.320: E/AndroidRuntime(845):  at java.lang.reflect.Method.invokeNative(Native Method)
02-28 15:18:31.320: E/AndroidRuntime(845):  at java.lang.reflect.Method.invoke(Method.java:515)
02-28 15:18:31.320: E/AndroidRuntime(845):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-28 15:18:31.320: E/AndroidRuntime(845):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-28 15:18:31.320: E/AndroidRuntime(845):  at dalvik.system.NativeStart.main(Native Method)
02-28 15:18:31.320: E/AndroidRuntime(845): Caused by: java.lang.NullPointerException
02-28 15:18:31.320: E/AndroidRuntime(845):  at br.com.agendadecontatos.LerContatos.onCreate(LerContatos.java:34)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.app.Activity.performCreate(Activity.java:5243)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-28 15:18:31.320: E/AndroidRuntime(845):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-28 15:18:31.320: E/AndroidRuntime(845):  ... 11 more
02-28 15:18:40.440: I/Process(845): Sending signal. PID: 845 SIG: 9

我已经尝试每次解决一个问题,但我在代码中找不到任何NullPointerException。这些是唯一看似无效的人。

Cursor C_Contatos =this.ctx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME);

Cursor C_Telefones = this._ctx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =" +
                        " " + _IDContato, null, null);

编辑:这是使用OnCreate方法的LerContatos代码:

package br.com.agendadecontatos;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class LerContatos extends Activity 
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ListView listaPessoas = (ListView)findViewById(android.R.id.list);

        List<ProvedorDeContatos> ListaContatos = new ArrayList<ProvedorDeContatos>();

        Contatos Contato = new Contatos(this);

        ListaContatos = Contato.getContatos();       

        ArrayAdapter<ProvedorDeContatos> adapter = 
                new ArrayAdapter<ProvedorDeContatos>(this, android.R.layout.simple_list_item_1, ListaContatos);   
        listaPessoas.setAdapter(adapter);

        Button botaoAdicionar = (Button) findViewById(R.id.btnAdicionar);

        botaoAdicionar.setOnClickListener(new View.OnClickListener() 
        {

                        @Override
                        public void onClick(View v) 
                        {

                            Intent intent = new Intent();
                            intent.setClass(LerContatos.this,
                                    CameraAndroid.class);

                            startActivity(intent);

                            finish();
                        }
                    });

    }       
}

感谢您的帮助!我找到了重新创建适配器的方法。

1 个答案:

答案 0 :(得分:0)

您可以在stacktrace中查找与您的代码相关的第一行。它会告诉你异常发生的位置(在这种情况下,看起来像: LerContatos.java:34

转到代码中的这一行,查看此处访问的变量。有一个在使用之前尚未实例化。这意味着,此时该变量的值为NULL,并且是错误的原因。