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;
}
}
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?