gpt4 book ai didi

android - 包含 fragment 的框架布局中的 View 的 findViewById 失败

转载 作者:行者123 更新时间:2023-11-29 01:49:32 25 4
gpt4 key购买 nike

我在框架布局中包含了一个 fragment ......我正在获取该 fragment 的 subview 使用框架布局。

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

当我尝试向 View 添加值时,它抛出 NullPointerException

编辑:在我将所有内容分成 fragment 之前它工作得很好..

EDIT2:我已经按照建议更改了 Activity 和 Fragment,Activity 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");
}

fragment 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;
}

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

最佳答案

I have enclosed a fragment in a framelayout...and I am getting the child views of the fragment using the framelayout.

首先,为了将 fragment 实际绑定(bind)到 Activity ,您需要调用 FragmentTransaction 上的 commit() 方法。

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

您不应尝试直接使用 fragment 的 View 。当您尝试初始化 fragment 的 View 时,这应该通过在实例化 fragment 时将数据作为参数传递给 fragment 或让 fragment 直接从中检索来完成。 Activity (使用 getActivity() 并将其转换为您的 Activity )在其回调之一(如 onCreateView())中。

关于android - 包含 fragment 的框架布局中的 View 的 findViewById 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19095266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com