简体   繁体   English

当我从另一个类调用方法时,出现空指针异常

[英]Null pointer exception when i invoke a method from another class

I have a class called HeadUpMain.java that contains the method NombreDueno(), this method returns an string. 我有一个名为HeadUpMain.java的类,其中包含方法NombreDueno(),该方法返回一个字符串。 I call this method from another class, named contadorNotify but i always got NullPointerException. 我从另一个名为contadorNotify的类调用此方法,但是我总是收到NullPointerException。 I'm learning to code, and i don´t know what to do in this case. 我正在学习编码,在这种情况下我不知道该怎么办。 Thank's in advance. 提前致谢。 This is part of the code for the HeadUpMain Class. 这是HeadUpMain类的代码的一部分。

 public class HeadUpMain extends AppCompatActivity {

  public String NombreDueno(){
    final String[] projection = new String[]
            { ContactsContract.Profile.DISPLAY_NAME };
    String name = " ";
    String division[];
    String parte1 = " ";

    final Uri dataUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
    final ContentResolver contentResolver = getContentResolver();
    final Cursor c = contentResolver.query(dataUri, projection, null, null, null);

    try
    {
        if (c.moveToFirst())
        {
            name = c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME));
            division = name.split(" ");
            parte1 = division[0];
        }
    }
    finally
    {
        c.close();
    }
    System.out.println(name);
    return parte1;

 }  
}

I call the method in the other class as follows: 我在另一个类中调用该方法,如下所示:

public class ContadorNotify extends CountDownTimer {

HeadUpMain nombre = new HeadUpMain();



private long inicio, notifyFirst, notifySecond, notifyThird;
private Context context;
MensajesNotificacion portador = new MensajesNotificacion();

public ContadorNotify(long millisInFuture, long countDownInterval, long cancel, long first, long second, long thidr
                      , Context context) {
    super(millisInFuture, countDownInterval);
    inicio = cancel / 1000;
    notifyFirst = first / 1000;

}

@Override
public void onTick(long millisUntilFinished) {
    //System.out.println("Seconds left: " + millisUntilFinished / 1000);
    if ((millisUntilFinished / 1000) == inicio) {
        onNotify("1");
    } 
}

public void onNotify(String event) {
    if (event == "1") {
        Notificacion noti = new Notificacion(nombre.NombreDueno() , portador.getMensaje(), context);
        noti.Notificador();
    } 
  }
}

I recivied the null pointer exception as soon as the notification it's triggered. 通知触发后,我立即删除了空指针异常。

The log is this: 日志是这样的:

08-25 17:55:32.969  17782-17782/headup.digitalexperiences.com.headup E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: headup.digitalexperiences.com.headup, PID: 17782
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
            at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:96)
            at headapp.digitalexperiences.com.headapp.HeadUpMain.NombreDueno(HeadUpMain.java:163)
            at headapp.digitalexperiences.com.headapp.ContadorNotify.onNotify(ContadorNotify.java:53)
            at headapp.digitalexperiences.com.headapp.ContadorNotify.onTick(ContadorNotify.java:39)
            at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

The reason why you are getting a null pointer is because the Activity was not created correctly therefore its Context is not created. 之所以得到空指针,是因为未正确创建活动,因此未创建其上下文。

HeadUpMain is an Android Activity. HeadUpMain是一个Android活动。 You should never create an Activity with new. 您永远不应使用new创建一个Activity。

HeadUpMain nombre = new HeadUpMain();   // Wrong

Instead it should be started from an Intent from your AndroidManifest, or by using startActivity(). 相反,应该从您的AndroidManifest中的Intent或使用startActivity()启动它。

Intent intent = new Intent(context, HeadUpMain.class);  
startActivity(intent);                  // Correct

http://developer.android.com/training/basics/firstapp/starting-activity.html http://developer.android.com/training/basics/firstapp/starting-activity.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM