在自定义对话框中添加ViewPager。无法使适配器正常工作

时间:2014-09-06 21:00:10

标签: android android-viewpager android-dialog

我有一个自定义对话框,我正在尝试实现ViewPager。下面是logCat,DialogClass和布局。我尝试了很多不同的方法,所以我回到了我开始的原始代码。如何在对话框中实现ViewPager?我没有使用片段只是布局。我似乎无法解决这个问题。我已经看过其他问题了,这不是我想要的。

LogCat 第565行错误= 返回arg0 ==((查看)arg1);

09-06 16:50:45.050: W/dalvikvm(29278): threadid=1: thread exiting with uncaught exception (group=0x41b40da0)
09-06 16:50:45.050: E/AndroidRuntime(29278): FATAL EXCEPTION: main
09-06 16:50:45.050: E/AndroidRuntime(29278): Process: com.theworkoutcalculator, PID: 29278
09-06 16:50:45.050: E/AndroidRuntime(29278): java.lang.ClassCastException: com.theworkoutcalculator.Activities.CaloriesLogMainActivity cannot be cast to android.view.View
09-06 16:50:45.050: E/AndroidRuntime(29278):    at com.theworkoutcalculator.UIElements.LogDialog$WizardPagerAdapter.isViewFromObject(LogDialog.java:565)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.support.v4.view.ViewPager.infoForChild(ViewPager.java:1333)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.View.measure(View.java:17387)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.View.measure(View.java:17387)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.View.measure(View.java:17387)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.View.measure(View.java:17387)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.View.measure(View.java:17387)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.View.measure(View.java:17387)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.View.measure(View.java:17387)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.View.measure(View.java:17387)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2211)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1325)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1547)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1234)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6465)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.Choreographer.doFrame(Choreographer.java:573)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.os.Handler.handleCallback(Handler.java:733)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.os.Looper.loop(Looper.java:157)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at android.app.ActivityThread.main(ActivityThread.java:5356)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at java.lang.reflect.Method.invokeNative(Native Method)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at java.lang.reflect.Method.invoke(Method.java:515)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-06 16:50:45.050: E/AndroidRuntime(29278):    at dalvik.system.NativeStart.main(Native Method)

DialogClass,取出大部分不相关的代码

public class LogDialog {

    Context mContext;
    LogDialogListener mListener;
    Log mLog;
    Date mDate;
    /**
     * @param context
     *            required to create and display the dialog
     * @param listener
     *            the callback listener that will receive the newly created log
     */
    public LogDialog(Context context, LogDialogListener listener) {

        mContext = context;
        mListener = listener;
        mDate = new Date();

    }

    /**
     * Interface that handles log creation, deletion, or log edit Notifies the
     * activity class that the log has been modified
     */
    public interface LogDialogListener {

        /**
         * passes the newly created log back to the activity
         * 
         * @param log
         */
        public abstract void logCreated(Log log);

        public abstract void logEdited(Log log);

        public abstract void logDeleted(Log log);
    }

    /**
     * assigns mLog to log for methods
     * 
     * @param log
     */
    public void setLog(Log log) {

        mLog = log;
    }
    /**
     * Creates an alert dialog using a layout inflator then displays the alert
     * dialog using mContext
     */
    // Dialog Design etc.
    public void show() {

        /*
         * Inflater in order to view the dialog
         */

        LayoutInflater factory = LayoutInflater.from(mContext);
        View view = factory.inflate(R.layout.journal_dialog_connector_main,
                null);
        AlertDialog.Builder builder = new AlertDialog.Builder((mContext));

        WizardPagerAdapter adapter = new WizardPagerAdapter();
        final ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
        pager.setAdapter(adapter);


    }
    class WizardPagerAdapter extends PagerAdapter {


        public Object instantiateItem(View collection, int position) {

            int resId = 0;
            switch (position) {
            case 0:
                resId = R.id.layout1;
                break;
            case 1:
                resId = R.id.layout2;
                break;
            case 2:
                resId = R.id.layout3;
                break;

            }
            return mContext;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);
        }
    }
}

布局          

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <include
                android:id="@+id/LinearLayout1"
                layout="@layout/journal_dialog_page_one" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <include
                android:id="@+id/LinearLayout2"
                layout="@layout/journal_dialog_history_list" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <include
                android:id="@+id/LinearLayout3"
                layout="@layout/scanner" />
        </LinearLayout>
    </android.support.v4.view.ViewPager>

</LinearLayout>

修改 ** CaloriesLogMainActivity **我在这里称之为Dialog。

public class CaloriesLogMainActivity extends Activity
        implements
            OnItemClickListener,
            OnClickListener,
            LogDialogListener,
            OnDateSetListener,
            {

    private TextView currentDateTextView;
    private TextView noLogsTextView;

    private Date currentDate = new Date();
    private ListView logsList;
    private LogsArrayAdapter logsAdapter;
    ViewPager pager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.journal_main);

        currentDateTextView = (TextView) findViewById(R.id.current_date);
        currentDateTextView.setText(formatDate(currentDate));

        // List View Day of the week
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        Date d = new Date();
        String dayOfTheWeek = sdf.format(d);
        TextView listDayOfWeek = (TextView) findViewById(R.id.tvDailyList);
        listDayOfWeek.setText(dayOfTheWeek + "'s Logs");


    }


    /**
     * Opens a new dialog where the user will be able to create a new log
     */
    private void show_create_log_dialog() {

        LogDialog dialog = new LogDialog(this, this);

        dialog.setDate(currentDate);
        dialog.show();
    }

    /**
     * Opens a new dialog passing the log into the LogDialog. This is used for
     * editing a single log
     * 
     * @param log
     */
    private void show_create_log_dialog(Log log) {

        LogDialog dialog = new LogDialog(this, this);
        dialog.setLog(log);
        dialog.show();

    }

    /**
     * Opens the a date picker dialog. Once the user picks a date, the log list
     * will be updated with logs from that day
     */

    private void show_datepicker_dialog() {

        Calendar cal = Calendar.getInstance();
        cal.setTime(currentDate);

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dialog = new DatePickerDialog(this, this, year, month,
                day);

        dialog.getDatePicker().setCalendarViewShown(true);

        /* Spinner View if you want to Show Set it to True */
        dialog.getDatePicker().setSpinnersShown(true);

        dialog.show();
    }

    /**
     * Starts a new activity that displays all the logs saved by the user
     */
    private void show_complete_log() {

        Intent myIntent = new Intent(this, CompleteLogActivity.class);
        this.startActivity(myIntent);
    }

    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.current_date) {
            show_datepicker_dialog();
        } else if (id == R.id.view_comp_log) {
            show_complete_log();
        }
    }
    /**
     * formats a java.util.date object to a string for displaying to the user
     * 
     * @param date
     * @return String
     */
    public static String formatDate(Date date) {

        Context context = CalorieLogApplication.context();
        String dateFormat = context.getResources().getString(
                R.string.date_format);
        return new SimpleDateFormat(dateFormat).format(date);
    }

    /**************************************************************************
     * 
     * LogDialogListener Method Implementation
     * 
     *************************************************************************/

    /**
     * 
     * Callback after a log is created from a LogDialogLIstener. Add the new log
     * to the list and refresh the loglist
     * 
     * @param [Log] log
     */
    public void logCreated(Log log) {

        if (areDatesEqual(log.getDate(), currentDate)) {
            // logs.add(log);
            logsAdapter.add(log);
            updateList();
        }
    }

    /**
     * 
     * Callback after a log is edited from a LogDialogLIstener. Refreshes the
     * log list
     * 
     * @param [Log] log
     */
    public void logEdited(Log log) {

        updateList();
    }

    /**
     * 
     * Callback after a log is Deleted from a LogDialogLIstener. Refreshes the
     * log list
     * 
     * @param [Log] log
     */
    public void logDeleted(Log log) {

        logsAdapter.remove(log);
        updateList();
    }

    /**
     * Compares Two date and returns whether the two dates are the same day
     * 
     * @param [Date] a
     * @param [Date] b
     * @return
     */
    private boolean areDatesEqual(Date a, Date b) {

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(a);
        cal2.setTime(b);

        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.DAY_OF_YEAR) == cal2
                        .get(Calendar.DAY_OF_YEAR);
    }

    /**
     * Refreshes the list of logs being displayed when the date is changed from
     * the date picker dialog
     */
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        Calendar cal = Calendar.getInstance();
        cal.set(year, monthOfYear, dayOfMonth);
        currentDate = cal.getTime();

        String date_string = formatDate(currentDate);
        currentDateTextView.setText(date_string);

        logsAdapter.setLogs(Log.logsByDate(currentDate));

        updateList();

    }

    /**
     * Calls the method to display the edit/create dialog box when a list item
     * is clicked
     */
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        Log log = logsAdapter.getItem(arg2);

        show_create_log_dialog(log);

    }
}

1 个答案:

答案 0 :(得分:1)

你应该替换:

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

有了这个:

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == arg1;
}

并且您的第二个错误:

public Object instantiateItem(View collection, int position) {

    int resId = 0;
    switch (position) {
    case 0:
        resId = R.id.layout1;
        break;
    case 1:
        resId = R.id.layout2;
        break;
    case 2:
        resId = R.id.layout3;
        break;

    }
    //Replace -> return mContext; with the code below
    return collection.findViewById(resId); 
}