我在FrameActivity中遇到问题,使用FragmentPagerAdapter来声明我的可滑动标签。其中一个选项卡包含Spinner,我想从中收集数据库中的数据。首先,我要发布我的FrameActivity
public class TranscationMain extends FragmentActivity implements
ActionBar.TabListener {
Context context;
private ViewPager viewPager;
Spinner spinner_question ;
ContentValues values;
SQLiteDatabase sql_lite_db_obj;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
Cursor cursor;
int i =0;
// Tab titles
private String[] tabs = { "Shipped In", "shipped Out", "New Entry" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transcation_main);
AndroidContext.setContext(this);
sql_lite_db_obj = DatabaseHelper.getInstance().getDb();
values = new ContentValues();
// Initilization
context = this;
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
spinner_question = (Spinner)findViewById(R.id.spinner_category);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(arg0.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
public void clickHandler(View v)
{
if (v.getId() == R.id.btn_new_category_id) {
// btn click event that define in NewEntry.java calss
// click on this button alert dialog will open with editext
// from where i insert data in database. which i have to show in
// spinner which define in same activity. Insertion of data is
// done successfully.
}
}
private void insertData(String category) {
// method to insert data in database
// working fine
}
现在这是上面代码的异常部分
private void loadSpinnerData() {
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
List<String> lables = db.getAllLabels();
System.out.println(" data in array : "+lables);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
System.out.println(" dapater : "+dataAdapter);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner_question.setAdapter(dataAdapter);
}
我想在此块中调用此方法
if (v.getId() == R.id.btn_new_category_id) {
loadSpinnerData()
}
但总是我得到一个NullPointer异常。只是因为
的初始化 spinner_question = (Spinner)findViewById(R.id.spinner_category);
Spinner未在TranscationMain的布局中定义,它在Fragment
下面定义 public class NewEntry extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
spnr = new Spinner(getActivity());
View rootView = inflater.inflate(R.layout.new_entry, container, false);
return rootView;
}
}
这就是为什么我在下面的行中获得例外。
spinner_question.setAdapter(dataAdapter);
现在我不明白我该如何解决这个问题。如何在Fragment中的微调器中绑定数据。我是片段活动的新手。在此先感谢所有
答案 0 :(得分:1)
Spinner is not define in above TranscationMain's layout, It define in below Fragment
这就是崩溃的原因。
findViewById
在当前膨胀的布局中查找视图。
如果微调器在片段中,则需要在片段中初始化它,然后将数据库中的项目加载到片段中的微调器。
片段
Spinner spinner_question;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_entry, container, false);
spinner_question = (Spinner)rootView.findViewById(R.id.spinner_category);
return rootView;
}