猿问

我的代码运行不出来了是为什么?,也没有说有错误

  1. package com.example.demo2;


  2. import java.util.ArrayList;

  3. import java.util.HashMap;

  4. import java.util.List;

  5. import java.util.Map;


  6. import android.content.Intent;

  7. import android.os.Bundle;

  8. import android.support.v4.app.Fragment;

  9. import android.view.LayoutInflater;

  10. import android.view.View;

  11. import android.view.View.OnClickListener;

  12. import android.view.ViewGroup;

  13. import android.webkit.WebView.FindListener;

  14. import android.widget.AdapterView;

  15. import android.widget.AdapterView.OnItemClickListener;

  16. import android.widget.ListView;

  17. import android.widget.SimpleAdapter;


  18. public class Fragment1 extends Fragment{


  19. private ListView mListview;

  20. private SimpleAdapter mSAdapter;

  21. private int[] image;

  22. private String[] news_title;

  23. private String[] news_time;

  24. private String[] news_introduce;

  25. private List<Map<String, Object>> mList;


  26. @Override

  27. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  28. // TODO Auto-generated method stub

  29. View view = inflater.inflate(R.layout.view1, container, false);

  30. return view;

  31. }

  32. @Override

  33. public void onActivityCreated(Bundle savedInstanceState) {

  34. // TODO Auto-generated method stub

  35. super.onActivityCreated(savedInstanceState);

  36. mListview =(ListView) getActivity().findViewById(R.id.listview1);

  37. setData();

  38. mListview.setOnItemClickListener(new OnItemClickListener() {


  39. @Override

  40. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

  41. Intent intent = new Intent(getActivity(), DetailsActivity.class);

  42. startActivity(intent);

  43. }

  44. });

  45. }


  46. private void setData() {

  47. mList= new ArrayList<Map<String,Object>>();

  48. image = new int[]{R.drawable.r1,R.drawable.r2,R.drawable.r3,R.drawable.r4};

  49. news_title= new String[]{"标题1","标题2","标题3","标题4"};

  50. news_time= new String[]{"2016-12-21","2016-12-21","2016-12-21","2016-12-21"};

  51. news_introduce= new String[]{"内容1","内容2","内容","内容4"};

  52. mSAdapter = new SimpleAdapter(getActivity(), getdata(), R.layout.item,

  53. new String[] { "pic", "title", "time", "introduce" },

  54. new int[] { R.id.news_image, R.id.news_title, R.id.news_time, R.id.news_introduce });

  55. mListview.setAdapter(mSAdapter);


  56. }


  57. private List<? extends Map<String, Object>> getdata() {

  58. for(int i=0;i<image.length;i++){

  59. Map<String, Object> map = new HashMap<String, Object>();

  60. map.put("pic", image[i]);

  61. map.put("title", news_title[i]);

  62. map.put("time", news_time[i]);

  63. map.put("introduce", news_introduce[i]);

  64. mList.add(map);

  65. }

  66. return mList;

  67. }



  68. }



  1. package com.example.demo2;


  2. import android.app.Activity;

  3. import android.os.Bundle;




  4. public class DetailsActivity extends Activity{


  5. @Override

  6. protected void onCreate(Bundle savedInstanceState) {

  7. // TODO Auto-generated method stub

  8. super.onCreate(savedInstanceState);

  9. setContentView(R.layout.details_layout);

  10. }

  11. }



  1. package com.example.demo2;




  2. import java.util.ArrayList;

  3. import java.util.List;


  4. import android.support.v4.app.Fragment;

  5. import android.support.v4.app.FragmentActivity;

  6. import android.support.v4.view.PagerTabStrip;

  7. import android.support.v4.view.ViewPager;

  8. import android.support.v7.app.ActionBarActivity;

  9. import android.graphics.Color;

  10. import android.os.Bundle;

  11. import android.view.Menu;

  12. import android.view.MenuItem;


  13. public class MainActivity extends FragmentActivity {


  14. private List<Fragment> mList;

  15. private PagerTabStrip mTab;

  16. private ViewPager mViewPager;

  17. private List<String> mTitleList;

  18. @Override

  19. protected void onCreate(Bundle savedInstanceState) {

  20. super.onCreate(savedInstanceState);

  21. setContentView(R.layout.activity_main);

  22. mViewPager =(ViewPager) findViewById(R.id.parger);

  23. add();

  24. }


  25. private void add() {

  26. mList = new ArrayList<Fragment>();

  27. mList.add(new Fragment1());

  28. mList.add(new Fragment2());

  29. mList.add(new Fragment3());

  30. mList.add(new Fragment4());

  31. mTitleList = new ArrayList<String>();

  32. mTitleList.add("热点");

  33. mTitleList.add("推荐");

  34. mTitleList.add("社会");

  35. mTitleList.add("军事");

  36. mTab=(PagerTabStrip)findViewById(R.id.tab);

  37. mTab.setBackgroundColor(Color.GRAY);

  38. mTab.setTextColor(Color.WHITE);

  39. mTab.setDrawFullUnderline(false);

  40. mTab.setTabIndicatorColor(Color.RED);

  41. MyFragmentPagerAdapter adapter =new MyFragmentPagerAdapter(getSupportFragmentManager(), mList, mTitleList);

  42. mViewPager.setAdapter(adapter);

  43. }

  44. }











  1. import java.util.List;


  2. import android.support.v4.app.Fragment;

  3. import android.support.v4.app.FragmentManager;

  4. import android.support.v4.app.FragmentPagerAdapter;




  5. public class MyFragmentPagerAdapter extends FragmentPagerAdapter{


  6. private List<Fragment> mList;

  7. private List<String> mTitleList;

  8. public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> mList, List<String> mTitleList) {

  9. super(fm);

  10. this.mList=mList;

  11. this.mTitleList=mTitleList;

  12. }


  13. @Override

  14. public Fragment getItem(int arg0) {

  15. // TODO Auto-generated method stub

  16. return mList.get(arg0);

  17. }


  18. @Override

  19. public int getCount() {

  20. // TODO Auto-generated method stub

  21. return mList.size();

  22. }

  23. @Override

  24. public CharSequence getPageTitle(int position) {

  25. // TODO Auto-generated method stub

  26. return mTitleList.get(position);

  27. }

  28. }


  1.  


  2. }



  1. package com.example.demo2;


  2. import android.os.Bundle;

  3. import android.support.v4.app.Fragment;

  4. import android.view.LayoutInflater;

  5. import android.view.View;

  6. import android.view.ViewGroup;


  7. public class Fragment2 extends Fragment{


  8. @Override

  9. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  10. // TODO Auto-generated method stub

  11. return inflater.inflate(R.layout.view2, container, false);

  12. }

  13. }



  1. package com.example.demo2;


  2. import android.os.Bundle;

  3. import android.support.v4.app.Fragment;

  4. import android.view.LayoutInflater;

  5. import android.view.View;

  6. import android.view.ViewGroup;


  7. public class Fragment3 extends Fragment{


  8. @Override

  9. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  10. // TODO Auto-generated method stub

  11. return inflater.inflate(R.layout.view3, container, false);

  12. }

  13. }




  1. import android.os.Bundle;

  2. import android.support.v4.app.Fragment;

  3. import android.view.LayoutInflater;

  4. import android.view.View;

  5. import android.view.ViewGroup;


  6. public class Fragment4 extends Fragment{


  7. @Override

  8. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  9. // TODO Auto-generated method stub

  10. return inflater.inflate(R.layout.view4, container, false);

  11. }

  12. }



慕粉1462455617
浏览 1190回答 1
1回答

我是小hang家

Adapter没有复写getView方法???
随时随地看视频慕课网APP

相关分类

Android
我要回答