我在AsyncTask
的帮助下从互联网
获取数据,并将该数据发送到Fragment_subclass
(它实现接口(interface)
)通过创建 Fragment_subclass
的 object
来使用 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/