2015-06-14 92 views
0

我正在構建一個接收動作的應用程序.END Intent並將剪輯數據中的所有文件顯示爲ListView。我的ListView位於由我的MainActiviy託管的ShareFragment之內。以下是我如何實現它。片段的onCreate和onCreateView不叫

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.SENDIntent,它跑進onCreate和處理這一行shareFragment = new ShareFragment()

onCreateShareFragmentonCreateView永遠不會被調用。 爲什麼?

注意我以爲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

move handleIntent(intent);進入onResume(),你嘗試在onCreate中設置數據,但此時不創建片段 –

+0

@ fmt.Println.MKO它的工作,似乎'Fragment'的創建不在UI線程中。你可以寫下你的答案,我會接受它。 –

回答

相關問題