新人求助,是关于Android主线程和子线程的问题

在编一个百度地图校园导航的程序,程序有时候能运行,有时候不能运行,不能运行时会报这个: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
麻烦各位朋友帮我看看该怎么改OnCreate方法中的代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

     . . . . . .

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        SDKInitializer.setCoordType(CoordType.BD09LL);
        setContentView(R.layout.activity_main);
        position=(TextView)  findViewById(R.id.position_text_view) ;
        accessPermission();
        initView();
        initLocation();
        Button mbutton1=(Button)findViewById(R.id.button1);
        mbutton1.setOnClickListener(MainActivity.this);
        Button mbutton2=(Button)findViewById(R.id.button2);
        mbutton2.setOnClickListener(MainActivity.this);
        mbutton3=(Button)findViewById(R.id.button3);
        mbutton3.setOnClickListener(MainActivity.this);
        mbutton4=(Button)findViewById(R.id.button4);
        mbutton4.setOnClickListener(MainActivity.this);
        mbutton5=(Button)findViewById(R.id.button5);
        mbutton5.setOnClickListener(MainActivity.this);
    }

           . . . . . . 

        @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.button1:
                if((flag%2)==0){
                    mbutton3.setVisibility(View.VISIBLE);
                    mbutton4.setVisibility(View.VISIBLE);
                    mbutton5.setVisibility(View.VISIBLE);
                    flag+=1;

                }else if((flag%2)==1){
                    mbutton3.setVisibility(View.GONE);
                    mbutton4.setVisibility(View.GONE);
                    mbutton5.setVisibility(View.GONE);
                    flag+=1;
                }
                break;
            case R.id.button2:
                initNavi();
                startBikeNavi();
                break;
            case R.id.button3:
                Toast.makeText(MainActivity.this,"请在地图上长按您要去的地方设置目的地",Toast.LENGTH_LONG).show();
                mbutton3.setVisibility(View.GONE);
                mbutton4.setVisibility(View.GONE);
                mbutton5.setVisibility(View.GONE);
                break;
            case R.id.button4:
                Toast.makeText(MainActivity.this,"你点击了button2",Toast.LENGTH_LONG).show();
                mbutton3.setVisibility(View.GONE);
                mbutton4.setVisibility(View.GONE);
                mbutton5.setVisibility(View.GONE);
                break;
            case R.id.button5:
                Toast.makeText(MainActivity.this,"你点击了button2",Toast.LENGTH_LONG).show();
                mbutton3.setVisibility(View.GONE);
                mbutton4.setVisibility(View.GONE);
                mbutton5.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

7个回答

安卓主线程,,,UI线程,,是不允许做耗时操作的,,,

你需要用个new一个子线程去完成耗时操作,,然后给主线程结果。

tsun1217
tsun1217 好的,多谢
大约 3 年之前 回复
Small_Mouse0
鼠小 http://www.imooc.com/learn/406,,你看下安卓异步加载吧,,
大约 3 年之前 回复
tsun1217
tsun1217 能举个有代码的例子吗?
大约 3 年之前 回复

我到网上查了下,好像是onCreate中有耗时的操作,希望各位给点建议

accessPermission();
initView();
initLocation();
这几个操作该很耗时,应该怎么放呢

耗时的操作new一个子线程去执行啊

tsun1217
tsun1217 Android 刚接触,不懂怎么new一个子线程啊
大约 3 年之前 回复

new Thread(new Runnable() {

@Override
public void run() {
    // 放你执行的操作
}

}).start();

new Thread()
{
public void run()
{

 }

}.start();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread()
{
public void run()
{
//此处发你的这些操作
}
}.start();

}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐