我有一個名爲HeadUpMain.java的類,它包含方法NombreDueno(),此方法返回一個字符串。我從另一個類中調用這個方法,名爲contadorNotify,但我總是得到NullPointerException。我正在學習編碼,在這種情況下我不知道該怎麼做。提前致謝。這是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;
}
}
我調用該方法中的其他類,如下所示:
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();
}
}
}
我儘快通知它的觸發recivied空指針異常。
日誌是這樣的:
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)
的可能重複[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/what-is-a-null -pointer-exception-and-how-do-i-fix-it) – John3136
查看http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android和http:// stackoverflow的.com /問題/ 29199308 /機器人化內容上下文getcontentresolver-上一個空對象引用。 –