3

我有一个活动类 CreateEventPage,这基本上调用 onCreate 方法中的一个片段,该方法加载用于创建事件的表单。在按下特定按钮时,会调用 showMapFragment() 方法,用 MyMapFragment 对象(扩展 Fragment 类)替换该片段。

CreateEventPage 类如下:

public class CreateEventPage extends FragmentActivity implements 
MapLongPressListener {
    private CreateEventFragment eventFragment;
    private MyMapFragment mapFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragmentcontainer);
        eventFragment = new CreateEventFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, eventFragment).commit();
        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.create_event_page, menu);
        return true;
    }
    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }
    public void showMapFragment(View v) {

            // If the frag is not available, we're in the one-pane layout and must swap frags...
            // Create fragment and give it an argument for the selected article

            mapFragment = new MyMapFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            transaction.replace(R.id.fragment_container, mapFragment);
            transaction.addToBackStack(null);

            transaction.commit();
    }
    @Override
    public void MapLongPressed(String locationAddress, double latitude, double longitude) {

        FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
        trans.replace(R.id.fragment_container, eventFragment).commit();
        eventFragment.initPositions(locationAddress, latitude, longitude);

    }

}

当我调用 eventFragment 对象的 initPositions 方法时,我尝试更新 EditText 对象的文本字段。但是,UI 不会更新。我也尝试在 UI 线程上运行该方法,它仍然没有更新。view 是一个全局变量,它存储了 Fragment 的 onCreateView 方法返回的 View 对象。initPositions 方法如下:

public void initPositions( String address,  double lat, double longi) {
                EditText location = (EditText) view.findViewById(R.id.LocationText);
                latitude = lat;
                longitude = longi;
                location.setText(address);
                Log.i("Init Positions", address);
                Log.i("Init Positions", ""+latitude);
                Log.i("Init Positions", ""+longitude);
                Log.i("Init Positions", inputLocation.getText().toString());
                }

然而,尽管如此,即使日志打印出我想要的正确值,UI 也不会更新。你能告诉我我做错了什么吗?

谢谢,罗希特

4

0 回答 0