我在fragment_tab.xml中创建了一个微调器。我试图从MainActivity中的arrayadapter填充微调器,但它给出了空指针异常。 我的问题是,具体的方法是什么?经过大量搜索,我找不到方向。 这是我的MainActivity:
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
Toolbar toolbar;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
Spinner spinner;
List<String> places;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayoutConfigure();
AddSpinner();
}
public void tabLayoutConfigure()
{
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
public void AddSpinner()
{
this.spinner= (Spinner)findViewById(R.id.spinner);
places= new ArrayList<String>();
places.add("Regional office");
places.add("Farm office");
ArrayAdapter<String> dataAdapter= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,places);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
这是我的TabFragment类(片段):
public class TabFragment extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab,container,false);
}
}
activity_main.xml中
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/textColorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/textColorPrimary" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:background="#689F38"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enrollment place"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner" />
任何建议都会有很大的帮助!
答案 0 :(得分:3)
我不太确定你想要实现什么,但我发现你在活动中初始化微调器,你是否在片段布局或活动布局中声明了“R.id.spinner”。如果您已在片段布局中声明它,则活动将无法找到该视图。您可以移动片段中的微调器代码。
但是,如果你可以发布活动和片段xml,很容易弄明白。
答案 1 :(得分:0)
通过在片段中添加方法来完成:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_tab,container,false);
AddSpinner(view);
return view;
}
public void AddSpinner(View view)
{
spinner= (Spinner)view.findViewById(R.id.spinner2);
places= new ArrayList<String>();
places.add("Regional office");
places.add("Farm office");
ArrayAdapter<String> dataAdapter= new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item,places);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
使用 spinner =(Spinner)view.findViewById(R.id.spinner) 正在解决这个问题。 特别感谢Praveen Pandey和这个问题:Error populating spinner in fragment