使用ViewPager崩溃设置壁纸

时间:2014-01-03 05:29:16

标签: android android-viewpager

每当按下壁纸按钮时,我的应用程序都会崩溃。这是我的代码。请帮忙。我给了必要的壁纸许可。我不知道如何正确使用壁纸管理器但有一些方法我可以完成这个

    public class ScreenSlideActivity extends FragmentActivity {

private static final int NUM_PAGES = 5;


private ViewPager mPager;
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen_slide);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {

            invalidateOptionsMenu();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.activity_screen_slide, menu);

    menu.findItem(R.id.action_previous).setEnabled(mPager.getCurrentItem() > 0);


    MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE,
            (mPager.getCurrentItem() == mPagerAdapter.getCount() - 1)
                    ? R.string.action_finish
                    : R.string.action_next);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:

            NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
            return true;

        case R.id.action_previous:

            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
            return true;

        case R.id.action_next:

            mPager.setCurrentItem(mPager.getCurrentItem() + 1);
            return true;
        case R.id.setasbg:
            WallpaperManager mwp = WallpaperManager.getInstance(getApplicationContext());            
           try {
            mwp.setResource(mPager.getCurrentItem());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           return true;

    }

    return super.onOptionsItemSelected(item);
}


private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return ScreenSlidePageFragment.create(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}
    }

这是我的另一个班级

    public class ScreenSlidePageFragment extends Fragment {
    public static final String ARG_PAGE = "page";

private int mPageNumber;


public static ScreenSlidePageFragment create(int pageNumber) {
    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    return fragment;
}

public ScreenSlidePageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.fragment_screen_slide_page, container, false);
    switch(mPageNumber){
    case 1:

    ((TextView) rootView.findViewById(R.id.text1)).setBackgroundResource(R.drawable.nfs);
    break;
    case 2:
 ((TextView) rootView.findViewById(R.id.text1)).setBackgroundResource(R.drawable.nfs2);

 break;
    case 3:
 ((TextView) rootView.findViewById(R.id.text1)).setBackgroundResource(R.drawable.nfs3);
        break;
    case 4:
 ((TextView) rootView.findViewById(R.id.text1)).setBackgroundResource(R.drawable.nfs4);
 break;
    }
    return rootView; 
    }    


public int getPageNumber() {
    return mPageNumber;
}
    }

Log Cat:

01-03 11:36:16.389: I/PGA(6939): New SOCKET connection: com.chirag.nfs1 (pid 6939, tid 6939)
01-03 11:36:22.219: D/dalvikvm(6939): GC_FOR_ALLOC freed 114K, 27% free 6573K/8899K, paused 0ms
01-03 11:36:22.249: I/dalvikvm-heap(6939): Grow heap (frag case) to 16.475MB for 8294412-byte allocation
01-03 11:36:22.269: D/dalvikvm(6939): GC_CONCURRENT freed 5K, 14% free 14668K/17027K, paused 0ms+0ms
01-03 11:36:22.289: D/dalvikvm(6939): GC_FOR_ALLOC freed 0K, 14% free 14668K/17027K, paused 0ms
01-03 11:36:22.289: I/dalvikvm-heap(6939): Grow heap (frag case) to 19.986MB for 3686412-byte allocation
01-03 11:36:22.309: D/dalvikvm(6939): GC_CONCURRENT freed 0K, 12% free 18268K/20675K, paused 0ms+0ms
01-03 11:36:25.709: D/dalvikvm(6939): GC_FOR_ALLOC freed 8112K, 51% free 10212K/20675K, paused 0ms
01-03 11:36:25.709: I/dalvikvm-heap(6939): Grow heap (frag case) to 20.029MB for 8294412-byte allocation
01-03 11:36:25.729: D/dalvikvm(6939): GC_CONCURRENT freed 2K, 12% free 18310K/20675K, paused 0ms+0ms
01-03 11:36:25.749: D/dalvikvm(6939): GC_FOR_ALLOC freed 0K, 12% free 18311K/20675K, paused 0ms
01-03 11:36:25.749: I/dalvikvm-heap(6939): Grow heap (frag case) to 23.543MB for 3686412-byte allocation
01-03 11:36:25.769: D/dalvikvm(6939): GC_CONCURRENT freed 0K, 10% free 21911K/24323K, paused 0ms+0ms
01-03 11:36:27.679: W/ResourceType(6939): No package identifier when getting name for resource number 0x00000001
01-03 11:36:27.679: D/AndroidRuntime(6939): Shutting down VM
01-03 11:36:27.679: W/dalvikvm(6939): threadid=1: thread exiting with uncaught exception (group=0xb2c5e180)
01-03 11:36:27.679: D/AndroidRuntime(6939): procName from cmdline: com.chirag.nfs1
01-03 11:36:27.679: E/AndroidRuntime(6939): in writeCrashedAppName, pkgName :com.chirag.nfs1
01-03 11:36:27.689: I/Process(6939): Sending signal. PID: 6939 SIG: 9
01-03 11:36:27.689: D/AndroidRuntime(6939): file written successfully with content: com.chirag.nfs1 StringBuffer : ;com.chirag.nfs1

0 个答案:

没有答案