rockers rockers - 3 months ago 13
Android Question

Error in Starting Dialog from Notification

I am creating an application in which I have an Alarm which when goes off creates
Notification and if you click on that notification a Dialog will come with a simple OK and Cancel button:

Source code till now :

AlarmActivity.java

package com.example.alarmclocktext;

import android.support.v4.app.DialogFragment;


import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class AlarmActivity extends Activity {
int hr = 0;
int min = 0;
int sec = 0;
int result = 1;

AlarmManager alarmManager;
PendingIntent pendingIntent;
BroadcastReceiver mReceiver;

EditText ethr;
EditText etmin;
EditText etsec;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
ethr = (EditText) findViewById(R.id.ethr);
etmin = (EditText) findViewById(R.id.etmin);
etsec = (EditText) findViewById(R.id.etsec);
RegisterAlarmBroadcast();

}
public void onClickSetAlarm(View v) {
String shr = ethr.getText().toString();
String smin = etmin.getText().toString();
String ssec = etsec.getText().toString();

if(shr.equals(""))
hr = 0;
else {
hr = Integer.parseInt(ethr.getText().toString());
hr=hr*60*60*1000;
}

if(smin.equals(""))
min = 0;
else {
min = Integer.parseInt(etmin.getText().toString());
min = min*60*1000;
}

if(ssec.equals(""))
sec = 0;
else {
sec = Integer.parseInt(etsec.getText().toString());
sec = sec * 1000;
}
result = hr+min+sec;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), result , pendingIntent);

}

@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);//unregister
super.onDestroy();
}


private void RegisterAlarmBroadcast() {
// TODO Auto-generated method stub
mReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("Alarm set off");
builder.setContentText("You have to Eat Something");
builder.setAutoCancel(true);
Intent dialogIntent=new Intent(getBaseContext(), Dialog.class);

TaskStackBuilder stackBuilder=TaskStackBuilder.create(getBaseContext());
stackBuilder.addParentStack(Dialog.class);
stackBuilder.addNextIntent(dialogIntent);
PendingIntent resDialogIntent=stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resDialogIntent);

NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(0,builder.build());
}

};

registerReceiver(mReceiver, new IntentFilter("sample"));// register in oncreate
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("sample"), 0);
alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.alarm, menu);
return true;
}

private void UnregisterAlarmBroadcast() {
alarmManager.cancel(pendingIntent);
getBaseContext().unregisterReceiver(mReceiver);
}
}


Dialog.java

package com.example.alarmclocktext;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

public class Dialog extends DialogFragment {

@Override
public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub

AlertDialog.Builder firstDialog=new AlertDialog.Builder(getActivity());
firstDialog.setMessage(R.string.text_show);
firstDialog.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
}).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});
return firstDialog.create();

}

}


Now whenever I start my application and set alarm time the alarm goes off and create notification.Till now there is no problem but when I click on the notification the application crashes and produce this error as below:

08-30 03:49:04.676: E/AndroidRuntime(1362): FATAL EXCEPTION: main
08-30 03:49:04.676: E/AndroidRuntime(1362): Process: com.example.alarmclocktext, PID: 1362
08-30 03:49:04.676: E/AndroidRuntime(1362): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.alarmclocktext/com.example.alarmclocktext.Dialog}: java.lang.ClassCastException: com.example.alarmclocktext.Dialog cannot be cast to android.app.Activity
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.os.Handler.dispatchMessage(Handler.java:102)
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.os.Looper.loop(Looper.java:136)
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-30 03:49:04.676: E/AndroidRuntime(1362): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 03:49:04.676: E/AndroidRuntime(1362): at java.lang.reflect.Method.invoke(Method.java:515)
08-30 03:49:04.676: E/AndroidRuntime(1362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-30 03:49:04.676: E/AndroidRuntime(1362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-30 03:49:04.676: E/AndroidRuntime(1362): at dalvik.system.NativeStart.main(Native Method)
08-30 03:49:04.676: E/AndroidRuntime(1362): Caused by: java.lang.ClassCastException: com.example.alarmclocktext.Dialog cannot be cast to android.app.Activity
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-30 03:49:04.676: E/AndroidRuntime(1362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
08-30 03:49:04.676: E/AndroidRuntime(1362): ... 11 more


Now what should I do to remove this error ? Any help

Answer

See the Dialog.class which you want to start is not an Activity .It is extending DialogFragment because which you practically cannot open a dialog via onclick of a notfication.

There is no way in android.Instead you should do

<activity android:theme="@android:style/Theme.Dialog">

and move everything you want to display here and call this activity by pending intent.