0

我有一个确定位置的主要活动。在这个主要活动中,我首先添加了一个闪屏片段。目前我发现第一个位置/GPS 工作正常并找到了一个位置,我想用我的主菜单替换这个闪屏。mainactivity中的相关代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.frame);

    //Check that the activity is using the layout version
    // the framelayout

    if(findViewById(R.id.fragment_container)!=null){
        //To avoid overlapping fragments check if we are restored from a state, then we don't have to do anything
        if(savedInstanceState != null){
            return;
        }

        Splash splashscr = new Splash();
        Main main = new Main();

        getSupportFragmentManager().beginTransaction()
        .add(R.id.fragment_container, splashscr).commit();

    // I **think something should be added here 
//to check if a location has been found yet.**

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, main);
        transaction.commit();
        }


}  

我尝试在该位置添加一段时间(foundlocation!=true)。然后在 onLocationChanged() 或 getLocation() 结束时将 foundlocation 设置为 true。但是,它并没有解决我的问题。有人可以帮我吗?如果我的问题不够清楚,请说出来。

编辑:在考虑了 Gabe 的评论后,我尝试了这个(locationfound 是一个公共布尔值,在活动类中启动为 false)。但是,如果我现在使用 DDMS 发送位置,则启动画面不会消失。如果我是正确的,那么第一次更改位置时,布尔值仍然是错误的,因此它应该切换片段。我在没有 if 语句的情况下尝试了它,然后它就可以工作了。我在这里忘记了什么?

public void onLocationChanged(final Location location) { 

       if(locationfound=false)
       {
//Some irrelevant code about saving the new location

        Main main = new Main();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, main);
        transaction.commit();

        locationfound=true;
           }

//changing value of sometextboxes in the Main fragment

   }
4

1 回答 1

0

不要试图在 onCreate 中等待,这会阻止 UI 线程导致 UI 无响应。相反,您应该在第一次调用 onLocationChanged 函数时切换片段(通过使用布尔标志变量来判断它是否是第一次)。

于 2012-11-13T19:51:55.723 回答