gpt4 book ai didi

java - 从另一个 Activity 获取 String 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 13:42:35 26 4
gpt4 key购买 nike

我正在使用 javamail 创建邮件客户端。我设置了一个存储邮件和密码的登录 Activity 。我有在名为 Data.java 的类中发送邮件的代码。问题是我无法从那里获取 SharedPreferences,因为它不是 Activity 并且它不扩展 PreferenceActivity。我需要使用 Looper.prepare() 因为否则它会引发另一个异常。我正在尝试从 MainActivity 获取共享首选项,但当我尝试打印它时,我收到了 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();
}
}
}

日志猫:

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)

最佳答案

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

abnull。这就是 NPE 的原因。

它们为空的原因:

MainActivity d = new MainActivity();

切勿使用new实例化 Activity 。 (使用 Intent 代替。但您不需要在此处实例化 Activity 。)

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

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

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

关于java - 从另一个 Activity 获取 String 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20593865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com