为什么通知方法为消息获取空值? [英] Why is the notification method getting null value for message?

查看:83
本文介绍了为什么通知方法为消息获取空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的2节课。 ViewTask类在broadcastmanager clas中调用传递参数的notification方法。但是参数在通知方法中显示为null。请注意为什么我从这里传递的信息没有传递给通知方法。





Below are my 2 classes. The ViewTask class calls the notification method in broadcastmanager clas passing a parameter. But the parameter is shown null inside the notification method. Pleas advice why the message I pass from here is not passed onto notification method.


package com.example.tasker;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class BroadcastManager extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		this.Notification(context, "Get the task from database");
	}

	public void Notification(Context context,String message) {
		System.out.println("Message is "+message);

        // Set Notification Title
        String strtitle = "To Do Notification";
        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(context, NotificationView.class);
        // Send data to NotificationView Class
        intent.putExtra("title", strtitle);
        intent.putExtra("text", message);
        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
 
        // Create Notification using NotificationCompat.Builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context)
                // Set Icon
                .setSmallIcon(R.drawable.ic_launcher)
                // Set Ticker Message
                .setTicker(message)
                // Set Title
                .setContentTitle("To Do App notification")
                // Set Text
                .setContentText(message)
                // Add an Action Button below Notification
                .addAction(R.drawable.ic_launcher, "Action Button", pIntent)
                // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                // Dismiss Notification
                .setAutoCancel(true);
 
        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        notificationmanager.notify(0, builder.build());
		}
	
}








package com.example.tasker;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ViewTask extends Activity {
	protected TaskerDbHelper db;
	List<task> list;
	MyAdapter adapt;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_task);
		
		db = new TaskerDbHelper(this);
		
		list = db.getAllTasks();
		adapt = new MyAdapter(this, R.layout.list_inner_view, list);
		ListView listTask = (ListView) findViewById(R.id.listView1);
		listTask.setAdapter(adapt);
//		while(true)
		
		new GetCDates().execute("");
		
	}
	
	
	
	
	private class GetCDates extends AsyncTask<string,>
	{
		Task current;
		
		String date,notifyDate;
		int day,month,year;
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub   
	                try {
	                	Calendar c = Calendar.getInstance(); 
	            		day = c.get(Calendar.DAY_OF_MONTH);
	            		month=c.get(Calendar.MONTH);
	            		year=c.get(Calendar.YEAR);
	            		
	            		String cdate=day+"-"+month+"-"+year;
	            		
	            		System.out.println("today is "+cdate);
	                  loop:for(int j=0;j<list.size();j++)>
	                    {
	                    	current = list.get(j);
	                    	date=current.getDate();
	                    	System.out.println("date is "+date);
	                    	if(date.equals(cdate))
	                    	{
	                    		notifyDate=date;
	                    		break loop;
	                    	}
	                    }
	            		String taskArray[]=db.getTaskForTheDay(notifyDate);
	            		BroadcastManager b=new BroadcastManager();
	            		for(int i=0;i<taskarray.length;i++)>
	                    b.Notification(ViewTask.this, taskArray[i]);
//	            		PackageManager pm = ViewTask.this.getPackageManager();
//	            	      ComponentName componentName = new ComponentName(
//	            	              ViewTask.this, BroadcastManager.class);
//	            	      pm.setComponentEnabledSetting(componentName,
//	            	              PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
//	            	              PackageManager.DONT_KILL_APP);
//	            	      Toast.makeText(getApplicationContext(),
//	            	              "Broadcast Receiver Started", Toast.LENGTH_LONG)
//	            	              .show();
	                    Thread.sleep(2000);
	                } catch (InterruptedException e) {
	                    // TODO Auto-generated catch block
	                    e.printStackTrace();
	                }
			return date;
			
		}
		
	}

	public void addTaskNow(View v) {
		

        
        
		
		EditText t = (EditText) findViewById(R.id.editText1);
		EditText t2=(EditText)findViewById(R.id.editText2);
		String s = t.getText().toString();
		String s2 = t2.getText().toString();
		if (s.equalsIgnoreCase("")) {
			Toast.makeText(this, "enter the task description first!!",
					Toast.LENGTH_LONG);
		} 
		else {
			
			if (s.equalsIgnoreCase("")) {
				Toast.makeText(this, "enter the date!!",
						Toast.LENGTH_LONG);
			}
				else{
			Task task = new Task(s, 0,s2);
			db.addTask(task);
			Log.d("tasker", "data added");
			t.setText("");
			t2.setText("");
			adapt.add(task);
			adapt.notifyDataSetChanged();
				}
		}

	}

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

	private class MyAdapter extends ArrayAdapter<task> {

		Context context;
		List<task> taskList = new ArrayList<task>();
		int layoutResourceId;

		public MyAdapter(Context context, int layoutResourceId,
				List<task> objects) {
			super(context, layoutResourceId, objects);
			this.layoutResourceId = layoutResourceId;
			this.taskList = objects;
			this.context = context;
		}

		/**
		 * This method will DEFINe what the view inside the list view will
		 * finally look like Here we are going to code that the checkbox state
		 * is the status of task and check box text is the task name
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			CheckBox chk = null;
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.list_inner_view,
						parent, false);
				chk = (CheckBox) convertView.findViewById(R.id.chkStatus);
				convertView.setTag(chk);

				chk.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						CheckBox cb = (CheckBox) v;
						Task changeTask = (Task) cb.getTag();
						changeTask.setStatus(cb.isChecked() == true ? 1 : 0);
						db.updateTask(changeTask);
						Toast.makeText(
								getApplicationContext(),
								"Clicked on Checkbox: " + cb.getText() + " is "
										+ cb.isChecked(), Toast.LENGTH_LONG)
								.show();
					}

				});
			} else {
				chk = (CheckBox) convertView.getTag();
			}
			Task current = taskList.get(position);
			chk.setText(current.getTaskName()+" - "+current.getDate());
			chk.setChecked(current.getStatus() == 1 ? true : false);
			chk.setTag(current);
			Log.d("listener", String.valueOf(current.getId()));
			return convertView;
		}

	}

}

推荐答案

public void Notification(Context context,String message) {





N otification ??请考虑一下。



您应该在那里放一些日志来查看您的数据库请求正在提供什么。



http://developer.android.com/reference/android/util/Log.html [ ^ ]



我打赌已经给出了答案。



那么你可以去调试一下,看看它失败了。

http://developer.android.com/tools/debugging/index.html [ ^ ]



您可以在NullpointerException上设置一个断点 - 如果你得到一个。



Notification?? Please think about that.

You should put some logging in there to see what your db-request is delivering.

http://developer.android.com/reference/android/util/Log.html[^]

I bet that already gives answers.

Then you can go for some debugging to see where it fails.
http://developer.android.com/tools/debugging/index.html[^]

You can set a break point on the NullpointerException - if you get one.


问题是我在这里添加的arrayList。在数据库创建中我指定了它因为大小为1000.因此,这里调用通知的for循环给出了错误。将它更改为列表工作。
The problem is the arrayList I have added here.In the database creation class I specified it as size 1000. Therefore the for loop here calling notification is giving error.Changing it to a List worked.


这篇关于为什么通知方法为消息获取空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆