如何在片段中设置UI值

时间:2014-09-17 11:17:58

标签: java android android-fragments

我有一个由片段布局组成的主动布局。现在我已经创建了一个片段类并在片段类中实现了asyncTask。现在在postExecute上我试图设置片段视图的值,就像尝试为TextView设置文本一样。 但我得到NullPointerException。

我的代码段是:

public class MainHandlerFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new MyTask().execute("xyzname");
    }   

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        return view;
    }
public class MyTask extends AsyncTask<String, Void, Organization> {
        @Override
        protected String doInBackground(String... urls) {
            // getting api calls
        }
        @Override
        protected void onPostExecute(String str) {
            loadData(str);
        }
    }
public void loadData(String str){
        getSupportFragmentManager().beginTransaction()
        ((TextView) findViewById(R.id.text_frag)).setText("name"); // at this line getting null pointer exception
    }
}
} 

main_layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

 <fragment
     android:id="@+id/home_fragment"
     android:name="com.stata.mobile.android.ui.MainHandlerFragment"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"/>

</FrameLayout>

home_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_top_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_frag"/>
</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

您可以在以下行创建视图:

View view = inflater.inflate(R.layout.home_fragment, container, false);

要获取TextView,您必须在此findViewById()个对象上调用view

public class MainHandlerFragment extends Fragment {

    //added this line
    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new MyTask().execute("xyzname");
    }   

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //updated this line
        view = inflater.inflate(R.layout.home_fragment, container, false);
        return view;
    }

    public class MyTask extends AsyncTask<String, Void, Organization> {
        @Override
        protected String doInBackground(String... urls) {
            // getting api calls
        }
        @Override
        protected void onPostExecute(String str) {
            loadData(str);
        }
    }
    public void loadData(String str){
        getSupportFragmentManager().beginTransaction()
        //updated this line
        ((TextView) view.findViewById(R.id.text_frag)).setText("name");
    }
}

答案 1 :(得分:2)

定义TextView字段以保存您的用户界面TextView

public class MainHandlerFragment extends Fragment {
private TextView mTextView;

覆盖onViewCreated()并调用worker以onViewCreated()方法执行:

@override
public void onViewCreated(View view, Bundle savedInstanceState)
{
    new MyTask().execute("xyzname");
}

onCreateView()添加此行:

mTextView = (TextView) view.findViewById(R.id.text_frag);

像这样更改loadData(String str)

public void loadData(String str){
    getSupportFragmentManager().beginTransaction()
    mTextView.setText("name");
}

答案 2 :(得分:1)

TextView 使用全局变量。在 onCreateView 中引用它,并在该引用上调用 setText

public class MainHandlerFragment extends Fragment {
    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new MyTask().execute("xyzname");
    }   

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        textView = (TextView) view.findViewById(R.id.text_frag);
        return view;
    }

    public class MyTask extends AsyncTask<String, Void, Organization> {
        @Override
        protected String doInBackground(String... urls) {
            // getting api calls
        }
        @Override
        protected void onPostExecute(String str) {
            loadData(str);
        }
    }

    public void loadData(String str){
        getSupportFragmentManager().beginTransaction() // you miss something here
        textView.setText("name"); // should be ok now
    }
}

}