【发布时间】:2014-09-16 05:16:57
【问题描述】:
我正在制作一个使用滑动视图显示数据的应用。我在所有类中都使用了 fragmentactivity,但它会导致 tabpagedapater 出现错误,即它与 fragmentstatepageadapter 不兼容我该如何解决这个问题?
Tabpageadapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public FragmentActivity getItem(int i) {
switch (i) {
case 0:
//Fragement for Android Tab
return new Android();
case 1:
//Fragment for Ios Tab
return new Ios();
case 2:
//Fragment for Windows Tab
return new Windows();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
}
主要活动
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.learn2crack.library.DatabaseHandler;
import java.util.HashMap;
public class Android extends FragmentActivity {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View android = inflater.inflate(R.layout.android_frag, container, false);
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
HashMap<String,String> user = new HashMap<String, String>();
user = db.getUserDetails();
/**
* Displays the registration details in Text view
**/
final TextView fname = (TextView)findViewById(R.id.fname);
final TextView lname = (TextView)findViewById(R.id.lname);
final TextView uname = (TextView)findViewById(R.id.uname);
final TextView email = (TextView)findViewById(R.id.email);
final TextView created_at = (TextView)findViewById(R.id.regat);
fname.setText(user.get("fname"));
lname.setText(user.get("lname"));
uname.setText(user.get("uname"));
email.setText(user.get("email"));
created_at.setText(user.get("created_at"));
return android;
}}
【问题讨论】:
-
在你的所有片段中,将“extends FragmentActivity”更改为“extends Fragment”
-
如何通过片段扩展类来使用 findviewbyid 和 getapplicationcontext?
-
为此你可以这样做“getActivity().getApplicationContext()”并使用“findViewById”你必须这样做:yourView.findViewById() 这里 yourView 是你膨胀的视图你的片段。
标签: android android-activity fragment android-fragmentactivity