Android 当片段处于恢复状态时,如何将文本设置为片段的TextView?
我在Android 当片段处于恢复状态时,如何将文本设置为片段的TextView?,android,android-fragments,Android,Android Fragments,我在AsyncTask的帮助下从互联网获取数据,并通过创建片段子类的对象,将数据发送到片段子类(它实现接口) 编辑: 此textView位于片段的布局中 txt = ( TextView ) findViewById (R.id.txt ) ; 此oPostExecute()属于async\u任务类 protected void onPostExecute (String result) { browse ob_br = new browse() ; ob_br.imp
AsyncTask
的帮助下从互联网
获取数据,并通过创建片段子类
的对象
,将数据发送到片段子类
(它实现接口
)
编辑:
此textView
位于片段的布局中
txt = ( TextView ) findViewById (R.id.txt ) ;
此oPostExecute()
属于async\u任务类
protected void onPostExecute (String result)
{
browse ob_br = new browse() ;
ob_br.implement_it_();
}
browse
是一个片段
,它正在实现,接口
,其名称为implement\u 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
事实上,我面临的问题是
我还没有初始化我的片段类&我像new browse()一样使用它作为传递变量,因此它产生了问题,因为每次提取新变量时。通过
TextView tv = (TextView) findViewById(R.id.textview_id);
现在将文本设置为
tv.setText("Your_text_here");
祝你好运 片段的getActivity()
方法将返回null,直到调用onActivityCreated()
之后。因此,您不能在使用它的地方使用它,否则将得到NullPointerException
此外,除非您在活动中执行FragmentTransaction
,否则您的片段不属于任何活动。如果要将文本值设置为textView,我建议您阅读。您只需遵循以下代码。
它正在断断续续地工作
公共类CafeBarHome扩展片段{
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;
}
}@Tushar。。。请看我编辑的部分!你在Oncreat()方法中提交片段吗?Kunal S.Kushwah。。。请参阅我编辑的部分以了解错误。browse.java:38——这是获取textview的那一行吗?Kunal S.Kushwah,通常认为我正在设置文本。第38行的nullpointerException(如果是获取textview的那一行)表明它不作为资源存在。主要问题可能是2-1。您的活动上下文不正确(请尝试获取正确的上下文)2。您的TextView资源对于我添加的上下文未定义,我的活动中的片段将其视为我想在onResume()上更改我的活动@user2147072我真的不知道您想做什么,但是当您创建浏览
的新实例时,它不会在实现中为您提供非空活动,除非您首先将片段附加到活动。