问题描述
我知道这可能是重复的,但是我的情况有所不同,需要对此特定问题进行一些解释.
I know this may be a duplicate but my situation is different which requires some explanation on this specific issue.
我遇到了错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
在onClick
中,从类MyCalendar
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyCalendar myCalendar = new MyCalendar();
myCalendar.addEvent();
// myCalendar.AddEvent(2,context);
}
});
它调用以下方法
public void addEvent(){
Calendar beginTime = Calendar.getInstance();
beginTime.set(2018, 11, 14, 7, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2018, 11, 14, 8, 30);
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
.putExtra(Events.TITLE, "TEST ANDROID")
.putExtra(Events.DESCRIPTION, "EVENT HAS BEEN ADDED FROM ANDROID STUDIO")
.putExtra(Events.EVENT_LOCATION, "GIBRALTAR")
.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
.putExtra(Events.CALENDAR_ID, 2)
.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
startActivity(intent);
}
当在MainActivity
中声明该方法时,它确实起作用,但是现在我将其移至另一个类,它显示了上述异常.
This method does work when it is declared within the MainActivity
but now that I have moved it to another class, it is showing that above exception.
我该如何解决?
推荐答案
您将需要将调用活动作为上下文参数传递给您的new MyCalendar(context)
调用.然后从您的日历类中调用context.startActivity(intent)
You will need to pass the calling activity as a context parameter to your new MyCalendar(context)
call. Then from your calendar class, invoke context.startActivity(intent)
这篇关于java.lang.NullPointerException:尝试调用虚拟方法'android.app.ActivityThread $ ApplicationThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!