我正在构建一个接收动作的应用程序。发送ListView
并将剪辑数据中的所有文件显示为ListView
。我的ShareFragment
位于由MainActiviy
托管的public class ShareFragment extends MFragment {
private static final String TAG = "ShareFragment";
private List<File> files;
private FileAdapter fileAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate called");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
files = new ArrayList<>();
fileAdapter = new FileAdapter(getActivity(), 0, files);
View rootView = inflater.inflate(R.layout.share_fragment, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.listView);
lv.setAdapter(fileAdapter);
Log.e(TAG, "onCreateView called");
return rootView;
}
public void setData(List<File> files){
Log.e(TAG, "setData called");
this.files = files;
fileAdapter.notifyDataSetChanged();
}
内。以下是我如何实现它。
MainActivity
以下是我在@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
shareFragment = (ShareFragment) getSupportFragmentManager().findFragmentByTag("share_fragment");
if(shareFragment == null) {
shareFragment = new ShareFragment();
shareFragment.setTitle("Share");
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fl_root, shareFragment, "share_fragment");
ft.commit();
handleIntent(intent);
}
handleIntent
以下是private void handleIntent(Intent intent) {
String action = intent.getAction();
String type = intent.getType();
if ((Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) && type != null) {
int size = intent.getClipData().getItemCount();
List<File> files = new ArrayList<>();
for (int i = 0; i < size; i++) {
File file = new File(intent.getClipData().getItemAt(i).getUri().getPath());
files.add(file);
}
shareFragment.setData(files);
}
}
方法:
action.SEND
我的问题是
当我的应用程序通过收到Intent
onCreate
启动时,它会遇到shareFragment = new ShareFragment()
并处理此行onCreate
但是
永远不会调用onCreateView
的{{1}}和ShareFragment
。的为什么吗
注意我认为FragmentManager
在我的应用首次运行时保留了ShareFragment
的实例。所以我试图找到我的片段重用它,但它总是返回null。
修改
我在NullPointerException
fileAdapter.notifyDataSetChanged();
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void icetea.com.quickshare.adapter.FileAdapter.notifyDataSetChanged()' on a null object reference
at icetea.com.quickshare.fragments.ShareFragment.setData(ShareFragment.java:49)
at icetea.com.quickshare.MainActivity.handleIntent(MainActivity.java:63)
at icetea.com.quickshare.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
at android.app.ActivityThread.access$900(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6141)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:1)
将template_vars = headers[1:]
kwargs = dict.fromkeys(template_vars)
移至 onResume()
您尝试在onCreate中设置数据,但此时片段尚未创建
您可以在Fragments文档中找到更多相关信息: http://developer.android.com/guide/components/fragments.html#Lifecycle