主要和空指针异常中的fata异常

时间:2013-12-03 10:13:40

标签: android nullpointerexception fatal-error

我创建了3个活动 在First Activity中有两个按钮,当我点击startNotification它开始通知时 并记下开始时间。当我们点击通知时,它打开secondActivity并发送注释 当我现在去第二个活动然后从第三个活动转到第三个活动时,它会给出错误NullPointerException

按钮点击

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            String MyText = "Reminder";
            Notification mNotification = new Notification(R.drawable.ic_launcher, MyText, System.currentTimeMillis() );
            int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
            String MyNotificationTitle = "Medicine!";
            String MyNotificationText  = "Don't forget to take your medicine!";
            MyIntent = new Intent(MainActivity.this,SecondActivity.class);
            MyIntent.putExtra("s1",System.currentTimeMillis());
            //PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),UNIQUE_INT_PER_CALL,MyIntent,0);

            PendingIntent contentIntent = PendingIntent.getActivity(context,iUniqueId,MyIntent,0);
            //A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent

            mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, contentIntent);
            mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0,   mNotification);

第二项活动

    Intent i=this.getIntent();
    if(i!=null)
    {
        **data=i.getStringExtra("summar");**
        if(data.equalsIgnoreCase("MainActivity"))
        {

            z=i.getExtras().getLong("s1");  
            int minutesZ = (int) ((z /(1000*60)) % 60);
            int minutesZ1 = (int) ((System.currentTimeMillis() /(1000*60)) % 60);
            Log.v("SecondTime",""+minutesZ);
            Log.v("CurrentTime",""+minutesZ1);
                x1=i.getExtras().getLong("s1");
            p=System.currentTimeMillis()-x1;
            int minutes = (int) ((p /(1000*60)) % 60);
            text1.setText(""+minutes);  
        }

        if(data.equalsIgnoreCase("ThirdActivity"))
        {
            //do something here
            Toast.makeText(SecondActivity.this,"hiii",2000).show();
        }
    }

第三项活动

 b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent i=new Intent(ThirdActivity.this,SecondActivity.class);
            startActivity(i);
            i.putExtra("summar","ThirdActivity");
        }
    });

1 个答案:

答案 0 :(得分:1)

在你的第三个活动中,你正在启动意图,然后将额外的内容加入其中..而是尝试:

第三项活动

 b1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Intent i=new Intent(ThirdActivity.this,SecondActivity.class);
        i.putExtra("summar","ThirdActivity");
        startActivity(i);

    }
});

并且在您的第二个活动中,尝试在您的情况下进行额外检查(在意图中使用 hasExtra()选项):

Intent i=this.getIntent();
    if(i!=null)
    {
        if(i.hasExtra("summar"))        //---Might be useful if the intent doesn't have that extra
             data=i.getStringExtra("summar");

    if(data.equalsIgnoreCase("MainActivity"))
    {

        z=i.getExtras().getLong("s1");  
        int minutesZ = (int) ((z /(1000*60)) % 60);
        int minutesZ1 = (int) ((System.currentTimeMillis() /(1000*60)) % 60);
        Log.v("SecondTime",""+minutesZ);
        Log.v("CurrentTime",""+minutesZ1);
            x1=i.getExtras().getLong("s1");
        p=System.currentTimeMillis()-x1;
        int minutes = (int) ((p /(1000*60)) % 60);
        text1.setText(""+minutes);  
    }

    if(data.equalsIgnoreCase("ThirdActivity"))
    {
        //do something here
        Toast.makeText(SecondActivity.this,"hiii",2000).show();
    }
}