Android中的NullPointerException:如何设置上下文? [英] NullPointerException in android: how to set context?

查看:111
本文介绍了Android中的NullPointerException:如何设置上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决此错误?

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
ACTIVITY.onNewIntent(Adult1Activity.java:243)

这是包含此错误的方法:

This is the method that includes this error:

private void ExtendedNotification(String time) {
    Intent resultIntent = new Intent(this, Adult1Activity.class);

    resultIntent.putExtra("A", "restore");
    PendingIntent restoreIntent = PendingIntent.getActivity(Adult1Activity.this, 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    resultIntent.putExtra("A", "close");
    PendingIntent closeIntent = PendingIntent.getActivity(Adult1Activity.this, 2, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

     final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Adult1Activity.this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("App Name")
            .setContentText(time)
            .setAutoCancel(true)
            .addAction(new NotificationCompat.Action(R.mipmap.ic_launcher, "Restore", restoreIntent))
            .addAction(new NotificationCompat.Action(R.mipmap.ic_launcher, "Close", closeIntent))
            .setContentIntent(restoreIntent);

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

    Notification notification = notificationBuilder.build();
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

    notificationManager.notify(0, notification);
}
@Override
protected void onNewIntent(Intent intent) {
    switch (intent.getStringExtra("A")) {
        case "restore":
            tv.setText(timeString);
            break;

        case "close":
            countDownTimer.cancel();
            isRunning = false;
            notificationManager.cancel(0);
            CloseApp();
            break;
    }

我认为问题不在其中使用上下文.

I think the problem is not using context in it.

我的应用程序中有一个带有StopBTN的计数器,我用2个按钮创建了通知.其中之一是RestoreBTN,它显示了我的ACTIVITY类.现在,当我单击StopBTN时,将显示此错误.

There is a Counter with StopBTN in my app and I create notification with 2 buttons. One of them is RestoreBTN that it shows my ACTIVITY class. Now as I click on StopBTN, this error shows.

编辑: handleIntent 方法中存在NullPointer错误.

EDIT: There is NullPointer error in handleIntent method.

private void handleIntent(Intent intent) {
    final String a = intent.getStringExtra(EXTRA_NOTE);
    if (a != null) {
        switch (a) {
            case NOTE_RESTORE:
                tv.setText(timeString);
                notificationManager.cancel(notification_id);
                break;

            case NOTE_CLOSE:
                countDownTimer.cancel();
                isRunning = false;
                notificationManager.cancel(notification_id);
                break;
        }
    }

推荐答案

您可能想为额外的键设置一个常量,以避免输入错误或类似A的内容,但输入错误的语言.还有动作的常量

You might want to set a constant for extra key, to avoid typos or something that looks like A but typed in wrong language. Also it's good to have constants for actions

private static final String EXTRA_A = "A";
private static final String A_RESTORE = "restore";
private static final String A_CLOSE = "close";

我不知道您是否愿意,但是不手动调用 onNewIntent().如果在单击通知时您的活动被破坏,则可能不会调用 onNewIntent().您需要输入两个条目来检查意图,一个输入活动已经运行时的状态,您会得到 onNewIntent(),另一个输入是否已重新创建并且您的意图是onCreate()

I don't know if you do, but not call onNewIntent() manually. Also onNewIntent() might not be called if your Activity is destroyed when you click on notification. You need to make two entries to check for intent, one for when activity is already running and you get onNewIntent(), and one if it's re-created and you get intent in onCreate()

@Override
protected void onCreate(Bundle b) {
    super.onCreate(b);
    // init views and stuff, and in final onCreate line handle the intent
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent); // Make sure to call super
    handleIntent(intent);
}

崩溃表示您的Intent确实不包含多余的"A".您不能在空字符串上使用 switch ,因此,如果您检查A是否为空,那将是安全的.

The crash means that your Intent does not really contain extra "A". You cannot use switch on a null string, thus you'd be safe if you check if A is null.

private void handleIntent(Intent intent) {
    final String a = intent.getStringExtra(EXTRA_A);
    if (a != null) {
        switch (a) {
           case A_RESTORE:
               ...

           case A_CLOSE:
               ...

请勿为不同的 PendingIntents 修改相同的意图.它们是可变的,并且 getActivity()不会创建防御性副本.

Do not modify the same intent for different PendingIntents. They are mutable and getActivity() does not create a defensive copy.

final Intent resultIntentRestore = new Intent(this, Adult1Activity.class);
resultIntentRestore.putExtra(EXTRA_A, A_RESTORE);
PendingIntent restoreIntent = PendingIntent.getActivity(Adult1Activity.this, 
        0, resultIntentRestore, PendingIntent.FLAG_UPDATE_CURRENT);

final Intent resultIntentClose = new Intent(this, Adult1Activity.class);
resultIntentClose.putExtra(EXTRA_A, A_CLOSE);
PendingIntent closeIntent = PendingIntent.getActivity(Adult1Activity.this, 
        2, resultIntentClose, PendingIntent.FLAG_UPDATE_CURRENT);

这篇关于Android中的NullPointerException:如何设置上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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