Hello StackOverFlow社区!我在我目前正在构建的一个android,java应用程序中遇到一行代码问题。
在应用程序启动期间,我运行(在其他代码中)以下代码,该代码创建另一个类的新实例。然后它在类中运行一个方法,在应用程序内部设置许多不同的东西。由于它是一个大量的处理,它在另一个线程,以保持应用程序以良好的速度运行。该方法不能是静态的,因为它使用上下文。每当我创建包含该线程的类的新实例(Longer_Tasks Set_Up_Reference = new Longer_Tasks())时,它就会崩溃。我想知道如何修复代码,因为我似乎在某处出现运行时错误!
new Thread(new Runnable()
{
public void run()
{
Longer_Tasks Set_Up_Reference = new Longer_Tasks();
Set_Up_Reference.Variable_Set_Up();
}
}).start();
我已通过调试器将其缩小到此行。
我为缺乏信息道歉,以下是构造函数:
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.SecureRandom;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
public class Longer_Tasks extends Activity
{
... Other Functions and Procedures ...
public void Variable_Set_Up()
{
Profile_Load_Data_From_File();
Profile_Load_Data_From_Server();
}
.... Other Functions and Procedures ...
}
以下是堆栈跟踪(我认为):
08-11 22:10:08.347: D/WifiStateMachine(1018): handleMessage: E msg.what=131155
08-11 22:10:08.347: D/WifiStateMachine(1018): processMsg: ConnectedState
08-11 22:10:08.348: D/WifiStateMachine(1018): processMsg: L2ConnectedState
08-11 22:10:08.361: D/WifiStateMachine(1018): handleMessage: X
08-11 22:10:11.364: D/WifiStateMachine(1018): handleMessage: E msg.what=131155
08-11 22:10:11.364: D/WifiStateMachine(1018): processMsg: ConnectedState
08-11 22:10:11.366: D/WifiStateMachine(1018): processMsg: L2ConnectedState
08-11 22:10:11.383: D/WifiStateMachine(1018): handleMessage: X
08-11 22:10:11.970: W/dalvikvm(9227): threadid=11: thread exiting with uncaught exception (group=0x41b33d40)
08-11 22:10:11.988: E/AndroidRuntime(9227): FATAL EXCEPTION: Thread-46755
08-11 22:10:11.988: E/AndroidRuntime(9227): Process: com.messenger_app, PID: 9227
08-11 22:10:11.988: E/AndroidRuntime(9227): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-11 22:10:11.988: E/AndroidRuntime(9227): at android.os.Handler.<init>(Handler.java:200)
08-11 22:10:11.988: E/AndroidRuntime(9227): at android.os.Handler.<init>(Handler.java:114)
08-11 22:10:11.988: E/AndroidRuntime(9227): at android.app.Activity.<init>(Activity.java:773)
08-11 22:10:11.988: E/AndroidRuntime(9227): at com.messenger_app.Longer_Tasks.<init>(Longer_Tasks.java:22)
08-11 22:10:11.988: E/AndroidRuntime(9227): at com.messenger_app.Home_Screen$1.run(Home_Screen.java:53)
08-11 22:10:11.988: E/AndroidRuntime(9227): at java.lang.Thread.run(Thread.java:841)
08-11 22:10:11.996: W/ActivityManager(1018): Force finishing activity com.messenger_app/.Home_Screen
08-11 22:10:12.160: D/dalvikvm(1018): GC_FOR_ALLOC freed 663K, 18% free 25966K/31436K, paused 151ms, total 151ms
希望这有帮助!
答案 0 :(得分:0)
您尝试在主线程之外实例化一个活动。
首先,永远不要在主线程之外做UI东西。 There's an established pattern for communicating back to the main thread
其次,永远不要手动实例化活动。 Use an Intent to start Activities
您可能需要浏览官方培训材料并挑选Android SDK附带的示例,以了解Android应用程序的工作原理。