接口通信的片段活动问题 [英] Interface communication from fragment to activity issue

查看:246
本文介绍了接口通信的片段活动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试使用片段活动通信的接口。我们的想法是,当一个按钮是pssed在一个片段$ P $,它检索数据从在同一个片段一个EditText,它然后发送字符串至MainActivty - 这个控制所有我的片段 - 其然后开始另一个片段,并提供该字符串该片段以备后用,但是,我有麻烦最初设置它发送数据的第一个接口。不幸的是什么也没有发生,我因此不能进入下一个片段应该显示。此外,我用getActivity()试过,但找不到相关方法的片段中,导致我相信,不知怎的,没有直接连接到MainActivity的片段(我才刚刚掌握的Java基础知识和一点点的Andr​​oid,只是学习。)

我所列出的相关信息如下,感谢您的支持!

片段

 公共类CreateWorkoutFragment扩展片段实现OnClickListener {

查看rootViewCreateWorkoutFragment;

的EditText editTextWorkoutName;

//使用ImageView的自定义按钮
ImageView的buttonNext;

字符串valueCreateWorkoutEditText;

OnDataPass dataPasser;

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                         捆绑savedInstanceState){
    rootViewCreateWorkoutFragment = inflater.inflate(R.layout.fragment_create_workout,集装箱,假);

    buttonNext =(ImageView的)rootViewCreateWorkoutFragment.findViewById(R.id.button_workout_name_next);

    editTextWorkoutName =(EditText上)rootViewCreateWorkoutFragment.findViewById(R.id.edit_text_workout_name);

    buttonNext.setOnClickListener(本);

    返回rootViewCreateWorkoutFragment;
}

公共接口OnDataPass {
    公共无效onDataPass(字符串数据);
}

@覆盖
公共无效onAttach(活动活动){
    super.onAttach(活动);
    dataPasser =(OnDataPass)的活动;
}

公共无效passData(字符串数据){
    dataPasser.onDataPass(数据);
}

@覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
        案例R.id.button_workout_name_next:
            。valueCreateWorkoutEditText = editTextWorkoutName.getText()的toString();
            passData(valueCreateWorkoutEditText);
            打破;
    }
}
}
 

主要活动

 公共类MainActivity扩展FragmentActivity实现OnClickListener,CreateWorkoutFragment.OnDataPass {

ImageView的buttonCreate;

片段newFragment;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppThemeBlue);
    的setContentView(R.layout.activity_main);

    buttonCreate =(ImageView的)findViewById(R.id.create_foreground);

    buttonCreate.setOnClickListener(本);

    FragmentManager fragManager = getSupportFragmentManager();
    FragmentTransaction tranManager = fragManager.beginTransaction();
    CreateWorkoutFragment createWorkoutFrag =新CreateWorkoutFragment();

    // fragment_change就在XML中的区域片段切换
    tranManager.add(R.id.fragment_change,createWorkoutFrag);
    tranManager.commit();

    newFragment = NULL;
}

@覆盖
公共无效onDataPass(字符串数据){
    // CreateFragment是不要与CreateWorkoutFragment混淆
    // CreateFragment是片断的,我想开始时,任何字符串
    //从CreateWorkoutFragment获得
    newFragment =新CreateFragment();
}

@覆盖
公共无效的onClick(视图v){

    开关(v.getId()){
    // create_foreground只是作为一个按钮的ImageView的
    // Additionaly,其他按键被用来创建其他片段,
    //我剪出来当前,因为它们不是所必要的是
    //为什么CreateWorkoutFragment是唯一的按钮和默认当前
    案例R.id.create_foreground:
        newFragment =新CreateWorkoutFragment();
        打破;
    默认:
        newFragment =新CreateWorkoutFragment();
    }

FragmentTransaction tranManager = getSupportFragmentManager()的BeginTransaction()。
    tranManager.replace(R.id.fragment_change,newFragment);
    tranManager.addToBackStack(空);
    tranManager.commit();
}
}
 

抱歉code是不完全整齐,但是,它是最相关的code从大类切出。正如我所说的,我已经尝试过其他方法,但无法从MainActivity任何回应无论哪种方式。在此先感谢!

就在我贴:得到了应用程序写入logcat的消息给我,它设法通过单击该按钮时,数据 - 至少我认为,是什么做的片段无法启动!在MainActivity> onDataPass()>新片段=新CreateFragment()任何想法?如前所述,其他按钮确实存在,并设法改变片段。但是,被删减少code量张贴。

解决方案
  

getActivity(),但它不能找到的片段内的相关联的方法

这是因为 getActivity()返回活动,不是 MainActivity 这是你的自定义子类。您可以轻松地用铸造解决这个问题。例如,在你的片段,你可以这样做:

  OnDataPass主要=(OnDataPass)getActivity();
main.onDataPass(消息);
 

由于这种投是必需的,界面似乎变得在我看来的方式。你可以很容易地直接转换为 MainActivity

  MainActivity主要=(MainActivity)getActivity();
main.onDataPass(消息);
 

I've recently tried to use an interface for fragment-activity communication. The idea is that when a button is pressed in a fragment, it retrieves data from an EditText in the same fragment, it then sends the string to the MainActivty - this controls all my fragments - which then starts another fragment and delivers the string to this fragment for use later, however, I'm having trouble initially setting up the first interface which sends the data. Unfortunately nothing happens, and I cannot therefore get to the next fragment which should be displayed. Additionally I have tried using getActivity() but it cannot find the associated method within the fragment, leading me to believe that the fragments somehow aren't directly connected to MainActivity (I've only just grasped basics of Java and a little of Android, just learning.)

I've listed the relevant information below, thanks for the assistance!

Fragment

public class CreateWorkoutFragment extends Fragment implements OnClickListener {

View rootViewCreateWorkoutFragment;

EditText editTextWorkoutName;

// Using an ImageView for custom button
ImageView buttonNext;

String valueCreateWorkoutEditText;

OnDataPass dataPasser;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootViewCreateWorkoutFragment = inflater.inflate(R.layout.fragment_create_workout, container, false);

    buttonNext = (ImageView) rootViewCreateWorkoutFragment.findViewById(R.id.button_workout_name_next);

    editTextWorkoutName = (EditText) rootViewCreateWorkoutFragment.findViewById(R.id.edit_text_workout_name);

    buttonNext.setOnClickListener(this);

    return rootViewCreateWorkoutFragment;
}

public interface OnDataPass {
    public void onDataPass(String data);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    dataPasser = (OnDataPass) activity;
}

public void passData(String data) {
    dataPasser.onDataPass(data);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button_workout_name_next:
            valueCreateWorkoutEditText = editTextWorkoutName.getText().toString();
            passData(valueCreateWorkoutEditText);
            break;
    }
}
}

Main Activity

public class MainActivity extends FragmentActivity implements OnClickListener, CreateWorkoutFragment.OnDataPass {

ImageView buttonCreate;

Fragment newFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppThemeBlue);
    setContentView(R.layout.activity_main);

    buttonCreate = (ImageView) findViewById(R.id.create_foreground);

    buttonCreate.setOnClickListener(this);

    FragmentManager fragManager = getSupportFragmentManager();
    FragmentTransaction tranManager = fragManager.beginTransaction();
    CreateWorkoutFragment createWorkoutFrag = new CreateWorkoutFragment();

    // fragment_change is just the area in XML where fragments switch
    tranManager.add(R.id.fragment_change, createWorkoutFrag);
    tranManager.commit();

    newFragment = null;
}

@Override
public void onDataPass(String data) {
    // CreateFragment is not to be confused with CreateWorkoutFragment
    // CreateFragment is the fragment I'm trying to start when any strings
    // are obtained from CreateWorkoutFragment
    newFragment = new CreateFragment();
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
    // create_foreground is just an ImageView used as a button
    // Additionaly, other buttons are used to create other fragments,
    // I've cut them out currently as they are not nessesary which is
    // why CreateWorkoutFragment is only button and default currently
    case R.id.create_foreground:
        newFragment = new CreateWorkoutFragment();
        break;
    default:
        newFragment = new CreateWorkoutFragment();
    }

FragmentTransaction tranManager = getSupportFragmentManager().beginTransaction();
    tranManager.replace(R.id.fragment_change, newFragment);
    tranManager.addToBackStack(null);
    tranManager.commit();
}
}

Sorry the code isn't exactly tidy, however, it was the most relevant code cut out from a large class. As I said, I have tried other methods yet cannot get any response from MainActivity either way. Thanks in advance!

Just before I posted: Got the app to write logcat messages to me, it manages to pass the data when the button is clicked - at least I think, and is something to do with the fragment not starting! At MainActivity>onDataPass()>new Fragment = new CreateFragment() Any ideas? As mentioned before, other buttons do exist and manage to change the fragment. However, were cutout to reduce amount of code posted.

解决方案

getActivity() but it cannot find the associated method within the fragment

This is because getActivity() returns an Activity, not a MainActivity which is your custom subclass. You can easily fix this with a cast. For example, in your fragment, you can do this:

OnDataPass main = (OnDataPass) getActivity();
main.onDataPass(message);

Since such a cast is required, the interface seems to get in the way in my opinion. You can just as easily cast directly to MainActivity:

MainActivity main = (MainActivity) getActivity();
main.onDataPass(message);

这篇关于接口通信的片段活动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆