gpt4 book ai didi

android - 如何获取动态 fragment 的 View ?

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

我创建了动态 fragment ,我想获取 fragment View 。这是我的 fragment :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/fragmenttext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

fragment 类:

package webviewtest.webviewtest;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.my_fragment, container, false);
return myFragmentView;
}
}

和我的 Activity 布局 fragment :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<FrameLayout
android:id="@+id/myfragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

然后在“onCreate”方法中尝试获取 fragment 的“TextView”

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.myfragment, new MyFragment());
ft.commit();

TextView textFragment = (TextView) findViewById(R.id.fragmenttext);
textFragment.setText("Some text");

但是我得到了“Null Pointer Exception”,我的错误在哪里?

最佳答案

问题出在这里:ft.commit();

来自documentation 提交():

Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.

因此,仅仅因为您调用了 commit(),并不意味着该 fragment 在 Activity 中膨胀了,即,不需要立即调用 onCreateView()

那么如何处理呢?

好吧,您需要通过Interface 来处理这个问题。创建一个接口(interface)并让您的 Activity 实现它。然后在 fragment 中的 onActivityCreated() 方法中调用接口(interface)中的方法。在该方法中,您将对 TextView 执行 findViewById()。我已经确定了如何实现它的框架。

  public interface IFragmentListener{
void fragmentInitialized();
}


 public MainActivity extends Activity implements IFragmentListener{

public void onCreate(Bundle savedInstanceState){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
MyFragment myFragment = new MyFragment();
myFragment.setFragmentListener(this);
ft.add(R.id.myfragment, myFragment);
ft.commit();
}

public void fragmentInitialized(){
TextView textFragment = (TextView) findViewById(R.id.fragmenttext);
textFragment.setText("Some text");
}
}


public class MyFragment extends Fragment {

IFragmentListener listener;

public void setFragmentListener(IFragmentListener listener){
this.listener = listener;
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(listener != null){
listener.fragmentInitialized();
}
}
}

当然,只有当您想从Activity 访问FragmentTextView 时,您才需要执行所有这些操作。如果您想从 fragment 中访问 if,则只需在 FragmentonActivityCreated() 方法中执行 findViewById()

关于android - 如何获取动态 fragment 的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33465207/

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