得到编译错误,我不明白

时间:2013-03-30 00:36:42

标签: java android compiler-errors timepicker

我是一个非常沮丧的新人! 我真的不知道从哪里开始。我有一个基于傻瓜书中的应用程序的应用程序。搞定了。然后尝试将其转换为使用WIMMOne对话框的时间和日期。在一堆错误的开始后,我加载了他们的一个样本并使其正常工作。然后我将代码分离出来并粘贴在我的样本中。我不能错过任何错误。 (eclipse不断添加它自己的东西也没有用。有时我加载源代码,所有语句,包括导入,都标记为错误,有时我得到R.id,---错误,我甚至有它将评论标记为错误。

但是,我已经清理过它并重新加载了足够多次,我认为我遇到了一些真正的错误。

我和示例代码之间存在一些基本差异: 样本是一个大的活动java文件,我的是一个片段。我的时间对话框有一个单独的java文件。样本在一个文件中,所以我用那种方式编码。我有两个菜单,一个列表,用户从中进入“详细”菜单。样本都是一个屏幕。

WIMM有2个JAR文件,我的libs都有。

为了节省空间,我收录了第一部分,我收到3个错误,但如果需要,我们很乐意提供。

第一个是样本。它工作,甚至使用我的注释代码,没有编译错误: 谢谢,克拉克

    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);

这是我最新的3个错误:“TimePickerDialog”在几个地方使用并且总是有错误。

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<Cursor> 
   {

    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);

3 个答案:

答案 0 :(得分:1)

这主要是为了解释编译错误意味着什么


  

错误:方法setContentView(int)未定义类型ReminderEditFragment

您正在setContentView上调用ReminderEditFragment方法,但ReminderEditFragment没有这样的方法。

您似乎正试图在Activity.setContentView上致电this。但是this不是Activity ...因为你的重构。

  

错误:方法findViewById(int)未定义类型ReminderEditFragment

与上面相同,对于不同的Activity方法。

(我不是Android专家,但看起来你正在片段上做你应该在Activity上做的事情。也许,你的Fragment类需要引用一些它可以用于那些的Activity实例打电话......?)


  

错误:构造函数TimePickerDialog(ReminderEditFragment)未定义

标准TimePickerDialog类有2个构造函数,但它们都没有一个ReminderEditFragment参数。您需要查看TimePickerDialog javadoc ...

答案 1 :(得分:0)

您正在复制Activity中使用的代码并在Fragment中使用它。

这需要进行一些修改。

setContentView(R.layout.reminder_edit);

这用于设置布局。它在onCreate()内部调用。

mTimeButton = (Button) findViewById(R.id.reminder_time); 

这用于获取对某个视图的引用。

Fragment中,要设置布局并查找我们覆盖onCreateView()的视图:

@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);

TimePickerDialog构造函数的参数是Context对象。 在活动中this有效,因为它会引用Activity的实例。 在片段中,您应该拨打getActivity()

TimePickerDialog myTimerHandler = new TimePickerDialog(getActivity());

答案 2 :(得分:0)

1)当活动的XML文件时,不要运行您的应用程序。
如果您这样做,请清理项目并删除生成的文件

2)在向布局添加小部件后,Аlways保存项目Ctrl + Shift + S。保存后会生成这个小部件的地址,并且没有问题 The method findViewById(int) is undefined for the type ReminderEditFragment