我想要做的是使用片段填充Viewpager中的listView,但由于主类扩展FragmentActivity
而Fragment
不是ListActivity
或者我应该使用ListFragment?在LogCat中指向此mViewPager.setAdapter
public class tabs extends FragmentActivity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
* three primary sections of the app. We use a {@link android.support.v4.app.FragmentPagerAdapter}
* derivative, which will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
/**
* The {@link ViewPager} that will display the three primary sections of the app, one at a
* time.
*/
ViewPager mViewPager;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_main_groups_activty);
db = new DatabaseHandler(this);
mContext = getBaseContext();
//Need this for not getting null pointer exception
productsList = new ArrayList<HashMap<String, ?>>();
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
// Specify that the Home/Up button should not be enabled, since there is no hierarchical
// parent.
actionBar.setHomeButtonEnabled(false);
//actionBar.setDisplayShowTitleEnabled(false);
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager, attaching the adapter and setting up a listener for when the
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter); //Logacat Error: at line 127
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
/**
* A fragment that launches other parts of the demo application.
*/
public static class LaunchpadSectionFragment extends ListFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);
Holder holder = new Holder();
View rootView = inflater.inflate(R.layout.load_main_groups_activty, container, false);
LoadAllProducts task = new LoadAllProducts();
task.execute();
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(
mContext, productsList,
R.layout.load_main_groups_listview, FolderName);
holder.lv= (ListView)rootView.findViewById(R.id.listView);
holder.lv.setAdapter(adapter);
return rootView;
}
class Holder {
ListView lv;
ViewPager pager;
}
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(
mContext, productsList,
R.layout.load_main_groups_listview, FolderName);
setListAdapter(adapter);
}
});
}
}
}
load_main_groups_activty ,还有另一种可自定义的布局 查看主要布局 load_main_groups_listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white"
>
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
>
</ListView>
</RelativeLayout>
logcat的:
07-18 10:27:34.366: E/AndroidRuntime(13948): FATAL EXCEPTION: main
07-18 10:27:34.366: E/AndroidRuntime(13948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jinisys.restoplusordering/com.jinisys.restoplusordering.tabs}: java.lang.NullPointerException
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.os.Looper.loop(Looper.java:137)
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-18 10:27:34.366: E/AndroidRuntime(13948): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 10:27:34.366: E/AndroidRuntime(13948): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 10:27:34.366: E/AndroidRuntime(13948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-18 10:27:34.366: E/AndroidRuntime(13948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 10:27:34.366: E/AndroidRuntime(13948): at dalvik.system.NativeStart.main(Native Method)
07-18 10:27:34.366: E/AndroidRuntime(13948): Caused by: java.lang.NullPointerException
07-18 10:27:34.366: E/AndroidRuntime(13948): at com.jinisys.restoplusordering.tabs.onCreate(tabs.java:121)
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.app.Activity.performCreate(Activity.java:5008)
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-18 10:27:34.366: E/AndroidRuntime(13948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-18 10:27:34.366: E/AndroidRuntime(13948): ... 11 more
答案 0 :(得分:0)
长期和简短的回答是永远不要使用ListActivity和ListFragment或其任何变体。
只需使用常规片段和活动,
ListActivity和ListFragment是如此有限,并试图隐藏和耦合既不应隐藏也不应耦合的东西。
Vogella有一个很棒的教程
http://www.vogella.com/tutorials/AndroidListView/article.html
片段中的用法没有区别。