0

I am new to android developing. I have a main activity which contains a button . If a user clicks it, then it should start or open another activity . I tried to create a simple TextView in the another activity just to make sure the button works and it did work and showed me my simple TextView. BUT when I created a GridView that I filled its data using an ArrayList and an adapter, every time I click on the button the app crashes and just close.

Here is what's inside my Button :

 btnSummary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getBaseContext(), Grid.class);
                startActivity(intent);
            }
        });

And here is what's inside my Grid class:

public class Grid extends AppCompatActivity {

    GridView gv;
    intentAdapter adapter;
    EditText plusPost, plusArticle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);

        getIntent();
        gv = findViewById(R.id.grid_lv);
        plusPost = findViewById(R.id.plus_et_post);
        plusArticle = findViewById(R.id.plus_et_article);
        ArrayList<Content> info = new ArrayList<>();
        Content data = new Content(plusArticle.getText().toString(), plusPost.getText().toString());
        info.add(data);

        adapter = new intentAdapter(Grid.this, R.layout.summary_custom_layout, info);
        gv.setAdapter(adapter);

    }
}

And finally here is my adapter code :


public class intentAdapter extends BaseAdapter {

    private Context context;
    private int resource;
    private ArrayList<Content> contents;

    public intentAdapter(Context context, int resource, ArrayList<Content> content) {
        this.context = context;
        this.resource = resource;
        this.contents = content;
    }

    @Override
    public int getCount() {
        return contents.size();
    }

    @Override
    public Content getItem(int position) {
        return contents.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if(v==null) {
            v = LayoutInflater.from(context).inflate(resource, null, false);
        }

        TextView article = v.findViewById(R.id.article_tv);
        TextView post_tv = v.findViewById(R.id.post_tv);
        Content data = contents.get(position);

        article.setText(data.getArticle());
        post_tv.setText(data.getContent());

        return v;



    }
}

Here is the crash message

Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference.

Is the problem because I inflated an edit text from another activiy?

NaFa
  • 15
  • 4

2 Answers2

0

Hey if you are using recyclerview in xml layout use RecyclerView.Adapter instead of base adapter it would be helpful. see more here: https://developer.android.com/guide/topics/ui/layout/recyclerview#java

  • Yeah you are right . But I'm just new to the whole adapter stuff. I'm still learning. Thanks for the advice anyway :) – NaFa May 15 '21 at 04:53
  • yeah man if you are learning it's the best time to learn new things that's why I suggested to go with recycler view adapter. – Ranjit Yadav May 15 '21 at 05:40
0

Try sending the data with the intent in the click listner to the grid Activity and from there send it to the adapter by overiding its constructer (adding more arguments in your case) This is the general idea and there are more advanced w ways to make activities share data but stick to this first to learn why you need them later.

USER249
  • 1,080
  • 7
  • 14