在DialogFrament中显示TimePicker时,警报应用程序崩溃

时间:2017-10-06 04:43:18

标签: java android

我正在尝试创建一个闹钟应用程序,在TimePicker中显示DialogFragment,但每次单击以打开时,我的应用程序都会崩溃。

以下是我找到此代码来源的链接:http://codingconnect.net/android-application-creates-alarm-clock/

另外,如何在我的数据库中存储Time格式的对象?

对话

package com.example.asus.test8;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.util.Calendar;

public class dialog extends DialogFragment
{


TimePicker alarmTimePicker;
PendingIntent pendingIntent;
AlarmManager alarmManager;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.dialog,container,false);
    alarmTimePicker = (TimePicker) 
getActivity().findViewById(R.id.timePicker);
    alarmManager = (AlarmManager) 
getActivity().getSystemService(Context.ALARM_SERVICE);
    return view;
}
public void OnToggleClicked(View view)
{
long time;
if (((ToggleButton) view).isChecked())
{
    Toast.makeText(getActivity(), "ALARM ON", Toast.LENGTH_SHORT).show();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
    calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
    Intent intent = new Intent(getActivity(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);

    time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
    if(System.currentTimeMillis()>time)
    {
        if (calendar.AM_PM == 0)
            time = time + (1000*60*60*12);
        else
            time = time + (1000*60*60*24);
    }
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, 
pendingIntent);
}
else
{
    alarmManager.cancel(pendingIntent);
    Toast.makeText(getActivity(), "ALARM OFF", Toast.LENGTH_SHORT).show();
}
}
}

MainActivity

package com.example.asus.test8;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView l;
    l= (ListView) findViewById(R.id.listView);

    FloatingActionButton fab = (FloatingActionButton) 
findViewById(R.id.fab);

}
public void click(View v)
{

FragmentManager fm=getSupportFragmentManager();
dialog dg=new dialog();
dg.show(fm,"TAG");
}

AlarmReceiver

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.widget.Toast;

public class AlarmReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
    Toast.makeText(context, "Alarm! Wake up! Wake up!", 
Toast.LENGTH_LONG).show();
    Uri alarmUri = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null)
    {
        alarmUri = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
    ringtone.play();

}
}

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asus.test8">


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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.example.asus.test8.AlarmReceiver" >
    </receiver>
</application>

</manifest>

1 个答案:

答案 0 :(得分:1)

尝试添加<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>,不要忘记YourActivity的检查请求权限。