0

我正在尝试在对话框中显示日历视图。以下是代码。当我单击启动对话框的按钮时,应用程序崩溃并显示NullPointerException. 在这种情况下,空指针是在行中创建的“cal”变量:

cal = (CalendarView)calenderDialog.findViewById(R.id.calendarView1);

package com.android.filllip.ToDo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;

public class CalendarDialog extends DialogFragment {
    NoticeDialogListener mListener;
    CalendarView cal;
    static int mYear;
    static int mMonth;
    static int mDay;
    static final String tag = "Calendar pop up";

    interface NoticeDialogListener {

        public void onDialogPositiveClick(DialogFragment dialog);

        public void onDialogNegativeClick(DialogFragment dialog);

        public void showTheSelectedDate();
    }

    *public Dialog onCreateDialog(Bundle onSaveInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.calendarpopup, null));
        builder.setNegativeButton("Set ",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                        mListener.onDialogPositiveClick(CalendarDialog.this);
                    }

                });
        builder.setPositiveButton("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                         mListener.onDialogPositiveClick(CalendarDialog.this);
                    }

                });
        AlertDialog calendarDialog = builder.create();
        cal = (CalendarView)calendarDialog.findViewById(R.id.calendarView1);
        Log.d(tag,"calender value is "+cal);
        cal.setOnDateChangeListener(new  OnDateChangeListener(){

            @Override
            public void onSelectedDayChange(CalendarView view, int year,
                    int month, int dayOfMonth) {
                // TODO Auto-generated method stub
                Log.d(tag,mYear+": year");
                Log.d(tag,mMonth+": month");
                Log.d(tag,mDay+": day");
                mYear = year;
                mMonth = month;
                mDay = dayOfMonth;
            }

        });
        return calendarDialog;

    }*

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the
            // host
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }

    }



}

我的布局文件是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CalendarView
        android:id="@+id/calendarView1"
        android:layout_width="match_parent"
        android:layout_height="300dp"

        />

</RelativeLayout>

logcat 跟踪:

03-25 17:10:01.531: E/AndroidRuntime(3102): FATAL EXCEPTION: main
03-25 17:10:01.531: E/AndroidRuntime(3102): java.lang.NullPointerException
03-25 17:10:01.531: E/AndroidRuntime(3102):     at com.android.filllip.ToDo.CalendarDialog.onCreateDialog(CalendarDialog.java:61)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.os.Handler.handleCallback(Handler.java:605)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.os.Looper.loop(Looper.java:137)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at android.app.ActivityThread.main(ActivityThread.java:4511)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at java.lang.reflect.Method.invoke(Method.java:511)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
03-25 17:10:01.531: E/AndroidRuntime(3102):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

像下面这样使用,

public Dialog onCreateDialog(Bundle onSaveInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View v= inflater.inflate(R.layout.calendarpopup, null);
            final CalendarView cal= (CalendarView)v.findViewById(R.id.calendarView1);
于 2013-03-25T21:30:07.120 回答
2

使用 CalendarDialog.setView 并不意味着 CalendarDialog 现在将与视图或 xml 布局有关。你应该在一个单独的视图组中扩展你的布局,然后从这个视图中通过 Id 找到你的视图,然后使用 setView 将视图组放在对话框中。

于 2013-03-25T21:27:40.273 回答