我是Java的新手,我正在编写我的第一个Thread作为备份上传应用程序。我从一个例子中得到了这个代码,并做了一些修改。
我需要检查是否存在布尔值(如果文件存在),如果不存在,则中断线程并将活动更改为错误显示,我尝试但它不起作用。它引发了异常,我的应用程序崩溃了。我该如何解决?
感谢所有人。
Thread threadMenu = new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable () {
public void run() {
/*
messageText.setText("uploading started.....");
ProgressDialog progress = new ProgressDialog(null, serverResponseCode);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
*/
}
});
File folder = new File(Environment.getExternalStorageDirectory().toString() + myfolder);
File dir = new File(folder.toString());
boolean exists = dir.exists();
uploadFile(folder + "/My_File.png");
//IF NOT FILES REDIRECT TO NO FILES ACTIVITY
if (!exists) {
try {
//Log.v("ARCHIVOS NO ENCONTRADOS", folder.toString());
Intent i=new Intent(UploadToServer.this, NoFiles.class);
startActivity(i);
} catch (InterruptedException e) {
// We've been interrupted: no more messages.
return;
}
// Thread.currentThread().interrupt();
Log.v("ARCHIVOS NO ENCONTRADOS", folder.toString());
Intent i=new Intent(UploadToServer.this, NoFiles.class);
startActivity(i);
Thread.currentThread().interrupt();
}
Thread.currentThread().interrupt();
}
});
threadMenu.start();
}
这是Logcat的输出 - >线程停止时,我退出了SD卡。
10-20 23:50:44.158: W/dalvikvm(16441): threadid=1: thread exiting with uncaught exception (group=0x40018578)
10-20 23:50:44.166: E/AndroidRuntime(16441): FATAL EXCEPTION: main
10-20 23:50:44.166: E/AndroidRuntime(16441): java.lang.NullPointerException
10-20 23:50:44.166: E/AndroidRuntime(16441): at com.uploadtoserver.UploadToServer.uploadFile(UploadToServer.java:149)
10-20 23:50:44.166: E/AndroidRuntime(16441): at com.uploadtoserver.UploadToServer$1$1.run(UploadToServer.java:105)
10-20 23:50:44.166: E/AndroidRuntime(16441): at android.os.Handler.handleCallback(Handler.java:587)
10-20 23:50:44.166: E/AndroidRuntime(16441): at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 23:50:44.166: E/AndroidRuntime(16441): at android.os.Looper.loop(Looper.java:130)
10-20 23:50:44.166: E/AndroidRuntime(16441): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-20 23:50:44.166: E/AndroidRuntime(16441): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 23:50:44.166: E/AndroidRuntime(16441): at java.lang.reflect.Method.invoke(Method.java:507)
10-20 23:50:44.166: E/AndroidRuntime(16441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-20 23:50:44.166: E/AndroidRuntime(16441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-20 23:50:44.166: E/AndroidRuntime(16441): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
这里要理解的主要是你无法从后台线程调用startActivity()方法,因为它可能会导致奇怪的竞争条件。相反,您需要将runnable发布到主线程的消息队列以在主线程上执行。
像这样更新你的onCreate方法
Thread threadMenu;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
threadMenu = new Thread(runnable);
threadMenu.start();
}
并在你的onCreate结束后写下这段代码
Runnable runnable = new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
// messageText.setText("uploading started.....");
// ProgressDialog progress = new ProgressDialog(null,
// serverResponseCode);
// progress.setTitle("Loading");
// progress.setMessage("Wait while loading...");
// progress.show();
}
});
File folder = new File(Environment.getExternalStorageDirectory()
.toString() + "myfolder");
File dir = new File(folder.toString());
boolean exists = dir.exists();
System.out.println("boolean value is " + exists);
// uploadFile(folder + "/My_File.png");
// IF NOT FILES REDIRECT TO NO FILES ACTIVITY
if (!exists) {
// Thread.currentThread().interrupt();
Log.v("ARCHIVOS NO ENCONTRADOS", folder.toString());
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(UploadToServer.this, NoFiles.class);
startActivity(i);
threadMenu.stop();
}
});
}
}
};
我建议你除了android中的线程之外应该使用AsyncTask,这是android推荐的。有关线程和Asynctask的更多信息可以在here找到。