带游标的java.lang.NullPointerException

时间:2011-08-28 19:26:43

标签: android cursor nullpointerexception

我有两个.java文件:Read.javaSave.java。当我尝试启动我的应用程序时,我得到NullPointerException。 Logcat条目指的是

Cursor cu = cr.query(URI, null, null, null, null);

rc.Cont(destination.getAbsolutePath());是NullPointerException的原因。

但是,我已经尝试Read.java分开了,除了我以外它工作得很好。我猜,这是来自另一个正在制造麻烦的班级的电话。谢谢你的帮助。

- Read.java

public class ReadCon(String destination) {

`...`

     public void Cont(String destination) {

           ContentResolver cr = getContentResolver();

           Cursor cu = cr.query(URI, null, null, null, null);

           ...

     }

}

- Save.java

public class SaveEnd extends Activity {

    File destination;

    Fonctions f = new Fonctions();

    //creerDossier return type is File

    destination = f.creerDossier(2, Save.pathsList[i]);

ReadCon rc = new ReadCon();

rc.Cont(destination.getAbsolutePath()); 

}

这是堆栈跟踪:

java.lang.NullPointerException

at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
at projet.sarelo.ReadContacts.SaveContacts(Read.java:134)
at projet.sarelo.SaveEnd.onStart(Save.java:75)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1132)
at android.app.Activity.performStart(Activity.java:4320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:126)
at android.app.ActivityThread.main(ActivityThread.java:3997)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

问题解决了。谢谢大家的帮助。不知何故,getContentResolver()来自

cont(String destination)null课程中返回SaveEnd。我使用Context实例来设置它。这是我的最终代码

public class ReadCon {
    public void Cont(File destination, Context c) {
       ContentResolver cr = c.getContentResolver();
       Cursor cu = cr.query(URI, null, null, null, null);
    }
}


public class SaveEnd extends Activity {
    Context c = this;
    ReadCon rc = new ReadCon();
    rc.Cont(destination, c); 
}