从解析向GridLayout添加内容

时间:2016-01-16 06:11:45

标签: android android-gridlayout

我正在尝试将从解析中收到的图像显示给客户,我可以使用LinearLayout来完成,但是当我尝试使用GridLayout时,它会给出空指针异常。

不起作用的GridLayout代码

package com.parse.starter;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.parse.FindCallback;
import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.List;

public class UserFeed extends AppCompatActivity {

    String receivedUserName;
    LinearLayout linearLayout;
    GridLayout.LayoutParams params;

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

        // linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();
        receivedUserName = intent.getStringExtra("username");
        setTitle(receivedUserName + "'s Feed");

        ParseQuery<ParseObject> query = ParseQuery.getQuery("images");
        query.whereEqualTo("uploaderName", receivedUserName);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> imagesList, ParseException e) {
                if (e == null) {

                    if (imagesList.size() > 0)

                        for (ParseObject imageInList : imagesList) {
                            ParseFile file = (ParseFile) imageInList.get("image");
                            file.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] data, ParseException e) {

                                    if (e == null) {
                                        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                                        ImageView imageView = new ImageView(getApplicationContext());
                                        imageView.setImageBitmap(bitmap);
                                        imageView.setPadding(8, 8, 8, 8);

                                        GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);

                                        params.rowSpec = GridLayout.spec(0, 50);
                                        imageView.setLayoutParams(new GridLayout.LayoutParams(params));
                                        gridLayout.addView(imageView);
                                    }
                                }
                            });


                        }

                } else {

                }
            }
        });


    }

}

有效的线性布局代码是

package com.parse.starter;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.parse.FindCallback;
import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.List;

public class UserFeed extends AppCompatActivity {

    String receivedUserName;
    LinearLayout linearLayout;
    GridLayout.LayoutParams params;

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

        // linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();
        receivedUserName = intent.getStringExtra("username");
        setTitle(receivedUserName + "'s Feed");

        ParseQuery<ParseObject> query = ParseQuery.getQuery("images");
        query.whereEqualTo("uploaderName", receivedUserName);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> imagesList, ParseException e) {
                if (e == null) {

                    if (imagesList.size() > 0)

                        for (ParseObject imageInList : imagesList) {
                            ParseFile file = (ParseFile) imageInList.get("image");
                            file.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] data, ParseException e) {

                                    if (e == null) {
                                        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                                        ImageView imageView = new ImageView(getApplicationContext());
                                        imageView.setImageBitmap(bitmap);
                                        imageView.setPadding(8, 8, 8, 8);

                                        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
                                        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                                                ViewGroup.LayoutParams.MATCH_PARENT,
                                                ViewGroup.LayoutParams.MATCH_PARENT
                                        ));
                                        linearLayout.addView(imageView);
                                    }
                                }
                            });


                        }

                } else {

                }
            }
        });


    }

}

0 个答案:

没有答案