在一个空对象引用android.content.Context.getPackageName() [英] android.content.Context.getPackageName()' on a null object reference

查看:8474
本文介绍了在一个空对象引用android.content.Context.getPackageName()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用从其他服务类服务类,但我得到这个错误:

  

android.content.Context.getPackageName()对空对象引用

你知道我是如何调用服务从其他服务?

当我安装我的应用程序的手机第一,一个BroadcastReceiver开始报警类别和报警类别,我要开始在ReadGmail另一个service()方法。但我得到的空对象引用错误。

下面是我的code:

 公共类报警延伸服务{
私人字符串用户名;
私人字符串密码;
私人字符串receivingHost;
上下文语境;

公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
    最终的处理程序处理程序=新的处理程序();
    TimerTask的doAsynchronousTask =新的TimerTask(){
        @覆盖
        公共无效的run(){
        handler.post(新的Runnable(){
                公共无效的run(){
                    。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建();
                    StrictMode.setThreadPolicy(政策);
                     字符串senderPassword =新的String(密码);
                        字符串senderUserName =新的String(username@gmail.com);
                        报警newGmailCli​​ent =新的警报();
                        newGmailCli​​ent.setAccountDetails(senderUserName,senderPassword);
                        newGmailCli​​ent.readGmail();
                }
            });
        }
    };
    定时器定时=新的Timer();
    timer.schedule(doAsynchronousTask,10,120000);

     返回super.onStartCommand(意向,标志,startId);


  };



公共无效setAccountDetails(用户名字符串,字符串密码){
    this.userName =用户名; //发件人的电子邮件也可作为用户名使用
    this.password =密码;
}


公共无效readGmail(){
    this.receivingHost =imap.gmail.com; //为IMAP协议
    属性props2 = System.getProperties();
    props2.setProperty(mail.store.protocol,IMAPS);
    会议届会议2 = Session.getInstance(props2,NULL);
        尝试 {
                店中店= session2.getStore(IMAPS);
                store.connect(this.receivingHost,this.userName,this.password);
                文件夹文件夹= store.getFolder(收件箱); //获取收件箱
                folder.open(Folder.READ_ONLY); //打开文件夹为只读
                留言信息[] = folder.getMessages();
                字符串键=嗨;
                串标的;
                的for(int i = 0; I< message.length;我++){
                    的System.out.println(消息[I] .getSubject());
                    主题=信息[我] .getSubject();

                 如果(subject.equals(键)){
                     的System.out.println(内部);
意图mTutorial =新的意图(Alarm.this,LaunchActivity.class);

this.startService(mTutorial);

//我想调用服务类在这里。 LaunchActivity是我的服务类。



                 }
                    //Log.d(message[i].getSubject(),消息[I] .getSubject());
                }
                folder.close(真正的);
                store.close();
        }赶上(例外五){
               的System.out.println(e.toString());
        }
}

@覆盖
公众的IBinder onBind(意向意图){
    // TODO自动生成方法存根
    返回null;
}
公共无效的onDestroy(){


    // TODO自动生成方法存根
    super.onDestroy();
    Log.d(,FirstService毁);
}


}
 

解决方案

这是你的问题:

 报警newGmailCli​​ent =新的警报();
 

警报类扩展服务。这是一个Android 服务。你不能用创建一个Android组件的一个实例。只有机器人可以创建Android组件。如果你想开始一个服务,你叫 startService

为什么要开始另服务?请解释一下。

I'm trying to call a service class from another service class but I get this error:

android.content.Context.getPackageName() on a null object reference

Do you know how I call service from another service?

When I setup my app to phone first, a broadcastreceiver is starting Alarm class and in Alarm class, I want to start another service in ReadGmail() method. But I get that null object reference error.

Here is my code:

public class Alarm extends Service {
private String userName;
private String password;
private String receivingHost;
Context context;

public int onStartCommand(Intent intent, int flags, int startId) {
    final Handler handler = new Handler();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
        handler.post(new Runnable() {
                public void run() {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                    StrictMode.setThreadPolicy(policy);
                     String senderPassword=new String("password");
                        String senderUserName=new String("username@gmail.com");
                        Alarm newGmailClient=new Alarm();
                        newGmailClient.setAccountDetails(senderUserName, senderPassword);
                        newGmailClient.readGmail();
                }
            });
        }
    };
    Timer timer = new Timer();
    timer.schedule(doAsynchronousTask, 10, 120000);

     return super.onStartCommand(intent, flags, startId);


  };



public void setAccountDetails(String userName,String password){  
    this.userName=userName;//sender's email can also use as User Name
    this.password=password;
}


public void readGmail(){
    this.receivingHost="imap.gmail.com";//for imap protocol
    Properties props2=System.getProperties();
    props2.setProperty("mail.store.protocol", "imaps");
    Session session2=Session.getInstance(props2, null);
        try {
                Store store=session2.getStore("imaps");
                store.connect(this.receivingHost,this.userName, this.password);
                Folder folder=store.getFolder("INBOX");//get inbox
                folder.open(Folder.READ_ONLY);//open folder only to read
                Message message[]=folder.getMessages();
                String key= "Hey";
                String subject;
                for(int i=0;i<message.length;i++){
                    System.out.println(message[i].getSubject());
                    subject=message[i].getSubject();

                 if(subject.equals(key)){
                     System.out.println("inside");
Intent mTutorial = new Intent(Alarm.this, LaunchActivity.class); 

this.startService(mTutorial); 

//I want to call service class in here. LaunchActivity is my service class.



                 }
                    //Log.d(message[i].getSubject(),message[i].getSubject());
                }
                folder.close(true);
                store.close();
        } catch (Exception e) {
               System.out.println(e.toString());
        }
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
public void onDestroy() {


    // TODO Auto-generated method stub
    super.onDestroy();
    Log.d("", "FirstService destroyed");
}


}

解决方案

This is your problem:

Alarm newGmailClient=new Alarm();

Your Alarm class extends Service. It is an Android Service. You cannot create an instance of an Android component with new. Only Android can create Android components. If you want to start a Service, you call startService.

Why do you want to start another Service? Please explain.

这篇关于在一个空对象引用android.content.Context.getPackageName()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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