Where to place the clickListeners in my code?
<p>The app has initial message screen that is displayed the first time it run where a date is obtained from user using datepicker then from next time it goes directly to main.xml. This is the logic i tried
in onCreate()</p>

<pre><code>if(&lt;date set&gt;)
{
   &lt;open main.xml&gt;
   &lt;listeners&gt; 
}
else
{
  &lt;get date from user&gt;
  &lt;set flag&gt;
  setContentView(R.layout.initial_msg);
  &lt;make changes in main.xml according to date&gt; 
} 
</code></pre>

<p>The problem is the first time it executes it obtains the date but the listeners are not loaded, i think this is because the code is not executed at all. when i place the listener outside the if block i get a null pointer exception. But when i close the app and start it again it works as this time it opens if() block not else().</p>

<pre><code> public class pgactivity extends Activity 
{
    /** Called when the activity is first created. */
    SharedPreferences prefs;
    Calendar c=Calendar.getInstance();
    Calendar tempDate;
    TextView tv1,tv2;
    Menu theMenu;
    LayoutInflater li;
    int week;

DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener()
{
    //@override
    public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
    {
        c.set(Calendar.DATE,dayOfMonth);
        c.set(Calendar.MONTH,monthOfYear);
        c.set(Calendar.YEAR,year);
        if(checkValidity())
        {
            SharedPreferences.Editor editor=prefs.edit();
            //set the flag that indicates concieved_date has been added
            editor.putBoolean("concieved_date", true); 

            int datepref;  
            datepref=c.get(Calendar.DATE);
            datepref=datepref*1000000;
            datepref+=c.get(Calendar.MONTH)*10000;
            datepref+=c.get(Calendar.YEAR);
            editor.putInt("date",datepref);
            editor.commit();
            setContentView(R.layout.main);
            setData();
        }
    }
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    //check if concieved date is set from the prefs file "app_data"
    //if yes open application page else open initial message screen to set the date
    prefs=getApplicationContext().getSharedPreferences("app_data",Context.MODE_APPEND);

    if(prefs.getBoolean("concieved_date", false))
    {

        setContentView(R.layout.main);
        setData();

        //Listener for the temp button 'false'
        //It resets the flag used to indicate if date is set or not. used for testing purpose
        Button btn1=(Button)findViewById(R.id.setfalse);
        btn1.setOnClickListener(new View.OnClickListener() 
        {               
            public void onClick(View v) 
            {
                SharedPreferences.Editor editor=prefs.edit();
                editor.putBoolean("concieved_date", false);
                editor.commit();

                TextView tv3=(TextView)findViewById(R.id.test);
                tv3.setText("entered listener");
            }
    });

        //Listener for the weekly_tip text view
        //when clicked open the layout giving the full description
        TextView tv2=(TextView)findViewById(R.id.weekly_tip);
        tv2.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent j=new Intent(pgactivity.this,weekly.class);
                Bundle b=new Bundle();
                b.putInt("cur_week",week);
                j.putExtras(b);
                startActivity(j);
            }
        });

        //Listener for open_remainders button to switch to Remainders page
        Button btn2=(Button)findViewById(R.id.open_remainders);
        btn2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                Intent i=new Intent(pgactivity.this,RemaindersPage.class);
                startActivity(i);
            }
        });
    }
    else
    {
        setContentView(R.layout.initial_msg);

        //click listener for textview 2
        TextView tv1=(TextView)findViewById(R.id.init_msg);
        tv1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                SharedPreferences.Editor editor=prefs.edit();
                editor.putBoolean("concieved_date", true);
                editor.commit();

                new DatePickerDialog(pgactivity.this,d,
                        c.get(Calendar.YEAR),
                        c.get(Calendar.MONTH),
                        c.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
    }

}

void setData()
{
    Long sec,tempSec;
    int date,day,month,year;
    date=prefs.getInt("date", 10101010);
    day=date/1000000;
    date=date%1000000;
    month=date/10000;
    date=date%10000;
    year=date;
    c.set(year, month, day);
    tempDate=c;

    //insert value to concieved_date textfield
    tv1=(TextView)findViewById(R.id.concieved_date);
    tv1.setText(android.text.format.DateFormat.format("dd MMM yyyy", tempDate));

    //insert value to delivery_date
    tv1=(TextView)findViewById(R.id.delivery_date);
    tempDate.add(Calendar.DATE, 280);
    tv1.setText(android.text.format.DateFormat.format("dd MMM yyyy", tempDate));

    //insert value to days_to_delivery
    tv1=(TextView)findViewById(R.id.days_to_delivery);
    c=Calendar.getInstance();  //c has current date
    sec=tempDate.getTimeInMillis()-c.getTimeInMillis();  //find diff in millisecs
    sec=sec/86400000;
    tv1.setText(sec.toString());

    //insert value for days_into_pregnancy
    tv1=(TextView)findViewById(R.id.days_into_pregnancy);
    tempSec=280-sec;
    tv1.setText(tempSec.toString());

    //insert value for current_week
    tv1=(TextView)findViewById(R.id.current_week);
    tempSec=tempSec/7;
    week=tempSec.intValue();
    tv1.setText(tempSec.toString());
}

//user method to check the date validity : check if date entered is a reasonable value
boolean checkValidity()
{
    Long duration;
    tempDate=Calendar.getInstance(); //get today's date in c

    //check if user date is on or before today's date
    if(tempDate.before(c)) 
    {
        AlertDialog.Builder ad=new AlertDialog.Builder(this);
        ad.setMessage("Specified date should be on or before today's date");
        ad.setNeutralButton("OK",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface arg0,int arg1){}
                }
        );
        ad.show();
        return false;
    }
    //check if diff between user date and todays date is more than 280 days
    duration=tempDate.getTimeInMillis()-c.getTimeInMillis();
    duration/=86400000;
    if(duration&gt;280)
    {
        AlertDialog.Builder ad=new AlertDialog.Builder(this);
        ad.setMessage("Specified date can be atmost 280 days before today's date");
        ad.setNeutralButton("OK",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface arg0,int arg1){}
                }
        ).show();
        return false;
    }
    return true;    
}
</code></pre>

<p>}</p>
