【发布时间】:2015-07-10 13:32:32
【问题描述】:
我正在开发一个 Android 应用程序,我正在尝试在登录后从服务器获取数据,但无论我怎么做,我都失败了好几个小时。 .请让我知道我在这里做错了什么。
错误日志:
07-10 15:24:09.136 4588-4588/com.example.TestLunch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.TestLunch, PID: 4588
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:139)
at com.example.TestLunch.Login.Login$3.onClick(Login.java:104)
以上是因为我试图从服务器打印回复。
public class Login extends Activity {
RestTemplate rest = StaticRestTemplate.getRest();
protected volatile String reply;
// below code is inside the onCreate method
Button loginButton = (Button) findViewById(R.id.LoginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!(v == null)) {
EditText userEmail = (EditText) findViewById(R.id.usernameText);
EditText userPassword = (EditText) findViewById(R.id.PasswordField);
if (!(userEmail.getText().toString().isEmpty())) {
if (!(userPassword.getText().toString().isEmpty())) {
loginUserViaRest(userEmail.getText().toString(), userPassword.getText().toString());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
reply = rest.getForObject(
"http://192.168.178.60:8080/dashboard", String.class);
}
});
thread.start();
ProgressDialog progress = new ProgressDialog(Login.this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
while (thread.getState()!=Thread.State.TERMINATED){
}
progress.dismiss();
// I get the error at below line.
Log.d("Reply is ",reply);
那么,出了什么问题。任何指针,谢谢。
【问题讨论】:
-
不知道你为什么用Thread类,用AsyncTask
-
@mohammadsadeghsaati :如果它是异步任务,我不会在将来某个时间点获取我的数据,我该如何检查它.. 你能给我写一个适合要求的答案吗,我会检查出来并行。
标签: android multithreading thread-safety resttemplate