从另一个活动获取String时出现NullPointerException

时间:2013-12-15 11:16:00

标签: java android nullpointerexception sharedpreferences

我正在使用javamail创建一个Mail客户端。我已经设置了一个存储邮件和密码的登录活动。我有一个代码,它在一个名为Data.java的类中发送邮件。问题是我无法从那里获得SharedPreferences,因为它不是一个Activity而且它不会扩展PreferenceActivity。我需要使用Looper.prepare(),否则会引发另一个异常。我正在尝试从MainActivity获取共享Prefs,但是当我尝试打印它时,我得到一个NullPointerException。我的MainActivity代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences prefs = getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
        mail1 = prefs.getString("email", null);
        pass = prefs.getString("pass", null);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    public String getMail1(){
        return mail1;
    }
    public String getPass(){
        return pass;
    }

Data.java代码:

public class Data{

    String a;
    String b;

    private void getEverything(){
        Looper.prepare();
        MainActivity d = new MainActivity();
        a = d.getMail1();
        b = d.getPass();
    }


    public void sendMail(){
        getEverything();
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(a, b);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(a));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(gmail.to));
            message.setSubject(gmail.subject);
            message.setText(gmail.text);

            Transport.send(message);
            System.out.println(a);
            System.out.println(b);
            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

logcat的:

java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
            at com.test.email.Data.sendMail(Data.java:61)
            at com.test.email.gmail.doInBackground(gmail.java:40)
            at com.test.email.gmail.doInBackground(gmail.java:19)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:1)

System.out.println(a);
System.out.println(b);

abnull。这就是NPE的原因。

他们是null的原因:

MainActivity d = new MainActivity();

永远不要使用new实例化活动。 (请改用Intent。但您不需要在此处实例化活动。)

因此,您在此处获取的活动对象与您在其中设置变量值的活动不同。

相反,在Data类中传递您需要的任何值作为方法参数,例如

public void sendMail(String email, String password) { ...