封闭片段的framelayout视图的findViewById失败

时间:2013-09-30 13:16:31

标签: android android-fragments nullpointerexception android-framelayout

我在framelayout中附加了一个片段......我正在获取片段的子视图 使用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);

}

现在它仍然返回一个NullPointerException,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;
}

将代码重构为单独的应用程序以进行测试...我已禁用filePath。 感谢Luksprog的回答。

1 个答案:

答案 0 :(得分:1)

  

我在一个framelayout中附上了一个片段......我得到了   使用framelayout创建片段的子视图。

首先,为了将片段实际绑定到活动,您需要在commit()上调用FragmentTransaction方法。

即使你这样做,你的代码仍会失败,因为片段事务是异步的,当你调用commit()时它不会发生,它将在以后当主UI线程完成当前时安排信息。要解决此问题,您可以在提交后使用的executePendingTransactions()上调用FragmentManager(但这不是正确的路线)。

您不应该尝试直接使用片段的视图。当您尝试初始化片段的视图时,应该通过在实例化时将数据作为参数传递给片段的Bundle或通过使片段直接从活动中检索它来完成(在其中一个回调中使用getActivity()并将其投射到您的活动中)(例如onCreateView())。