Note that there are some explanatory texts on larger screens.

plurals
  1. POSubclassing an OnClickListener
    primarykey
    data
    text
    <p>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). </p> <p>Basically, it's a ListView showing the current days of the month that, when clicked, opens a dialog asking for an event description. </p> <p>Obviously, this dialog has an OK button that when clicked should update the ListView with the input from the EditText.</p> <p>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.</p> <pre><code>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&lt;?&gt; 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 &lt; 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} } } } } </code></pre> <p>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..</p> <p>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.</p> <p>When I run it, ADB gives me:</p> <pre><code>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) </code></pre> <p>Line 80 is <code>dialogLayout.addView(dBtn);</code></p> <p>This was my latest attempt: trying to add a newly created button to my XML defined LinearLayout.</p> <p>I've also tried having the button defined in XML and just adding a listener to it. That also complains about an NPE.</p> <p>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.</p> <p>I'm at the end of my attempts.</p> <p>Any suggestions would be very appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload