Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用接口将数据从活动传递到片段_Android_Interface - Fatal编程技术网

Android 使用接口将数据从活动传递到片段

Android 使用接口将数据从活动传递到片段,android,interface,Android,Interface,在我的应用程序中,我有一个选择器控件(在片段中),当用户单击它时,一个新的菜单活动将显示一些项目列表,当用户单击任何项目时,活动将完成,界面应将所选项目字符串再次传输到片段中的选择器 以下是我的菜单活动: public class Menu extends AppCompatActivity { ListView listView; public interface ItemListener { void getItem(String s); }

在我的应用程序中,我有一个选择器控件(在片段中),当用户单击它时,一个新的菜单活动将显示一些项目列表,当用户单击任何项目时,活动将完成,界面应将所选项目字符串再次传输到片段中的选择器

以下是我的菜单活动:

public class Menu extends AppCompatActivity {

    ListView listView;


    public interface ItemListener {
        void getItem(String s);
    }


    ItemListener itemListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        // Error Goes Here

        itemListener = (ItemListener) this;

        // Define and fill the list view

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                itemListener.getItem("Some Item");
                finish();

            }
        });
    }
}
但是我得到了一个错误:

Caused by: java.lang.ClassCastException: Menu cannot be cast to Menu$ItemListener

我错了,任何帮助都会非常感激的

从你现在的评论中可以更清楚地看到你想做什么<代码>片段很大程度上取决于
活动
,它实际上无法独立运行,任何接口实现都会在
onAttach()
期间被捕获,
片段
的上下文是
活动
所以如果
活动
未实现接口,则会在
onAttach()中发生崩溃
在片段中

但在您的场景中,您有两个不同的
活动
,因此在这种情况下,您可以使用
捆绑
广播接收器
传递数据。要使用
broadcastReceiver
片段中创建一个方法

private void registerReciver() {
BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
                String getData = intent.getStringExtra("data");
            }
    }
};
IntentFilter intentFilter = new IntentFilter("action");
this.registerReceiver(receiver, intentFilter);
}

调用registerReciver()在片段
onCreate()
方法中。现在,从第二个
活动
关闭它的地方,启动
广播接收器
发送一些数据:

                        Intent intent = new Intent();
                        intent.putExtra("data", "some data");
                        intent.setAction("action");
                        this.sendBroadcast(intent);
第二种方法在
片段
覆盖
onResume
方法中也应该有效,该方法将在
活动
第二个
活动
完成后调用。在
活动中
将数据放入
包中
如下:

Addorder fragment = new Addorder();
Bundle bundle = new Bundle();
bundle.putString("data", "Some data");
fragment.setArguments(bundle);
而在
onResume()中的
Fragment
方法中获取数据:

Bundle bundle = this.getArguments();
if (bundle != null) { //to prevent crash must check against null
    String getData = bundle.getString("data", defaultValue);
 }

我认为这两种方法都应该有效,在我看来,听者是无用的。在
Addorder
中,您应该使用
startActivityForResult()
打开菜单活动。在
菜单中设置结果后,您将在片段的
onActivityResult
回调中收到它。下面是一个示例:您的
活动
没有实现您的接口。您还需要在
活动中实现它。为什么我的活动应该实现侦听器?picker控件位于片段中,不在活动中。两个解决方案都抛出错误且不工作!第一种解决方案:涉及菜单的循环继承,第二种解决方案:Main不能被转换到菜单
片段
很大程度上取决于
活动
,它实际上不能独立运行,任何接口实现都会在
onAttach()期间被捕获,
Fragment
的上下文是
Activity
,因此如果
Activity
未实现
接口
将在
onAttach()
中发生崩溃。我已经编辑了代码。错误出现在
implements
remove
Menu
中。不起作用,请更清楚我的片段父项是主要活动(BottomNavigationView中的片段),选择器在片段中,菜单活动是独立活动,我想将数据从菜单活动传递到附加了
activity
fragment的addOrder fragments?这是主要问题,如果
片段
附加在
MainActivity
上,则需要在
MainActivity
中实现接口。如果您试图将数据从一个
活动
传递到附加到另一个
活动
片段
,则此带有接口的部分将无法工作,您不能这样传递数据。这就是你想做的吗?谢谢,伙计,完美的解决方案
Bundle bundle = this.getArguments();
if (bundle != null) { //to prevent crash must check against null
    String getData = bundle.getString("data", defaultValue);
 }