0

我是一个非常沮丧的新手!我真的不知道从哪里开始。我有一个基于傻瓜书中的应用程序。得到它的工作。然后尝试将其转换为使用 WIMMOne 对话框获取时间和日期。在一堆错误的开始之后,我加载了他们的一个样本并让它工作。然后我隔离了其中的代码并粘贴到我的示例中。我无法克服过去的错误。(eclipse不断添加自己的一些内容并没有帮助。有时我加载源代码并且所有语句(包括导入)都被标记为错误,有时我得到R.id,---错误,我什至有它将评论标记为错误。

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

我的和示例代码之间有几个基本的区别:示例是一个大的活动 java 文件,我的是一个片段。我的时间对话框有一个单独的 java 文件。样本在一个文件中,所以我用这种方式编码。我的有两个菜单,用户可以从一个列表转到“详细”菜单。样本都是一个屏幕。

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

为了节省空间,我包含了第一部分,其中我遇到了 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);
4

3 回答 3

1

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


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

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

您似乎正在尝试调用Activity.setContentView. thisthis不是Activity... 因为你的重构。

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

同上,换一种Activity方法。

(我不是 Android 专家,但您似乎在 Fragment 上做一些您应该在 Activity 上做的事情。也许,您的 Fragment 类需要引用一些可用于这些调用的 Activity 实例。 . ?)


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

标准TimePickerDialog类有 2 个构造函数,但它们都不接受单个ReminderEditFragment参数。你需要看看TimePickerDialogjavadoc ...

于 2013-03-30T01:00:12.870 回答
0

您正在复制在 an 中Activity使用的代码并在Fragment.

这将需要一些修改。

setContentView(R.layout.reminder_edit);

这用于设置布局。它被称为内部onCreate()

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

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

在 aFragment中,要设置布局并查找我们覆盖的视图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对象。在 Activitythis中是有效的,因为它会引用 Activity 的实例。在 Fragment 中,您应该getActivity()改为调用。

TimePickerDialog myTimerHandler = new TimePickerDialog(getActivity());
于 2013-03-30T01:08:27.037 回答
0

1) Don't run your app, when active XML file.
If you did it, clean project and remove generated file

2) А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

于 2013-03-30T01:12:02.480 回答