Java 我的viewpager实现在片段/活动方面让我很困惑
我正在运行一个选项卡寻呼机适配器,但我似乎无法理解如何实现不在主活动中的代码。我编写的一些代码要求我扩展活动,使之不可能,因为我已经扩展了片段。有人能给我一些启发吗Java 我的viewpager实现在片段/活动方面让我很困惑,java,android,android-activity,android-fragments,android-asynctask,Java,Android,Android Activity,Android Fragments,Android Asynctask,我正在运行一个选项卡寻呼机适配器,但我似乎无法理解如何实现不在主活动中的代码。我编写的一些代码要求我扩展活动,使之不可能,因为我已经扩展了片段。有人能给我一些启发吗 public class Home extends Fragment{ ListView list; String url = "http://blog.compassion.com/living-in-the-philippines-rural-life/"; ProgressDialog mProgres
public class Home extends Fragment{
ListView list;
String url = "http://blog.compassion.com/living-in-the-philippines-rural-life/";
ProgressDialog mProgressDialog;
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
TextView titlebutton = (TextView) getActivity().findViewById(R.id.TextView01);
titlebutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Title().execute();
}
});
return inflater.inflate(R.layout.home_fragment, container);
}
private class Title extends AsyncTask<Void, Void, Void> {
String body;
String title;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document document = Jsoup.connect(url).get();
Elements elements = document.select("p");
title = document.title();
for(Element elements123 : elements){
body+=elements123.text();
System.out.println(elements123.text());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
TextView txttbody = (TextView) getActivity().findViewById(R.id.textView2);
txttbody.setMovementMethod(new ScrollingMovementMethod());
txttbody.setText(title +"\n"+body);
mProgressDialog.dismiss();
}
}
public类Home扩展片段{
列表视图列表;
字符串url=”http://blog.compassion.com/living-in-the-philippines-rural-life/";
进程对话框;
创建视图上的公共视图(更平坦的充气机,
@可为空的视图组容器,@Nullable Bundle savedInstanceState){
//TODO自动生成的方法存根
TextView标题按钮=(TextView)getActivity().findViewById(R.id.TextView01);
titlebutton.setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
新标题().execute();
}
});
返回充气机。充气(R.layout.home_碎片,容器);
}
私有类标题扩展了异步任务{
弦体;
字符串标题;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
mProgressDialog=newprogressdialog(getActivity());
setTitle(“Android基本JSoup教程”);
设置消息(“加载…”);
mProgressDialog.setUndeterminate(false);
mProgressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
Document Document=Jsoup.connect(url.get();
元素=文件。选择(“p”);
title=document.title();
用于(元素123:元素){
body+=元素123.text();
System.out.println(elements123.text());
}
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
TextView txtBody=(TextView)getActivity().findViewById(R.id.textView2);
setMovementMethod(新的ScrollingMovementMethod());
txttbody.setText(title+“\n”+body);
mProgressDialog.disclose();
}
}
要在主类中使用此viewPager,您需要扩展类FragmentActivity。viewPager活动不是片段,但viewPager每个选项卡内的页面都是片段,因此您可以使用FragmentActivity来控制它们。此外,请确保在导入的android.support.v4版本可用时使用它们 这将在您的onCreatView中解决您的NPE
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view v = inflater.inflate(R.layout.home_fragment, container, false);
// TODO Auto-generated method stub
TextView titlebutton = (TextView) v.findViewById(R.id.TextView01);
titlebutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Title().execute();
}
});
return v;
}
你问了什么问题吗,你只是告诉我们你的心情。有什么问题吗?我的代码不工作。它在onCreateView部分上有空指针。但是我应该如何设置每个片段的contentView?每当我尝试在这些片段上写一些函数时,它们似乎都没有反映出来。步骤1:Home扩展FragmentActivity。步骤2:获取操作步骤3:actionBar.setNavigationMode(ActionBar.NAVIGATION\u MODE\u TABS步骤4:为操作栏选项卡设置侦听器。在布局文件中,您需要将android.support.v4.view.ViewPager作为根布局。您需要为每个片段创建单独的内容视图。查看此页面。我有一个getItem方法,返回片段类型。它有一个开关,返回片段依赖项索引上的g。我该怎么办?@LeoComia看看这个。它说指定的孩子已经有了父母?我不知道Post行号,但NPE已经走了是吗?