手机应用java停止运行_代码不报错,应用可以启动,点击按钮就停止运行

求大神,求帮忙,求解答,先行谢谢!!

这是java中的代码

package com.example.sun;

import android.os.Bundle;

import android.app.Activity;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.ToggleButton;

public class MainActivity extends Activity {

private ToggleButton toggleButton;

private ImageView imageView;

private MyThreah myThreah;

static TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

android.util.Log.i("tag", "初始化开始");

toggleButton=(ToggleButton) findViewById(R.id.toggleButton1);

imageView=(ImageView) findViewById(R.id.imageView1);

myThreah=new MyThreah();

textView=(TextView) findViewById(R.id.TextView1);

android.util.Log.i("tag", "初始化结束");

toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

android.util.Log.i("tag", "button被点击");

try {

imageView.setImageResource((arg1)?R.drawable.on:R.drawable.off);

myThreah.start();

} catch (Exception e) {

android.util.Log.i("tag", "button点击异常");

}

}

});

}

}

class MyThreah extends Thread{//自己定义的一个线程类

@Override

public void run() {

android.util.Log.i("tag", "run方法被执行");

MainActivity.textView.setText("改变的数"+"10");

android.util.Log.i("tag", "run完毕");

}

}

这是xml文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:orientation="vertical" >

android:id="@+id/toggleButton1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textOn="已开启"

android:textOff="已关闭"/>

android:id="@+id/imageView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:src="@drawable/off" />

android:id="@+id/TextView1"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="这是文本框" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值