我有这个有效的通知代码,但是每当我点击通知时,应用程序就会关闭,我不知道为什么。我想当我点击通知时取消它,如果可能的话,我也想让它定时,你能帮我吗?
这是代码:
package egypt.interfaceAct;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class Notifications extends Activity {
NotificationManager nm;
int uniqueID = 13393;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle programNum = getIntent().getExtras();
final String body = programNum.getString("Activity");
final String startTime = programNum.getString("Start");
final String endTime = programNum.getString("End");
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancel(uniqueID);
String notify = body + "Start at: " + startTime + " to: " + endTime;
Intent intent = new Intent(this,ProgramsList2.class);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
//String body = (String) ((TextView)parent.getChildAt(position)).getText();
String title = "Egypt On The Go";
//String time = body + "\n start at:" + startTime+ "\n end at:" + endTime.get(position);
Notification n = new Notification(R.drawable.egypt, notify, System.currentTimeMillis());
n.setLatestEventInfo(this, title, notify, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);
n.flags = Notification.FLAG_AUTO_CANCEL;
//Intent i = new Intent(this, ProgramsList2.class);
//startActivity(i);
finish();
}
}
这是 logCat 也许它会有所帮助:
06-25 19:06:19.818: E/AndroidRuntime(883): Uncaught handler: thread main exiting due to uncaught exception
06-25 19:06:19.824: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{egypt.interfaceAct/egypt.interfaceAct.ProgramsList2}: java.lang.NullPointerException
06-25 19:06:19.824: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-25 19:06:19.824: E/AndroidRuntime(883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-25 19:06:19.824: E/AndroidRuntime(883): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-25 19:06:19.824: E/AndroidRuntime(883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-25 19:06:19.824: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 19:06:19.824: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:123)
06-25 19:06:19.824: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-25 19:06:19.824: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 19:06:19.824: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:521)
06-25 19:06:19.824: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-25 19:06:19.824: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-25 19:06:19.824: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)
06-25 19:06:19.824: E/AndroidRuntime(883): Caused by: java.lang.NullPointerException
06-25 19:06:19.824: E/AndroidRuntime(883): at egypt.interfaceAct.ProgramsList2.onCreate(ProgramsList2.java:68)
06-25 19:06:19.824: E/AndroidRuntime(883): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-25 19:06:19.824: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-25 19:06:19.824: E/AndroidRuntime(883): ... 11 more
这是 ProgramsList2 的代码
package egypt.interfaceAct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import egypt.database.EgyptDataSource;
import egypt.database.Hotel;
import egypt.database.MySQLiteHelper;
import egypt.database.TA;
import egypt.database.TouringPrograms;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
public class ProgramsList2 extends Activity{
EgyptDataSource datasource;
MySQLiteHelper sqlHelper;
NotificationManager nm;
static final int uniqueID = 13992;
//ArrayAdapter<TouringPrograms> touringProgramAdapter;
final String ACTIVITY_KEY = "activity";
final String STIME_KEY = "Stime";
final String ETIME_KEY = "Etime";
ListView list;
ArrayList<Map<String, String>> al;
Map<String, String> map;
SimpleAdapter sa;
String[] from = {ACTIVITY_KEY, STIME_KEY, ETIME_KEY};
int[] to = {R.id.tableTextID, R.id.tableText2ID, R.id.tableText3ID};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.programsmain);
datasource = new EgyptDataSource(this);
datasource.open();
list = (ListView) findViewById(R.id.programsMainList);
al = new ArrayList<Map<String,String>>();
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancel(uniqueID);
Bundle programNum = getIntent().getExtras();
final String progNum = programNum.getString("ProgNum");
final String dayNum = programNum.getString("DayNum");
List<TouringPrograms> Activityvalues = datasource.getTouringPrograms(progNum, dayNum);
final List<TouringPrograms> STimevalues = datasource.getTouringProgramsStartTime(progNum, dayNum);
final List<TouringPrograms> ETimevalues = datasource.getTouringProgramsEndTime(progNum, dayNum);
Object[] ActivityArray = Activityvalues.toArray();
Object[] STimeArray = STimevalues.toArray();
Object[] ETimeArray = ETimevalues.toArray();
for (int i = 0; i < Activityvalues.size(); i++) {
map = new HashMap<String, String>();
map.put(ACTIVITY_KEY, ActivityArray[i].toString());
map.put(STIME_KEY, STimeArray[i].toString());
map.put(ETIME_KEY, ETimeArray[i].toString());
al.add(map);
}
final List<TouringPrograms> Activityvalues2 = datasource.getTouringPrograms(progNum, dayNum);
sa = new SimpleAdapter(this, al, R.layout.programstable, from, to);
list.setAdapter(sa);
//list.setTextFilterEnabled(true);
//ListView lv = getApplication()
list.setOnItemLongClickListener(new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
final String taName = Activityvalues2.get(position).toString();
List<TA> HI = datasource.getTAHistoricalInfo(taName);
if (HI.size()==0)
{
}
else
{
String historical = "Historical Info: " + TextUtils.join(", ", HI);
List<TA> location = datasource.getTALocation(taName);
String loc = "Location: " + TextUtils.join(", ", location);
List<TA> tatype = datasource.getTAType(taName);
String type = "Type: " + TextUtils.join(", ", tatype);
List<TA> photo = datasource.getTAPhoto(taName);
final String pho = photo.get(0).toString();
List<TA> latitude = datasource.getTALat(taName);
final String lat = latitude.get(0).toString();
List<TA> longitude = datasource.getTALong(taName);
final String lon = longitude.get(0).toString();
final String startTime = STimevalues.get(position).toString();
final String endTime = ETimevalues.get(position).toString();
//Toast.makeText(hotelSearchList.this, "The hotel clicked is " + hotelName, Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(ProgramsList2.this);
builder.setTitle("Information About " + taName);
builder.setMessage(loc + "\n" + type + "\n" + historical + "\n");
builder.setPositiveButton("View Photo", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i = new Intent(ProgramsList2.this, imageV.class);
i.putExtra("photo", pho);
i.putExtra("name", taName);
startActivity(i);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//dialog.dismiss();
Intent i = new Intent(ProgramsList2.this, Notifications.class);
i.putExtra("Activity", taName);
i.putExtra("Start", startTime);
i.putExtra("End", endTime);
startActivity(i);
}
});
builder.setNeutralButton("Map it", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Intent i = new Intent(ProgramsList2.this, mapActivity.class);
i.putExtra("latitude", lat);
i.putExtra("longitude", lon);
startActivity(i);
}
});
builder.setCancelable(true);
builder.show();
}
return false;
}});
//list.setOnItemClickListener((OnItemClickListener) this);
}
}
太感谢了