如何解决"无法暂停活动java.lang.nullpointerexception#34;?

时间:2014-06-23 06:37:35

标签: android listview tabs actionbarsherlock

我的Activity中有4个标签,我正在使用滑动更改它们。所以我使用4片段作为标签。滑动工作完美。但我的问题是,在我的Tab1中,我有一个列表视图,列表视图的每一行都包含两个按钮。所以按钮点击也很完美。但是当我从Tab1滑动到Tab3,并再次移动到Tab1,之后当我点击按钮时,它显示“无法暂停活动java.lang.nullpointerexception”。我不知道发生了什么。这是我的Adapter类的代码,我正在实现按钮单击。请帮帮我。

 public class StoreListViewAdapter extends ArrayAdapter<Item> {
Context context;
private ArrayList<Item> items;
private LayoutInflater layoutInflater;

public StoreListViewAdapter(Context context, int resourceId,
        ArrayList<Item> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items = items;
    layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
     EntryItem entryItem = (EntryItem) item;
     final Button buttonView = (Button) view
                .findViewById(R.id.button_view);
        final Button buttonOffline = (Button) view
                .findViewById(R.id.button_offline);
    buttonView.setTag(position);
        buttonView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, ViewButtonActivity.class);
                context.startActivity(i);
            }
        });

}

她是我的TabPagerAdapter: -

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {
    switch (index) {
    case 0:
        return new StoreFragment();
    case 1:
        return new NewsFragment();
    case 2:
        return new EventFragment();
    case 3:
        return new ContactUsFragment();
    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 4;
}

这是我的片段活动: -

public class MainActivity extends SherlockFragmentActivity{
private ViewPager viewPager;
private ActionBar actionBar;
Tab tab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.ExampleTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar = getSupportActionBar();
    FragmentManager fm = getSupportFragmentManager();
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    viewPager = (ViewPager) findViewById(R.id.pager);
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Find the ViewPager Position
            actionBar.setSelectedNavigationItem(position);
        }
    };

    viewPager.setOnPageChangeListener(ViewPagerListener);
    // Locate the adapter class called ViewPagerAdapter.java
    TabsPagerAdapter viewpageradapter = new TabsPagerAdapter(fm);
    // Set the View Pager Adapter into ViewPager
    viewPager.setAdapter(viewpageradapter);

    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Pass the position on tab click to ViewPager
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    };
    // Create first Tab
    tab = actionBar.newTab().setText("STORE").setTabListener(tabListener);
    actionBar.addTab(tab);

    // Create second Tab
    tab = actionBar.newTab().setText("NEWS").setTabListener(tabListener);
    actionBar.addTab(tab);

    // Create third Tab
    tab = actionBar.newTab().setText("EVENTS").setTabListener(tabListener);
    actionBar.addTab(tab);

    // Create fourth Tab
    tab = actionBar.newTab().setText("CONTACTUS")
            .setTabListener(tabListener);
    actionBar.addTab(tab);
}

这是片段类: -

public class StoreFragment extends SherlockListFragment {
ListView listview;
ArrayList<Item> items = new ArrayList<Item>();
StoreListViewAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_store, container,
            false);
    listview = (ListView) rootView.findViewById(android.R.id.list);

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
        }
    });
    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    items.add(new EntryItem(R.drawable.img1, "Stuff India", "Category:Entertainment",
            "View", "Offline","STUFF is the world's biggest gadget and technology lifestyle magazine with more than 25 editions globally. The Indian edition upholds these international values while showcasing the best gadgets and technology available in India."));
    items.add(new SectionItem("Previous Issue"));
    items.add(new EntryItem(R.drawable.img2, "Medgate Today", "Category:Sports",
            "View", "Offline","Detail Description"));
    items.add(new EntryItem(R.drawable.img3, "HWM Singapor", "Category:News",
            "View", "Offline","Detail Description"));
    items.add(new EntryItem(R.drawable.img4, "Shape Singapore", "Category:LifeStyle",
            "View", "Offline","Detail Description"));
    items.add(new EntryItem(R.drawable.img5, "The Field", "Category:Entertainment",
            "View", "Offline","Detail Description"));
    items.add(new EntryItem(R.drawable.img1, "TV Times", "Category:News",
            "View", "Offline","Detail Description"));

    adapter = new StoreListViewAdapter(getActivity(),
            R.layout.tab_row_list_entry, items);
     listview.setAdapter(adapter);
}

public void onViewButtonClick(View view){
    Intent i = new Intent(getActivity(),ViewButtonActivity.class);
    startActivity(i);
}

这是logcate错误: -

06-23 11:45:11.839: E/AndroidRuntime(3520): FATAL EXCEPTION: main
06-23 11:45:11.839: E/AndroidRuntime(3520): java.lang.RuntimeException: Unable to pause        activity {com.ebizzinfotech.magazineapp/com.ebizzinfotech.magazineapp.MainActivity}:    java.lang.NullPointerException
06-23 11:45:11.839: E/AndroidRuntime(3520):     at     android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at    android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread.access$1700(ActivityThread.java:117)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.os.Looper.loop(Looper.java:130)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at java.lang.reflect.Method.invoke(Method.java:507)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at dalvik.system.NativeStart.main(Native Method)
06-23 11:45:11.839: E/AndroidRuntime(3520): Caused by: java.lang.NullPointerException
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:126)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.Activity.performSaveInstanceState(Activity.java:1037)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1181)
06-23 11:45:11.839: E/AndroidRuntime(3520):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2340)
06-23 11:45:11.839: E/AndroidRuntime(3520):     ... 12 more

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 @shayan pourvatan帮助我。我正在发布帮助他人的解决方案。实际上它是由于ActionBarSherlock兼容性库而给出错误。解决方法是您的适配器扩展FragmentStatePagerAdapter而不是FragmentPagerAdapter。