显示TOAST时尝试调用虚方法?

时间:2016-04-23 07:00:39

标签: java android android-fragments android-asynctask android-dialogfragment

当您点击我的活动中的按钮时,它会启动/显示DatePickerDialog。当用户选择日期并点击" ok"我想在原始类中运行AsyncTask(单击按钮的活动)。一切正常但我希望在AsyncTask完成时向用户显示一个TOAST,但是这样做时仍然会出错。

继承我的代码:

BuyerHomePage.java中的Button方法

    public void MeetingCreator(){
        CalenderImageButton =     (ImageButton)findViewById(R.id.CalenderImageButton);
    CalenderImageButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                                DialogFragment picker = new DatePickerFragment();
                                picker.show(getFragmentManager(), "datePicker");
           }
        }
    );
}

DatePickerFragment.java代码

 public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

public static String formattedDate;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
    Calendar c = Calendar.getInstance();
    c.set(year, month, day);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    formattedDate = sdf.format(c.getTime());

    BuyerHomePage Meeting = new BuyerHomePage();
    Meeting.new MeetingSender().execute();
}
}

BuyerHomePage.java(AsyncTask中的post方法)

@Override
    protected void onPostExecute (String result){

        if (result.equals("email sent")) {
            //This is where i get the error
            Toast.makeText(BuyerHomePage.this, "Email sent!", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(BuyerHomePage.this, "Can't send email", Toast.LENGTH_LONG).show();
        }
    }

错误日志:

04-23 02:25:31.631 22071-22071/com.wilsapp.wilsapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.wilsapp.wilsapp, PID: 22071
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
    at android.widget.Toast.<init>(Toast.java:102)
    at android.widget.Toast.makeText(Toast.java:259)
    at com.wilsapp.wilsapp.BuyerHomePage$MeetingSender.onPostExecute(BuyerHomePage.java:930)
    at com.wilsapp.wilsapp.BuyerHomePage$MeetingSender.onPostExecute(BuyerHomePage.java:836)
    at android.os.AsyncTask.finish(AsyncTask.java:651)
    at android.os.AsyncTask.-wrap1(AsyncTask.java)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

如果有任何混淆或我需要解释任何事情请评论!谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个。

var tabID = sessionStorage.tabID && sessionStorage.closedLastTab !== '2' ? sessionStorage.tabID : sessionStorage.tabID = Math.random();
sessionStorage.closedLastTab = '2';
$(window).on('unload beforeunload', function() {
      sessionStorage.closedLastTab = '1';
});