开发者

Subclassing an OnClickListener

开发者 https://www.devze.com 2023-02-18 16:33 出处:网络
I\'m trying to create a simple Calendar application in android, mostly for personal learning as I am new to Android (and Java for that matter).

I'm trying to create a simple Calendar application in android, mostly for personal learning as I am new to Android (and Java for that matter).

Basically, it's a ListView showing the current days of the month that, when clicked, opens a dialog asking for an event description.

Obviously, this dialog has an OK button that when clicked should update the ListView with the input from the EditText.

However, I'm having trouble getting the OnClickListener for the OK button to work, no matter what I try, I get a NullPointerException on the line where I set it.

public class SimpleCalendar extends ListActivity {

    public static Context appContext;
    public static int nMonthChoice = CalendarIO.myCalendar.get(Calendar.MONTH);
    private static ArrayAdapter aAd;
    private static String[] ArrayOfDays;
    final private static String[] dayNames = {"Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday", "Saturday"};
    Dialog userInput;
    int currentDay;
    EditText dialogInput;


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        appContext = this;
        TextView tv = new TextView(this);
        CalendarIO.initCalendar(tv);

        CalendarIO.myCalendar.set(Calendar.MONTH, nMonthChoice);
        updateMonth();
        aAd = new ArrayAdapter(this, R.layout.main, ArrayOfDays);

        setListAdapter(aAd);

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

            lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                LinearLayout dialogLayout = (LinearLayout) findViewById(R.id.dLL);
                currentDay = position + 1;
                dialogInput = (EditText) findViewById(R.id.dET);

                userInput = new Dialog(appContext);
                userInput.setContentView(R.layout.dialog);
                userInput.setTitle("New Event");

                Button dBtn = new Button(appContext);
                dBtn.setOnClickListener(new View.OnClickListener()
    {
            public void onClick(View v)
            {

                        userInput.dismiss();
                        CalendarIO.myCalendar.set(Calendar.DAY_OF_MONTH, currentDay);
                        SimpleCalendar.updateMonth();
                        CalendarIO.CalendarEvents.put(CalendarIO.myCalendar.get(Calendar.DAY_OF_YEAR), dialogInput.getText());
                        SimpleCalendar.aAd = new ArrayAdapter(appContext, R.layout.main, ArrayOfDays);
                        setListAdapter(SimpleCalendar.aAd);

        }});
                dialogLayout.addView(dBtn);
                userInput.show();

            }
        });

    }

    public static void updateMonth() {
        int nCounter, nDaysInMonth;
        String sCurrentEvent;
        String currentDay;

        nDaysInMonth = CalendarIO.myCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        ArrayOfDays = new String[nDaysInMonth];

        for (nCounter = 0; nCounter < nDaysInMonth; nCounter++) {


            CalendarIO.myCalendar.set(Calendar.DAY_OF_MONTH, nCounter + 1);
            sCurrentEvent = (String) CalendarIO.CalendarEvents.get(
                    CalendarIO.myCalendar.get(Calendar.DAY_OF_YEAR));

            currentDay = dayNames[CalendarIO.myCalendar.get(Calendar.DAY_OF_WEEK) - 1];

            ArrayOfDays[nCounter] =
                    currentDay + ", " + MonthView.monthNames[nMonthChoice]
                    + " " + (nCounter + 1);

            try {
                if (!sCurrentEvent.equals("")) {
                    ArrayOfDays[nCounter] += ("\t- " + sCurrentEvent + "\n");
                }
            } catch (NullPointerException e) {//don't do anything}
            }

        }
    }
}

I know the code is a little unreadable but I hope you'll bear through it. I'm also planning on cleaning up some of the global variables later..

Anyway, these are the main two methods for input. updateMonth() turns a hash map from my CalendarIO class into a String Array, and of course, onCreate is my main Activity method.

When I run it, ADB gives me:

17:51:53.814    22161   ERROR   AndroidRuntime  FATAL EXCEPTION: main
17:51:53.814    22161   ERROR   AndroidRuntime  java.lang.NullPointerException
17:51:53.814    22161   ERROR   AndroidRuntime      at 
org.me.simplecalendar.SimpleCalendar$1.onItemClick(SimpleCalendar.java:80)

Line 80 is dialogLa开发者_C百科yout.addView(dBtn);

This was my latest attempt: trying to add a newly created button to my XML defined LinearLayout.

I've also tried having the button defined in XML and just adding a listener to it. That also complains about an NPE.

Also, I've tried defining the listener in XML as well and pointing it to a method in the class, but it throws an error saying that it can't find my method in the ComponentInfo class, which is a little bizarre - but I'm sure it's just something I haven't learned yet.

I'm at the end of my attempts.

Any suggestions would be very appreciated.


It seems as though your null pointer reference could be your dialogLayout. This is because you are calling findByViewId before calling SetContentView. You need to call SetContentView before calling any findByViewId methods. You should ideally do this at the beginning of the onCreate method.


Dialog buttons use DialogInterface.OnClickListener not View.OnclickListener


Thanks for the answers. In the end, I decided to use an AlertDialog with a custom View. I wasn't aware that they supported custom views at first (I'm new to this :P). The calendar works great now!

0

精彩评论

暂无评论...
验证码 换一张
取 消