我的问题很简单:如何在Spinners
中使用(填充)Fragments
?或者更好地说,我的代码下面有什么问题?像我一样添加Spinner
就像它获得的一样简单,但在尝试了所有不同的方式后,没有任何效果。 FragmentPagerAdapter
与Spinner
有什么关系?如果我从其他地方声明的方法添加Spinner
,则spinner
会填充没有问题(例如,如果从spinner
填充button
)。
提前致谢
public class MainActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. 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}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
public List<String> fragments = new Vector<String>();
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
private MySQLite database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager); // pager
mViewPager.setAdapter(mSectionsPagerAdapter);
// We initialise the database
database = new MySQLite(this);
Spinner spin = (Spinner)findViewById(R.id.spinner1);
List<String> toSpin = new ArrayList<String>();
toSpin.add("ONE");
toSpin.add("TWO");
toSpin.add("THREE");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,toSpin);
spin.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
fragments.add(ConnectionFragment.class.getName());
fragments.add(DataFragment.class.getName());
}
@Override
public Fragment getItem(int position) {
// we need to instantiate the list of fragments
return Fragment.instantiate(getBaseContext(),
fragments.get(position));
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class ConnectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public ConnectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View connectionView = inflater.inflate(
R.layout.fragment_main_dummy, container, false);
return connectionView;
}
}
public static class DataFragment extends Fragment {
public DataFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View dataView = inflater.inflate(R.layout.fragment_linear,
container, false);
return dataView;
}
}
logcat的:
11-16 18:26:13.092: E/AndroidRuntime(16442): FATAL EXCEPTION: main
11-16 18:26:13.092: E/AndroidRuntime(16442): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iwallet/com.example.iwallet.MainActivity}: java.lang.NullPointerException
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.app.ActivityThread.access$600(ActivityThread.java:138)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.os.Looper.loop(Looper.java:213)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.app.ActivityThread.main(ActivityThread.java:4787)
11-16 18:26:13.092: E/AndroidRuntime(16442): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 18:26:13.092: E/AndroidRuntime(16442): at java.lang.reflect.Method.invoke(Method.java:511)
11-16 18:26:13.092: E/AndroidRuntime(16442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-16 18:26:13.092: E/AndroidRuntime(16442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-16 18:26:13.092: E/AndroidRuntime(16442): at dalvik.system.NativeStart.main(Native Method)
11-16 18:26:13.092: E/AndroidRuntime(16442): Caused by: java.lang.NullPointerException
11-16 18:26:13.092: E/AndroidRuntime(16442): at com.example.iwallet.MainActivity.onCreate(MainActivity.java:65)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.app.Activity.performCreate(Activity.java:5008)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-16 18:26:13.092: E/AndroidRuntime(16442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
11-16 18:26:13.092: E/AndroidRuntime(16442): ... 11 more
答案 0 :(得分:4)
如果你想在你的片段中制作你的微调器,也许你必须在 onCreatedView()中声明这些片段。不在FragmentActivity的 onCreate()中。