Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找到片段视图元素,并在异步任务完成后更新它们_Java_Android_Android Fragments_Android Asynctask - Fatal编程技术网

Java 找到片段视图元素,并在异步任务完成后更新它们

Java 找到片段视图元素,并在异步任务完成后更新它们,java,android,android-fragments,android-asynctask,Java,Android,Android Fragments,Android Asynctask,我有一个包含多个片段的活动。每个片段实现我的UpdateRequest接口,其中每个片段执行一些异步任务并从web服务下载数据。我的问题是,我真的不知道如何更新现有的片段。我读了这个 但我仍然不知道如何定位每个片段的视图,然后更新它们。我举个例子: 有一个UserFragment,它应该表示用户配置文件: public class UserFragment extends SherlockFragment implements UpdateRequest { View

我有一个包含多个片段的活动。每个片段实现我的UpdateRequest接口,其中每个片段执行一些异步任务并从web服务下载数据。我的问题是,我真的不知道如何更新现有的片段。我读了这个

但我仍然不知道如何定位每个片段的视图,然后更新它们。我举个例子:

有一个UserFragment,它应该表示用户配置文件:

public class UserFragment extends SherlockFragment implements
        UpdateRequest {

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_user_info,
                container, false);
        return rootView;
    }
    ...
    public void update(final OnFragmentUpdatedListener context) {
    ...
    asyntask
     ...
    }

}
片段用户信息如下所示:

<RelativeLayout 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" >



    <ImageView
        android:id="@+id/fragment_user_picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/image_of_user" />

    <TextView
        android:id="@+id/fragment_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user_info" />

</RelativeLayout>


我想要的是定位UserFragment的实例,然后在其AsyncTask.onPostExecute()方法中将fragment\u user\u name textView中的文本更改为AsyncTask返回的内容

最简单的方法是在片段中创建最后一个变量来保存对TextView的引用。然后AsyncTask作为片段的匿名内部类,将直接访问它。比如说:

<RelativeLayout 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" >



    <ImageView
        android:id="@+id/fragment_user_picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/image_of_user" />

    <TextView
        android:id="@+id/fragment_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user_info" />

</RelativeLayout>
公共类UserFragment扩展SherlockFragment实现UpdateRequest{

private View rootView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_user_info, container, false);
    return rootView;
}
...
public void update(final OnFragmentUpdatedListener context) {
    final TextView userNameView = (TextView) rootView.findViewById(R.id.fragment_user_name);
    ...
    asyntask
    ...
    onPostExecute() {
        userNameView.setText(....);
    }

}

findByID()应该是findViewById(int),它将返回对要更新的TextView的引用。OP应该能够从onPostExecute()调用findViewById()@greybearedgeek,如果你修正了你的答案,我会投票表决。我也在考虑这个解决方案,但我如何确保在更新之前调用onCreateView?那么rootView将为null a我们都知道这将如何结束。好的,我终于找到了合适的解决方案。我按照你的建议做了,但这返回了NullPointerException,因为更新()如我所料,在onCreateView之前被调用。因此我将“final TextView userNameView=(TextView)rootView.findViewById(R.id.fragment_user_name);”代码移动到onPostCreate,现在它工作得很好。