tsun1217
tsun1217
采纳率100%
2017-08-14 02:20

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

10
已采纳

在编一个百度地图校园导航的程序,程序有时候能运行,有时候不能运行,不能运行时会报这个: 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条回答

  • Small_Mouse0 鼠小 4年前

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

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

    点赞 评论 复制链接分享
  • zgy621101 紫风幻雪 4年前

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

    }
    
    点赞 评论 复制链接分享
  • qq_37437983 我要出家当道士 4年前

    new Thread()
    {
    public void run()
    {

     }
    

    }.start();

    点赞 评论 复制链接分享
  • ziyejinwei1994 ziyejinwei1994 4年前

    new Thread(new Runnable() {

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

    }).start();

    点赞 评论 复制链接分享
  • SingleView SingleView 4年前

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

    点赞 评论 复制链接分享
  • tsun1217 tsun1217 4年前

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

    点赞 评论 复制链接分享
  • tsun1217 tsun1217 4年前

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

    点赞 评论 复制链接分享

相关推荐