【Android新手求助】为什么创建新线程后App会闪退

Keyon同学 2018-04-13 11:09:15
本人大二Android新手,在做课本例题时遇到了很多闪退的问题,原因各种各样,现在又遇到了加入开启线程的代码后程序崩溃的问题,请大佬给予指点


还有一个问题就是 烦请大佬解释下extends AppCompatActivity和extends Activity的区别,因为书上的例题代码用的编译软件是Eclipse,所有MainActivity都是继承自Activity的,而我用的是Android Studio,默认是继承自AppCompatActivity的,有时候程序闪退就是继承的原因,有时候需要继承Activity才能运行,而有时候要继承AppCompatActivity才能用,能麻烦解释一下吗


标题所讲的问题代码(一个产生随机数的程序)如下所示:
public class MainActivity extends AppCompatActivity implements Runnable {
private TextView tv_random;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_random = (TextView) findViewById(R.id.random);

Thread t = new Thread(this);
t.start();

handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x101) {
tv_random.setText(msg.obj.toString());
}
super.handleMessage(msg);
}
};
}

@Override
public void run() {
String value = "";
int max = 9999999;
int min = 1000000;
while (!Thread.currentThread().isInterrupted()){
value = String.valueOf(new Random().nextInt(max)%(max-min+1)+min);
Message m = handler.obtainMessage();
m.obj = value;
m.what = 0x101;
handler.sendMessage(m);
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
...全文
2129 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
江三木洛 2018-04-13
  • 打赏
  • 举报
回复
Thread t = new Thread(this); t.start(); handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x101) { tv_random.setText(msg.obj.toString()); } super.handleMessage(msg); } }; 应该是空指针,在新线程里handler.sendMessage(m);时不能保证handler已经被初始化了,把handler = new Handler()放在start前面
Keyon同学 2018-04-13
  • 打赏
  • 举报
回复
再运行一次莫名其妙的好了,但是我还是给log贴出来请大神帮忙解读下哪里有问题吧 刚学不怎么会看log信息
$ adb shell am start -n "com.example.exam7_1/com.example.exam7_1.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 16420 on device emulator-5554
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/zygote: JIT profile information will not be recorded: profile file does not exits.
I/chatty: uid=10106(com.example.exam7_1) identical 10 lines
W/zygote: JIT profile information will not be recorded: profile file does not exits.
I/MemoryIntArray: created from parcel MemoryIntArray@24256559 mMemoryAddr=3955920896 mFd=47
I/InstantRun: starting instant run server: is main process
I/zygote: Background concurrent copying GC freed 10573(2MB) AllocSpace objects, 0(0B) LOS objects, 57% free, 1135KB/2MB, paused 7.349ms total 82.076ms
W/zygote: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
          Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
D/OpenGLRenderer: HWUI GL Pipeline
D/: HostConnection::get() New Host Connection established 0xe189e240, tid 16452
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xe3549380: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xe3549380: ver 2 0 (tinfo 0xe967ee50)
D/EGL_emulation: eglMakeCurrent: 0xe3549380: ver 2 0 (tinfo 0xe967ee50)
Keyon同学 2018-04-13
  • 打赏
  • 举报
回复
引用 1 楼 r00_a2lBUR 的回复:
Message m = handler.obtainMessage(); 换成new的方式可能就不闪退了
用new的方式写还是不行…
Shay24156 2018-04-13
  • 打赏
  • 举报
回复
AppcompaActivity相对于Activity的主要的两点变化; 1:主界面带有toolbar的标题栏; 2,theme主题只能用android:theme=”@style/AppTheme (appTheme主题或者其子类),而不能用android:style。 否则会提示错误: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 至于具体为什么报错.建议你把log信息附上
r00_a2lBUR 2018-04-13
  • 打赏
  • 举报
回复
Message m = handler.obtainMessage(); 换成new的方式可能就不闪退了
x443137254 2018-04-13
  • 打赏
  • 举报
回复
楼上说的可能是对的,或者你把睡1秒那句话往上拉,先睡1秒再发消息,就保证没问题,还有一种写法是在声明成员变量的时候就直接初始化,这个能保证是第一时间得到初始化

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧