2

我已經在framelayout中包含了一個片段......並且我正在使用framelayout獲取片段 的子視圖。findViewById從framelayout包含一個片段的視圖失敗

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pan); 
    CardFragment fragment=new CardFragment(); 
    FragmentManager fm=getSupportFragmentManager(); 
    FragmentTransaction ft=fm.beginTransaction(); 
    ft.add(R.id.container, fragment); 
    container=(FrameLayout)findViewById(R.id.container); 
    img_pan=(ImageView)container.findViewById(R.id.img_pan); 
    txt_name=(TextView)container.findViewById(R.id.txt_name); 
    txt_father=(TextView)container.findViewById(R.id.txt_father); 
    txt_dob=(TextView)container.findViewById(R.id.txt_dob); 
    txt_type=(TextView)container.findViewById(R.id.txt_type); 
    txt_panid=(TextView)container.findViewById(R.id.txt_panid); 

} 

@Override 
public void onResume() 
{ 
    super.onResume(); 
    Intent i=getIntent(); 
    String name=i.getStringExtra("pan_name"); 
    String father=i.getStringExtra("pan_father"); 
    String dob=i.getStringExtra("pan_dob"); 
    String type=i.getStringExtra("pan_type"); 
    String panid=i.getStringExtra("pan_id"); 
    filePath=i.getStringExtra("pan_filePath"); 
    Log.d("CardActivity", "Filepath: "+filePath); 
    Bitmap bmp=BitmapFactory.decodeFile(filePath); 
    if(bmp==null) 
    { 
     Log.d("CardActivity", "Error getting image to bitmap"); 
    } 
    img_pan.setImageBitmap(bmp); 
    txt_name.setText(name); 
    txt_father.setText(father); 
    txt_dob.setText(dob); 
    txt_type.setText(type); 
    txt_panid.setText(panid); 
} 

當我嘗試值添加到視圖,它拋出一個NullPointerException

編輯:這是工作只是罰款之前,我分隔一切都變成碎片..

EDIT2:我有改變了活性和所建議的片段,該活動的onCreate:

Bundle state=new Bundle(); 
    state.putString("pan_name", name); 
    state.putString("pan_father", father); 
    state.putString("pan_dob", dob); 
    state.putString("pan_type",type); 
    state.putString("pan_id", panid); 
    state.putString("pan_filepath", filePath); 
    CardFragment fragment=new CardFragment(); 
    fragment.setArguments(state); 
    FragmentManager fm=getSupportFragmentManager(); 
    FragmentTransaction ft=fm.beginTransaction(); 
    ft.add(R.id.container, fragment); 
    ft.commit(); 
    if(fm.executePendingTransactions()) 
    { 
     Log.d("CardActivity", "Executed all pending operations"); 
    } 

片段onCreateView方法:

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) 
{ 
    super.onCreateView(inflater, container, savedInstanceState); 
    img_pan=(ImageView)container.findViewById(R.id.img_pan); 
    txt_name=(TextView)container.findViewById(R.id.txt_name); 
    txt_father=(TextView)container.findViewById(R.id.txt_father); 
    txt_dob=(TextView)container.findViewById(R.id.txt_dob); 
    txt_type=(TextView)container.findViewById(R.id.txt_type); 
    txt_panid=(TextView)container.findViewById(R.id.txt_panid); 
    Bundle bundle=getArguments(); 
    String name=bundle.getString("pan_name");//line 39 
    String father=bundle.getString("pan_father"); 
    String dob=bundle.getString("pan_dob"); 
    String type=bundle.getString("pan_type"); 
    String id=bundle.getString("pan_id"); 
    String filePath=bundle.getString("pan_filepath"); 
    Bitmap bmp=BitmapFactory.decodeFile(filePath); 
    img_pan.setImageBitmap(bmp); 
    txt_name.setText(name); 
    txt_father.setText(father); 
    txt_dob.setText(dob); 
    txt_type.setText(type); 
    txt_panid.setText(id); 

    return inflater.inflate(R.layout.layout_pan, container,false); 

} 

現在,它仍然會返回一個空指針異常時,logcat的似乎在getArguments()我已經嘗試使用saveInstanceState沒有成功必須指出。

 E/AndroidRuntime(13367): Caused by: java.lang.NullPointerException 
    E/AndroidRuntime(13367): at  com.example.newscancardapp.CardFragment.onCreateView(CardFragment.java:39) 
    E/AndroidRuntime(13367): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 
    E/AndroidRuntime(13367): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 
    E/AndroidRuntime(13367): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
    E/AndroidRuntime(13367): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086) 
    E/AndroidRuntime(13367): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877) 
    E/AndroidRuntime(13367): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552) 
    E/AndroidRuntime(13367): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133) 
    E/AndroidRuntime(13367): at android.app.Activity.performStart(Activity.java:4529) 
    E/AndroidRuntime(13367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929) 

編輯3此代碼工作:

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) 
{ 
    super.onCreateView(inflater, container, savedInstanceState); 
    View pan_layout=inflater.inflate(R.layout.layout_card, container,false); 
    img_pan=(ImageView)pan_layout.findViewById(R.id.img_pan); 
    txt_name=(TextView)pan_layout.findViewById(R.id.txt_name); 
    txt_father=(TextView)pan_layout.findViewById(R.id.txt_father); 
    txt_dob=(TextView)pan_layout.findViewById(R.id.txt_dob); 
    txt_panid=(TextView)pan_layout.findViewById(R.id.txt_panid); 
    Bundle bundle=getArguments(); 
    String name=bundle.getString("name"); 
    String father=bundle.getString("father"); 
    String dob=bundle.getString("dob"); 
    String id=bundle.getString("id"); 
    //String filePath=bundle.getString("filepath"); 
    //Bitmap bmp=BitmapFactory.decodeFile(filePath); 
    //img_pan.setImageBitmap(bmp); 
    txt_name.setText(name); 
    txt_father.setText(father); 
    txt_dob.setText(dob); 
    txt_panid.setText(id); 
    return pan_layout; 
} 

已經重構了代碼轉換成用於測試的單獨的應用程序......我已禁用的文件路徑。 感謝Luksprog的答案。

+1

這將是偉大的,如果你發佈你的LogCat也介入你的問題更加可視 –

回答

1

我已經在framelayout中包含了一個片段......並且我正在使用framelayout獲取片段的子視圖 。

首先,爲了實際的片段綁定到活動中,您需要調用commit()方法對你FragmentTransaction

即使你這樣做,你的代碼仍然會失敗,因爲片段事務是異步的,當你調用commit()時它不會發生,它將在主UI線程完成當前消息的將來被調度。要解決此問題,您可以在提交後使用FragmentManager上的executePendingTransactions()電話(但這不是正確的路線)。

您不應該嘗試直接使用片段的視圖。在嘗試初始化片段的視圖時,應通過在實例化片段時將具有數據參數的Bundle傳遞給片段,或者通過使片段直接從活動中檢索片段來完成此操作(使用getActivity()並投射它對你的活動)在其回調之一(如onCreateView())。

+0

添加更改的文件,這似乎是在撤回參數時導致錯誤... – vamsiampolu

+1

@ user2309862哪一個是行'39 'CardFragment'中?另外,在你的'onCreateView()'方法中,你應該先**膨脹'R.layout.layout_pan'佈局,然後在膨脹的佈局中查找視圖(而不是在容器中!)。同時返回以前充氣的視圖(並且不要再次充氣)。 – Luksprog

+0

String name = bundle.getString(「pan_name」);是39行。我應該使用getArguments還是savedInstanceState。 – vamsiampolu

相關問題