ViewPager和Fragment中的ListView和GridView

时间:2015-08-27 16:47:32

标签: android listview gridview android-viewpager fragment

  • 我想在Fragment中使用List List和GridView
  • 我使用库支持版本4来查看分页器
  • 我有很多错误,我不明白我的代码错在哪里

公共类Gallery_fragment扩展了FragmentActivity {

public static Database                  db;
public static ArrayAdapter              adapter;
public static ArrayList<StructureImage> images = new ArrayList<StructureImage>();





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.gallery);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);

        PagerAdapter adpter2 = new PagerAdapter(this, getSupportFragmentManager());
        viewPager.setAdapter(adpter2);

        adapter = new AdapterImage(images);

        this.db = new Database(this);
        db.database();

        getImageListFromServer();

    }//end oncreate


    private void getImageListFromServer() {
        String result = Webservice.readUrl("http://192.168.1.100/file-server/service.php", null);
        if (result != null) {
            try {
                db.open();
                JSONArray tasks = new JSONArray(result);

                String[] array_1 = new String[100];
                for (int i1 = 0; i1 < db.count_gallery(); i1++) {
                    array_1[i1] = db.Display_gallery(i1, 0);

                }

                for (int i = 0; i < tasks.length(); i++) {
                    JSONObject object = tasks.getJSONObject(i);

                    String id = object.getString("id");
                    String title = object.getString("title");
                    String tozihat = object.getString("tozihat");
                    String thumbnail = object.getString("thumbnail");

                    int pars = Integer.parseInt(id);

                    Log.i("LOG", "i=" + i);
                    if (Arrays.asList(array_1).contains(id)) {
                        Log.i("LOG", "NO ! new Record");

                    } else {//new id}

                        db.inser_gallery(pars, title, tozihat, thumbnail);

                    }

                }
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
        db.close();
        fill_listview();
    }


    private void fill_listview() {
        images.clear();
        G.pagenumber.clear();
        G.imageIds.clear();
        G.imageTitles.clear();
        G.filenaming.clear();
        db.open();
        G.dbcountposition = db.count_gallery();
        for (int i = 0; i < db.count_gallery(); i++) {
            StructureImage image = new StructureImage();

            image.id = db.Display_gallery(i, 0);
            image.title = db.Display_gallery(i, 1);
            image.tozihat = db.Display_gallery(i, 2);
            image.thumbnail = db.Display_gallery(i, 3);
            // Start - add for view pager
            int imageId = G.context.getResources().getIdentifier("image" + db.Display_gallery(i, 0), "drawable", G.context.getPackageName());
            Log.i("LOG", "image id: " + imageId);
            G.pagenumber.add(i);
            G.imageIds.add(imageId);
            G.imageTitles.add("" + db.Display_gallery(i, 1));
            G.filenaming.add("" + db.Display_gallery(i, 3));
            // End
            int pp = Integer.parseInt(db.Display_gallery(i, 0));
            Log.i("LOG2", pp + "---  1");
            if (pp > 17) {
                Log.i("LOG2", "image.thumbnail" + image.thumbnail);
                DownloadManager.addToDownloadList(image.thumbnail);
            }
            images.add(image);

        }

        Collections.sort(images);
        Collections.reverse(images);

        adapter.notifyDataSetChanged();
        db.close();
    }

}

FragmentA类:

public class FragmentA extends Fragment {

    private ViewGroup                       layout;
    public static ArrayList<StructureImage> images = new ArrayList<StructureImage>();
    public ArrayAdapter                     adapter;
    public static Database                  db;
    private View                            view;


    public static Fragment instance(int catId) {
        Fragment fragment = new FragmentA();

        Bundle data = new Bundle();
        data.putInt("catId", catId);
        fragment.setArguments(data);

        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.db = new Database(getActivity());
        db.database();

        GridView list = (GridView) layout.findViewById(R.id.list);
        adapter = new AdapterImage(images);
        list.setAdapter(adapter);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        layout = (ViewGroup) inflater.inflate(R.layout.fragment_a, null);
        return layout;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

}

我的错误:

08-27 20:59:58.832: E/AndroidRuntime(9586): FATAL EXCEPTION: main
08-27 20:59:58.832: E/AndroidRuntime(9586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aradmobile.gol/com.aradmobile.gol.Gallery_fragment}: java.lang.NullPointerException
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.os.Looper.loop(Looper.java:137)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at java.lang.reflect.Method.invoke(Method.java:511)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at dalvik.system.NativeStart.main(Native Method)
08-27 20:59:58.832: E/AndroidRuntime(9586): Caused by: java.lang.NullPointerException
08-27 20:59:58.832: E/AndroidRuntime(9586):     at com.aradmobile.gol.Gallery_fragment.onCreate(Gallery_fragment.java:45)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.app.Activity.performCreate(Activity.java:5104)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-27 20:59:58.832: E/AndroidRuntime(9586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-27 20:59:58.832: E/AndroidRuntime(9586):     ... 11 more

0 个答案:

没有答案