Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello people,

I am making some application and I have some error, which I can not solve, I searched on google but nothing helped me.

I have Main class and "Lasta" class, Lasta is name of class.
In Main class I only have this code:

Java
package com.nemanjapetrovic.ndevelope.lasta;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity 
{
	//Global variable
	Lasta lasta = new Lasta();
	
	//Controls ID's
	Button date,time,search;
	EditText from_station,to_station;

	
	// ------------------ Load ------------------

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		//Controls
		date = (Button)findViewById(R.id.date);
		time = (Button)findViewById(R.id.time);
		search = (Button)findViewById(R.id.search);
		from_station = (EditText)findViewById(R.id.from_station);
		to_station = (EditText)findViewById(R.id.to_station);
		
		date.setText(lasta.get_DATE());
		time.setText(lasta.get_TIME());
	}
	
	public void dateButton_OnClick(View v)
	{
		lasta.DateChange();
		date.setText(lasta.get_DATE());
	}
	
	public void timeButton_OnClick(View v)
	{
		lasta.TimeChange();
		time.setText(lasta.get_TIME());
	}
	
	public void seachButton_OnClick(View v)
	{
		lasta.Search();
	}
}



In another class, Lasta class I have method who creates DatePickerDialog, and also TimePickerDialog.

Here is a code:

Java
package com.nemanjapetrovic.ndevelope.lasta;

import android.app.Activity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.os.Bundle;
import android.view.View;

public class Lasta extends Activity
{
	//Global variables
	
	//Variable data for JSON
	private String _STATION_FROM;
	private String _STATION_TO;
	private String _DATE;
	private String _TIME;
	
	//Variable for date/time picker dialog change
	private Calendar nowCalendar = Calendar.getInstance();
	
	
	//------------------ Constructors ------------------
	
	
	//Default constructor
	public Lasta()
	{
		//Set station variables to ""
		this._STATION_FROM = "";
		this._STATION_TO = "";
		//Set date variable
		UpdateDATE();
		//Set time variable
		UpdateTIME();
	}
	
	//Constructor that sets all elements
	public Lasta(String sf, String st, String d, String t)
	{
		this._STATION_FROM = sf;
		this._STATION_TO = st;
		this._DATE = d;
		this._TIME = t;
	}

	
	// ------------------ DateChange ------------------
	 HERE I AM GETTING AN ERROR
	
	//DatePickerDialog.OnDateSetListener
	private DatePickerDialog.OnDateSetListener dateDialog = new DatePickerDialog.OnDateSetListener()
	{
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
		{
			//Set new data
			nowCalendar.set(Calendar.YEAR, year);
			nowCalendar.set(Calendar.MONTH, monthOfYear);
			nowCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			
			UpdateDATE();
		}
	};

         HERE I AM GETTING AN ERROR
	//Calling from MAIN
	//Method for creating PickerDialog and changing _DATE
	public void DateChange() 
	{
		//This DatePickerDialog calls DatePickerDialog.OnDateSetListener which sets all data for Calendar
		new DatePickerDialog(this, DatePickerDialog.THEME_HOLO_DARK, dateDialog, nowCalendar.get(Calendar.YEAR), nowCalendar.get(Calendar.MONTH), nowCalendar.get(Calendar.DAY_OF_MONTH)).show();	
	}
	
	private void UpdateDATE()
	{
		//Set new _DATE
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); //Formating date
		this._DATE = df.format(this.nowCalendar.getTime());
	}


I am getting an error in method called:
Java
public void DateChange() 
	{
		//This DatePickerDialog calls DatePickerDialog.OnDateSetListener which sets all data for Calendar
		new DatePickerDialog(this, DatePickerDialog.THEME_HOLO_DARK, dateDialog, nowCalendar.get(Calendar.YEAR), nowCalendar.get(Calendar.MONTH), nowCalendar.get(Calendar.DAY_OF_MONTH)).show();	
	}


Here is an error code:
04-18 21:53:14.192: E/AndroidRuntime(31880):
XML
FATAL EXCEPTION: main
04-18 21:53:14.192: E/AndroidRuntime(31880): java.lang.IllegalStateException: Could not execute method of the activity
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.view.View$1.onClick(View.java:3804)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.view.View.performClick(View.java:4439)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.widget.Button.performClick(Button.java:139)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.view.View$PerformClick.run(View.java:18395)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.os.Handler.handleCallback(Handler.java:725)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.os.Looper.loop(Looper.java:176)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.app.ActivityThread.main(ActivityThread.java:5319)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at java.lang.reflect.Method.invoke(Method.java:511)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at dalvik.system.NativeStart.main(Native Method)
04-18 21:53:14.192: E/AndroidRuntime(31880): Caused by: java.lang.reflect.InvocationTargetException
04-18 21:53:14.192: E/AndroidRuntime(31880):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at java.lang.reflect.Method.invoke(Method.java:511)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.view.View$1.onClick(View.java:3799)
04-18 21:53:14.192: E/AndroidRuntime(31880):    ... 12 more
04-18 21:53:14.192: E/AndroidRuntime(31880): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.app.Activity.getSystemService(Activity.java:4662)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.app.Dialog.<init>(Dialog.java:164)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.app.AlertDialog.<init>(AlertDialog.java:114)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.app.AlertDialog.<init>(AlertDialog.java:110)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at android.app.DatePickerDialog.<init>(DatePickerDialog.java:113)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at com.nemanjapetrovic.ndevelope.lasta.Lasta.DateChange(Lasta.java:99)
04-18 21:53:14.192: E/AndroidRuntime(31880):    at com.nemanjapetrovic.ndevelope.lasta.MainActivity.dateButton_OnClick(MainActivity.java:40)





Sorry for this long question. Thank you!
Posted
Comments
[no name] 18-Apr-15 17:31pm    
And what -other than Google- you invetigate to solve the pb?
[no name] 18-Apr-15 17:35pm    
StackOverflow and etc...
new DatePickerDialog(this, DatePickerDialog.THEME_HOLO_DARK, dateDialog, nowCalendar.get(Calendar.YEAR), nowCalendar.get(Calendar.MONTH), nowCalendar.get(Calendar.DAY_OF_MONTH)).show();
Here in this line I am getting an error, and I think the error is "this", and should use "getApplication()" but it wont work, it's say getApplication() do not exist.
[no name] 18-Apr-15 17:42pm    
Ok, sounds you investigate...give me/us un rato
[no name] 18-Apr-15 17:45pm    
Thank you. If you have example code for date picker dialog used in another class except Main class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900