使用时间选择器的空指针异常

时间:2013-09-19 21:42:20

标签: java android dialog nullpointerexception timepicker

我正在尝试创建一个简单的应用程序,当您单击“检查”按钮时打开时间选择器对话框,一旦设置了时间并点击“确定”,它就会检查系统时间并输出是否正确。但是,我在MainActivity.java的第21行给出了一个空指针异常。此时,只需设置代码以在点击“检查”后调出对话框。

MainActivity.java

package com.example.timechecker;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public FragmentManager getSupportFragmentManager() {
    return null;
}
public void checkTime(View view) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");

}

}

TimePickerFragment.java

package com.example.timechecker;
import java.util.Calendar;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment
                        implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
}
}

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timechecker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.timechecker.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
        <activity
        android:name="com.example.timechecker.TimePickerFragment"
        android:label="@string/app_name" >
    </activity>
</application>

</manifest>

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/whatTime"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="What time is it?"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/check"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="checkTime"
    android:text="Check" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

啊,我知道这里发生了什么。

public FragmentManager getSupportFragmentManager() {
    return null;
}
public void checkTime(View view) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");

}

您重写getSupportFragmentManager以返回null,然后调用该方法来显示对话框,看起来您是故意创建了nullpointer? = d