android - 当Fragment处于Resume状态时,如何将TEXT设置为Fragment的TextView?

标签 android android-fragments

我在AsyncTask的帮助下从互联网获取数据,并将该数据发送到Fragment_subclass(它实现接口(interface) )通过创建 Fragment_subclassobject 来使用 onPostExecute()

编辑:

textView位于fragment的布局内

txt = ( TextView ) findViewById     (R.id.txt ) ;

oPostExecute()属于async_task类。

protected void onPostExecute (String result)
{
    browse ob_br = new browse() ;
    ob_br.implement_it_();
}

browse 是一个 fragment ,它正在实现 interface ,其名称为 implement_it_

public class browse extends Fragment implements implement_it_
{
    -------------------- // code ....
    public void implement_it_() 
    {
         TextView txt_1 = ( TextView ) getActivity().findViewById ( R.id.txt) ;
         txt_1.setText("hello");
    }
}

已编辑:错误,

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.the_hindu_news.browse.implement_it_(browse.java:38)
at com.example.the_hindu_news.Async_list.onPostExecute(Async_list.java:56)
at com.example.the_hindu_news.Async_list.onPostExecute(Async_list.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
GC_CONCURRENT freed 185K, 12% free 2578K/2904K, paused 10ms+5ms, total 237ms

实际上,我面临的问题是......

我还没有初始化我的 fragment 类,我使用它就像 new browser() 作为传递变量,所以它会产生问题,因为每次获取新变量时。

最佳答案

如果你想给textView设置文本值。你只需按照下面的代码操作即可。 它正在 fragment 中工作。

公共(public)类 CafeBarHome 扩展 Fragment{

Button home;
TextView txt;
@Override   
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState){

    LinearLayout cafeLayout = (LinearLayout) inflater.inflate(R.layout.cafehomefrag1,container, false);

     Intent i = getActivity().getIntent();
    home = (Button) cafeLayout.findViewById(R.id.btn_cafehome);
    txt = (TextView) cafeLayout.findViewById(R.id.txt1);
    txt.setText("Rio Web solution");
    String h = i.getStringExtra("value");
    Log.e("Second Screen", h);

    return cafeLayout;
}

}

关于android - 当Fragment处于Resume状态时,如何将TEXT设置为Fragment的TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15516315/

相关文章:

android - Monaca.io 无法通过添加 cordova google map 插件来构建

java - 在 Activity 中访问 Fragment 的 textView 时出现 NullPointerException

android - Parcelable with arraylist<CustomClass> 将值从 Activity 传递到 fragment android

android - 带有自定义 ArrayAdapter 和 Filter 的 AutoCompleteTextView

java - flutter : Android license status unknown

安卓工作室 : add virtual folder in gradle

java - Android 中频繁更改 View 的最佳方法?

android - 具有多个待定 Intent 的 ListView 小部件

android - 使用 "id"作为来自 FragmentActivity 的 "startActivityForResult"的 requestCode

android - Android 中哪一种更好的设计方法 - 多个 Activity/Fragments