Getting compile errors, I don't uderstand-Collection of common programming errors
I am a very frustrated newbee! I really don’t know where to start. I have an app based on the one in the dummies book. Got it working. Then tried to convert it to use the WIMMOne dialogs for time and date. After a bunch of false starts I loaded one of their samples and got it working. I then isolated the code in that and pasted in my sample. I cannot get past errors. (It doesn’t help that eclipse constantly adds some of its own. Sometimes I load the source and all statements, including imports, are marked as errors, sometimes I get the R.id,— in error, I have even had it marked comments as in error.
However, I have cleaned it and reloaded it enough times that I think I am down to a few real errors.
There are a couple basic differences between mine and the sample code: The sample is one big activity java file, mine is in a fragment. Mine had a separate java file for the time dialog. The sample was in the one file, so I coded mine that way. Mine has two menus, a list from which the user goes to a “detail” menu. The sample is all one screen.
There were 2 JAR files from WIMM, I have both in my libs.
To save space I included the first part, where I am getting 3 errors, but will be happy to provide it all if needed.
The first is the sample. It works and even with my commented code, there are no compile errors: Thanks,Clark
package com.test.medication;
/**************************
* Java Imports
**************************/
import java.util.Calendar;
/******************************
* android imports
******************************/
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/******************************
* Wimm imports
******************************/
import com.wimm.framework.app.LauncherActivity;
import com.wimm.framework.app.TextInputDialog;
import com.wimm.framework.app.TimePickerDialog;
import com.wimm.framework.app.TimePickerDialog.OnTimeSelectedListener;
public class MedicationsActivity extends LauncherActivity
{
/****************************
* private globals
****************************/
private TextInputDialog textDialog;
private TimePickerDialog timeDialog;
private long msToCount = 0;
private static String textInput = "please enter pills";
private EditText timeDisplay;
private EditText pillsDisplay;
private long newMs = 0;
private int Hours;
private int Minutes;
private static int hoursToCount = 0;
private static int minutesToCount = 0;
private Calendar c ;
private int mIndex = 0;
private boolean textEntered = false;
// get an store the simple name of this class into an String
private static final String TAG = MedicationsActivity.class.getSimpleName();
/***********************
* define constants
**********************/
// define id for both the time dialog, and the enter pills dialog
static final int TIME_DIALOG_ID = 0;
static final int PILLS_DIALOG_ID = 1;
// the actions for the intent definitions posted
public static final String ACTION_POST_NOTIFICATION = "com.test.medication.action.POST";
public static final String ACTION_UPDATE_NOTIFICATION = "com.test.medication.action.UPDATE";
public static final String ACTION_CANCEL_NOTIFICATION = "com.test.medication.action.CANCEL";
//delay between views
private static final int viewDelay = 5000; // ms
@Override
public void onCreate(Bundle savedInstanceState)
{
//-----------------------------------------------
// initialize time entry listener
// pass current state
super.onCreate(savedInstanceState);
// open up main layout resource and upload into wimm
setContentView(R.layout.main);
Log.d("MedsMC","S onCrt-1");
// Subscribe listeners to the buttons and text fields
Button okButton = (Button)findViewById(R.id.ok);
Button pillsButton = (Button)findViewById(R.id.enterPills);
pillsDisplay = (EditText)findViewById(R.id.entryMeds);
timeDisplay = (EditText)findViewById(R.id.entryTime);
Log.d("MedsMC","S onCrt-2L" + timeDisplay);
// set up the time picker dialog with initial time
//and subscribe a listener upon the press of ok n the time dialog
TimePickerDialog myTimerHandler = new TimePickerDialog(this);
myTimerHandler.setTime(6, 0);
myTimerHandler.setTimeSelectedListener(timeSetListener);
Log.d("MedsMC","S onCrt-3myTH" + timeDisplay);
This is my latest with 3 of the errors: the “TimePickerDialog” is used in several places and always has an error.
package com.dummies.android.taskreminder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.DatePickerDialog.OnDateSetListener;
//import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri; //
import android.os.Bundle; //
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View; //
//import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button; //
import android.widget.DatePicker;
import android.widget.EditText; //
//import android.widget.TimePicker;
import android.widget.Toast;
import android.util.Log; //
import android.text.format.Time;
import android.app.Dialog; //
import android.content.Context;
import android.content.Intent;
/*******************************************
* wimm imports
*/
import com.wimm.framework.app.LauncherActivity;
//import com.wimm.framework.app.TextInputDialog;
mport com.wimm.framework.app.TimePickerDialog;
import com.wimm.framework.app.TimePickerDialog.OnTimeSelectedListener;
//import com.wimm.framework.app.DatePickerDialog;
//import com.wimm.framework.app.DatePickerDialog.OnDateSelectedListener;
public class ReminderEditFragment
extends Fragment
implements OnDateSetListener,
// OnTimeSetListener,
LoaderCallbacks
{
private TimePickerDialog timeDialog;
//private EditText timeDisplay;
private static int intHours = 0;
private static int intMinutes = 0;
private long lngNewMs = 0;
private Calendar calC ;
// define constent to be used
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String TIME_FORMAT = "kk:mm";
static final int TIME_DIALOG_ID = 0;
static final int PILLS_DIALOG_ID = 1;
public static final String DEFAULT_EDIT_FRAGMENT_TAG = "editFragmentTag";
private EditText mTitleText;
private EditText mBodyText;
//private TextInputDialog textDialog;
//private TextInputDialog mBodyDialog;
//private TimePickerDialog timeDialog;
private Button mTimeButton;
//private Button timeDisplay;
//private TimePickerDialog mTimeDialog;
private Button mDateButton;
private Button mConfirmButton;
private Calendar mCalendar;
//OK
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// save the calendar instance
outState.putSerializable(CALENDAR, mCalendar);
}
private long mRowId;
//OK
@Override
public void onCreate(Bundle savedInstanceState)
{
//-- house keeping
super.onCreate(savedInstanceState);
Log.d("RemindEF","S onCrt");
if (savedInstanceState != null &&
savedInstanceState.containsKey(CALENDAR) )
{
mCalendar = (Calendar) savedInstanceState.getSerializable(CALENDAR);
Log.d("RemindEF","Cs onCrt:" + mCalendar);
}
else
{
mCalendar = Calendar.getInstance();
Log.d("RemindEF","C onCrt:" + mCalendar);
}
Bundle arguments = getArguments();
//-----------------
// retrieve row id from list
if (arguments != null)
{
mRowId = arguments.getLong(ReminderProvider.COLUMN_ROWID);
Log.d("RemindEF","A onCrt:" + mRowId);
}
//-----------------------------------------------
// initialize time entry listener
//???????? ERROR: The method setContentView(int) is undefined for the type ReminderEditFragment
setContentView(R.layout.reminder_edit);
Log.d("RemindEF","X onCrt");
//???????ERROR: The method findViewById(int) is undefined for the type ReminderEditFragment
mTimeButton = (Button)findViewById(R.id.reminder_time);
Log.d("MedsMC","S onCrt-2L" + mTimeButton);
//?????? ERROR: The constructor TimePickerDialog(ReminderEditFragment) is undefined
TimePickerDialog myTimerHandler = new TimePickerDialog(this);
myTimerHandler.setTime(6, 0);
myTimerHandler.setTimeSelectedListener(timeSetListener);
Log.d("MedsMC","S onCrt-3myTH" + mTimeButton);
-
You are copying code that was being used in an
Activity
and using it in aFragment
.This will require some modification.
setContentView(R.layout.reminder_edit);
This is used to set the layout. It is called inside
onCreate()
.mTimeButton = (Button) findViewById(R.id.reminder_time);
This is used to get a reference to a certain View.
In a
Fragment
, to set the layout and find views we overrideonCreateView()
:@Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View v = inflater.inflate(R.layout.reminder_edit, null); mTimeButton = (Button) v.findViewById(R.id.reminder_time); return v; }
TimePickerDialog myTimerHandler = new TimePickerDialog(this);
The argument of the
TimePickerDialog
constructor is aContext
object. In an Activitythis
would be valid because it would reference the Activity’s instance. In a Fragment you should callgetActivity()
instead.TimePickerDialog myTimerHandler = new TimePickerDialog(getActivity());
-
This is mainly an attempt to explain what the compilation errors mean.
ERROR: The method setContentView(int) is undefined for the type ReminderEditFragment
You are calling a
setContentView
method onReminderEditFragment
, butReminderEditFragment
has no such method.It appears that you are trying to call
Activity.setContentView
onthis
. Butthis
is not anActivity
… because of your refactoring.ERROR: The method findViewById(int) is undefined for the type ReminderEditFragment
Same as above, for a different
Activity
method.(I’m not an Android expert, but it seems that you are doing things on a Fragment that you should be doing on an Activity. Perhaps, your Fragment class needs a reference to some Activity instance that it can use for those calls … ?)
ERROR: The constructor TimePickerDialog(ReminderEditFragment) is undefined
The standard
TimePickerDialog
class has 2 constructors, but neither of them take a singleReminderEditFragment
argument. You need to look at theTimePickerDialog
javadoc … -
1) Don’t run your app, when active XML file.
If you did it, clean project and remove generated file2) ?lways save project
Ctrl + Shift + S
after adding widgets to layout. After saving will generated an addresses of this widgets and will have no problem with
The method findViewById(int) is undefined for the type ReminderEditFragment