0

好吧所以我想哟传递任何文件从文件管理器(活动)的片段。这里是我的代码:传递文件从活动到片段使用包

   Bundle bundle = new Bundle(); 
        bundle.putString ("File", chosenFile); 
        Intent intent = new Intent (getApplicationContext(), UploadFragment.class); 
        intent.putExtras (bundle); 
        startActivity (new Intent (FileExplore.this, UploadFragment.class)); 

该应用程序在Logcat中崩溃是状态无法找到显式活动类。然后它给了我发生代码错误的行,这是startActivity(新的意图(.....)。是错误,因为我没有正确地将活动发送到片段的意图?

在片段我有以下代码button.setOnclickListener()下。

Intent intent = new Intent (getActivity(), FileExplore.class); 
      startActivity (intent); 

回答

0

首先你不能创建片段或使用意向将数据发送给它的。

你可以把一个片段里面,你活动打电话: getFragmentManager().beginTransaciton().add(R.id.id_of_container, new UploadFragment(), false).commit();

如果你想从活动数据传递到片段您使用setArgument()方法做它片断创建,例如:

Bundle bundle = new Bundle(); 
bundle.putString ("File", chosenFile); 

UploadFragment fragment = new UploadFragment(); 
fragment.setArguments(bundle); 

getFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit(); 
+0

唉唉!我看到让我测试一下,谢谢一堆! – user3900101 2014-10-26 22:27:29

+0

我得到一个错误说.add()不能被应用于一个实际的参数。 – user3900101 2014-10-27 11:06:45

+0

对不起,我的坏添加方法没有布尔作为第三个参数,我已经更新了我的答案 – Hellboy 2014-10-27 11:32:38

相关问题