1
我正在使用viewpager和tabspageradapter。在我的主要活動中,我有一個屬性來安全選擇選項卡中的當前片段:current_tab。它會在選擇新選項卡時更改其值,但是我不知道如何在開始時設置它的值,因爲我需要attribut更新我的片段中的listview。我嘗試了很多東西,但是我在大多數時候都得到了nullpointerexception,誰能告訴我它是如何工作的?如何使用supportfragmentmanager與findfragmentbytag獲得第一個片段
代碼我TabsPagerAdapter的:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int index){
switch(index) {
case 0:
return new AllShowsFragment();
case 1:
return new UnwatchedShowsFragment();
case 2:
return new WatchedShowsFragment();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
主要活動的最重要的部分代碼:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
public static FragmentCommunicater fragmentCommunicater;
public static boolean simple_progress;
public static int editmode;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private DatabaseHandler db;
private Show current_show;
private static MyFragment current_tab;
private boolean first_launch=true;
private String[] tabs = { "All Shows", "Unwatched", "Watched" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHandler(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);
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) {
}
});
}
public void onResume(){
super.onResume();
getSettings();
}
public void getSettings() {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
simple_progress = sharedPrefs.getBoolean("simple_progress_key", false);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + viewPager.getCurrentItem());
if ((viewPager.getCurrentItem() == 0 || viewPager.getCurrentItem() == 1 || viewPager.getCurrentItem() == 2) && (page != null)) {
current_tab = ((MyFragment) page);
current_tab.update();
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
public static void update(){
current_tab.update();
}
}
我的片段類在那裏我得到了新的空指針異常ONY:
public class AllShowsFragment extends MyFragment {
private SQLiteDatabase newDB;
private ListView lv;
private List<Show> showList = new ArrayList<Show>();
private int position;
private DatabaseHandler db;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_shows, container, false);
lv = (ListView) rootView.findViewById(R.id.mainlist);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
position = pos;
createClickMenu();
return true;
}
});
return rootView;
}
public void update(){
populateListfromDB();
}
public void populateListfromDB() {
db = new DatabaseHandler(this.getActivity());
newDB = db.getWritableDatabase(); //Exception at this line
showList.clear();
Cursor cursor = newDB.rawQuery("SELECT id as _id, title, cseason, cepisode, watched FROM "
+ db.TABLE_SHOWS, null);
cursor.moveToFirst();
if (cursor != null & cursor.moveToFirst()) {
cursor.moveToFirst();
do {
Show show = new Show(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
cursor.getString(2), cursor.getString(3),
Integer.parseInt(cursor.getString(4)));
showList.add(show);
} while (cursor.moveToNext());
}
myCursorAdapter = new CustomAdapter(this, showList,((MainActivity)this.getActivity()).editmode,
((MainActivity)this.getActivity()).simple_progress);
lv.setAdapter(myCursorAdapter);
}
}
例外,前幾行:
Caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at wrodarczyk.com.showtracker.AllShowsFragment.populateListfromDB(AllShowsFragment.java:55)
at wrodarczyk.com.showtracker.AllShowsFragment.update(AllShowsFragment.java:50)
at wrodarczyk.com.showtracker.MainActivity.update(MainActivity.java:183)
at wrodarczyk.com.showtracker.MainActivity.onTabSelected(MainActivity.java:134)
at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:506)
at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:492)
at wrodarczyk.com.showtracker.MainActivity.onCreate(MainActivity.java:53)
感謝,這對我幫助很大,但現在我得到一個空指針異常我的片段類,我編輯與新的片段代碼的帖子,會很好,如果你看看它 – mrtn 2014-10-11 18:49:32
不客氣。 StackOverflow問題應該關注於一個問題。當一個問題解決後,您應該接受最佳答案,如果您有其他問題,請發佈新問題。 – 2014-10-11 18:56:38
好的抱歉,我會發佈一個新問題,再次感謝您的幫助! – mrtn 2014-10-11 18:58:36