问题描述:

So the idea is to allow the user to click a button (or textview), display a datepicker, select a date, save the date to a database(on the TODO list) and display the selected date via a toast to the user (or on the textview if i take that route). All this is on a fragment of a tabbed activity

For now I've just used a button and when I click it the app simply crashes. The catlog shows the following

08-19 09:50:57.422 21472-21472/com.example.alibasmaci.maralapplication E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.alibasmaci.maralapplication, PID: 21472

java.lang.NullPointerException

at com.example.alibasmaci.maralapplication.OneWayFragment.showDialogOnButtonClick(OneWayFragment.java:49)

at com.example.alibasmaci.maralapplication.OneWayFragment.onCreateView(OneWayFragment.java:93)

Now the java class in question is here:

 package com.example.alibasmaci.maralapplication;

import android.app.DatePickerDialog;

import android.app.Dialog;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.EditText;

import android.widget.Spinner;

import android.widget.Toast;

/**

* A simple {@link Fragment} subclass.

*/

public class OneWayFragment extends Fragment {

Spinner spinDeparture;

Spinner spinDestination;

Button btnDeparture;

EditText etDepartureDate;

int year_x, month_x, day_x;

static final int DIALOG_ID = 0;

String[] cities = {

"Ottawa",

"Montreal",

"Toronto"

};

public static OneWayFragment newInstance() {

OneWayFragment fragment = new OneWayFragment();

return fragment;

}

public OneWayFragment() {

// Required empty public constructor

}

public void showDialogOnButtonClick(){

btnDeparture = (Button)getActivity().findViewById(R.id.btnDepartureDate);

btnDeparture.setOnClickListener(

new View.OnClickListener() {

@Override

public void onClick(View view) {

getActivity().showDialog(DIALOG_ID);

}

}

);

}

protected Dialog onCreateDialog(int id){

if (id == DIALOG_ID)

return new DatePickerDialog(getActivity(),dPickerListener, year_x, month_x, day_x);

return null;

}

private DatePickerDialog.OnDateSetListener dPickerListener = new DatePickerDialog.OnDateSetListener() {

@Override

public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

year_x = year;

month_x = monthOfYear;

day_x = dayOfMonth;

Toast.makeText(getActivity(), year_x+"/"+month_x+"/"+day_x,Toast.LENGTH_LONG).show();

}

};

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

View rootView = inflater.inflate(R.layout.fragment_one_way, container, false);

// Spinner to display departure points

spinDeparture = (Spinner) rootView.findViewById(R.id.spinnerDeparture);

ArrayAdapter adapterDeparture = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,cities);

spinDeparture.setAdapter(adapterDeparture);

// Spinner to display destinations

spinDestination = (Spinner) rootView.findViewById(R.id.spinnerDestination);

ArrayAdapter adapterDestination = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,cities);

spinDeparture.setAdapter(adapterDestination);

// Show date picker when departure button is clicked

showDialogOnButtonClick();

return rootView;

}

}

The line to which the error is referring to is here:

 public void showDialogOnButtonClick() {

Any thoughts on why this is crashing?

网友答案:

Try this:

Move this : btnDeparture = (Button)rootView.findViewById(R.id.btnDepartureDate);

to onCreateView() { ...} after the inflate

and for onClick try this:

 public void showDialogOnButtonClick(){
        btnDeparture.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        view.getContext().showDialog(DIALOG_ID);
                    }
                }
        );
    }

Try it.

相关阅读:
Top