eishiakonno
eishiakonno

Reputation: 73

onClickListener on DialogFragment

I have a fragment that extends DialogFragment and I have a custom layout for it which contains three EditTexts and two buttons - save and cancel. My dialog displays just fine, using the onCreateView method for specifying the layout, but when I implemented setOnclickListener my app crashes. This may have a simple solution, but I am stuck already several hours. I would very much appreciate an advice or example code.

Below is the code for the DialogFragment

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class UpdateGrade extends DialogFragment{

    private Button cancelBtn;
    @SuppressLint("SimpleDateFormat")
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.update_grade_layout, container);
        cancelBtn = (Button) view.findViewById(R.id.cancelBtn);
        EditText dateField = (EditText) view.findViewById(R.id.dateField);
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = df.format(c.getTime());
        dateField.setText(formattedDate);

        cancelBtn.setOnClickListener(new View.OnClickListener() {  //LINE 28

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        return view;
    }
}

and below is where I am calling the DialogFragment to appear

        @Override
        public void onItemClick(AdapterView<?> a, View v, int i, long l) {
            // TODO Auto-generated method stub
            @SuppressWarnings("unchecked")
            HashMap<String, String> selected_row = (HashMap<String, String>) simpleAdapter.getItem(i);
            String selectedGrade=selected_row.get("grade_id");
            FragmentManager fm = getFragmentManager();
            UpdateGrade d = new UpdateGrade();
            d.show(fm, "update_grade_layout");

        }

This is the LogCat detail

11-17 02:09:56.758: E/AndroidRuntime(778): FATAL EXCEPTION: main
11-17 02:09:56.758: E/AndroidRuntime(778): java.lang.NullPointerException
11-17 02:09:56.758: E/AndroidRuntime(778):  at com.feufern.apms.UpdateGrade.onCreateView(UpdateGrade.java:28)
11-17 02:09:56.758: E/AndroidRuntime(778):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
11-17 02:09:56.758: E/AndroidRuntime(778):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
11-17 02:09:56.758: E/AndroidRuntime(778):  at android.app.BackStackRecord.run(BackStackRecord.java:622)
11-17 02:09:56.758: E/AndroidRuntime(778):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
11-17 02:09:56.758: E/AndroidRuntime(778):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:417)

Upvotes: 0

Views: 218

Answers (1)

Adam Arold
Adam Arold

Reputation: 30528

You are trying to add a listener:

cancelBtn.setOnClickListener(new View.OnClickListener() {  // ...

to a cancelBtn but it is null.

You should check the xml files you have because this:

cancelBtn = (Button) view.findViewById(R.id.cancelBtn);

might be problematic. This is usually a layout problem so as others pointed out double check your layout files.

As a general rule of thumb for debugging NullPointerExceptions:

You will always see that on which line you get an NPE. You only have to debug your program and set a breakpoint at that line. This way you can easily check which reference is null. In your case you have to break at line 28 and check the references.

Upvotes: 1

Related Questions