简体   繁体   English

包含片段的帧布局中的视图的findViewById失败

[英]findViewById of a view from the framelayout enclosing a fragment fails

I have enclosed a fragment in a framelayout...and I am getting the child views of the fragment using the 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);
}

When I try to add values to the view,it throws a NullPointerException 当我尝试向视图添加值时,它抛出NullPointerException

EDIT:It was working just fine before I seperated everything into a Fragment.. 编辑:在我将所有内容分离成一个片段之前,它工作得很好。

EDIT2:I have changed the Activity and the Fragment as suggested,the Activity onCreate: 编辑2:我已经按照建议更改了活动和片段,活动在创建时:

   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");
    }

The fragment onCreateView method: 片段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);

}

Now it still returns a NullPointerException,the logcat seems to be pointed at the getArguments().I have tried using saveInstanceState without success. 现在它仍然返回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)

Edit 3 This code is working: 编辑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;
}

Having refactored the code into a seperate app for testing...I have disabled the filePath. 将代码重构为一个单独的应用程序进行测试...我已禁用filePath。 Thanks to Luksprog for the answer. 感谢Luksprog的回答。

I have enclosed a fragment in a framelayout...and I am getting the child views of the fragment using the framelayout. 我已经将片段封装在框架布局中...并且正在使用框架布局获取该片段的子视图。

First of all, in order to actually bind the fragment to the activity you need to call the commit() method on your FragmentTransaction . 首先,为了将片段实际绑定到活动,您需要在FragmentTransaction上调用commit()方法。

Even if you do this, your code will still fail because a fragment transaction is asynchronous, it doesn't happen when you call commit() , it will be scheduled in the future when the main UI thread finishes the current message. 即使执行此操作,您的代码仍然会失败,因为片段事务是异步的,当您调用commit()时不会发生这种情况,它将在将来主UI线程完成当前消息时进行调度。 To solve this you could call executePendingTransactions() on the FragmentManager you use after committing(but this is not the proper route). 为了解决这个问题,您可以在提交后使用的FragmentManager上调用executePendingTransactions() (但这不是正确的路由)。

You shouldn't try to work with the views of the fragment directly. 您不应该尝试直接使用片段的视图。 As you're trying to initialize the views of the fragment, this should be done either through passing a Bundle with the data as arguments to the fragment when you instantiate it or by making the fragment retrieve it directly from the activity(using getActivity() and casting it to your activity) in one of its callbacks(like onCreateView() ). 当您尝试初始化片段的视图时,应通过实例化片段时将带有数据的Bundle传递给片段作为参数来实现,或者通过使片段直接从活动中检索它来实现(使用getActivity()并将其转换为您的活动)在其回调之一(例如onCreateView() )中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM