尝试从广播接收器开始活动 [英] Trying to start an activity from broadcast receiver

查看:78
本文介绍了尝试从广播接收器开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个锁屏.当我尝试从broadcastReceiver启动 com.fira.locker.LockScreenActivity 时,出现一个错误.错误提示:

I'm trying to create a lockscreen. When I try to start the com.fira.locker.LockScreenActivity from the broadcastReceiver, I just get an error. The error says:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.setFlags(int)' on a null object reference

这是我的代码:

package com.fira.locker;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by Johannett321 on 10/04/16.
 */
public class LockScreenReceiver extends BroadcastReceiver {

    public String screenlockedNumber;

    @Override
    public void onReceive(Context context, Intent intent) {
        //start activity
        Intent i = new Intent();
        i.setClassName("com.fira.locker",     "com.fira.locker.LockScreenActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
        }
}

推荐答案

为什么不像这样开始简单的意图.

Why are you not starting simple intent like this..

startActivity(new Intent(this, LockScreenActivity.class));
finish();

或者您可以尝试一下.

Intent i = new Intent(context,LockScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

这篇关于尝试从广播接收器开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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