-2

I am trying to recreate a memo application that is on Samsung devices. this memo application is supposed to just create memos of title and body and display them in a recycler view or list. whenever I go into my MainActivity my application crashes

MainActivity.class

public class MainActivity extends AppCompatActivity
{
    ArrayList<Memo> memos;
    RecyclerView memoList;
    MemoAdapter memoAdapter;
    Button addMemo;


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

        memoList = findViewById(R.id.memoList);
        addMemo = findViewById(R.id.addMemo);
        memoAdapter = new MemoAdapter(this,memos);
        memoList.setAdapter(memoAdapter);
    }
}

Memo.class

public class Memo
{

    private String mtitle;
    private String mbody;
    public Memo(String title, String body)
    {
        mtitle = title;
        mbody = body;
    }

    public String getTitle()
    {
        return mtitle;
    }

    public void setTitle(String mtitle) {
        this.mtitle = mtitle;
    }

    public String getBody() {
        return mbody;
    }

    public void setBody(String mbody) {
        this.mbody = mbody;
    }
}

MemoAdapter.class

public class MemoAdapter extends RecyclerView.Adapter<MemoAdapter.ViewHolder>
{
    private Context context;
    private ArrayList<Memo> memo;

    public MemoAdapter(Context context, ArrayList<Memo> memo)
    {
        this.context = context;
        this.memo = memo;
    }

    @NonNull
    @Override
    public MemoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_listitem,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MemoAdapter.ViewHolder holder, int position)
    {
        holder.listitem.setText(memo.get(position).getTitle());
        /*holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                TODO: open composeactivity to edit the text of this memo
            }
        });*/
    }

    @Override
    public int getItemCount() {
        return memo.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder
    {
        private TextView listitem;
        private LinearLayout linearLayout;

        public ViewHolder(@NonNull View itemView)
        {
            super(itemView);
            listitem = itemView.findViewById(R.id.listItem);
            linearLayout = itemView.findViewById(R.id.LL);
        }
    }

Thank you for the help, let me know if I need to post anything more.

  • Please [edit] your question to provide the complete [stack trace from the crash](https://stackoverflow.com/a/23353174). – Mike M. Dec 08 '19 at 03:06

4 Answers4

0

Without stack trace it's hard to say actual problem. But according your mentioned code, you didn't initialize the memos before using it. So, probably it's a NullPointerException.

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

    memoList = findViewById(R.id.memoList);
    addMemo = findViewById(R.id.addMemo);

    //Initialize the memos with empty array list before using it.
    memos = new ArrayList<>();
    memoAdapter = new MemoAdapter(this, memos);

    // Set LayoutManager and Adapter to RecyclerView
    memoList.setLayoutManager(new LinearLayoutManager(this));
    memoList.setAdapter(memoAdapter);
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

set layout manager to recyclerview.and try to run

ArrayList<Memo> memos;
RecyclerView memoList;
MemoAdapter memoAdapter;
Button addMemo;
@Override
protected void onCreate(Bundle savedInstanceState)
{
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 memoList = findViewById(R.id.memoList);
 addMemo = findViewById(R.id.addMemo);
 llManager=new GridLayoutManager(activity,2, RecyclerView.VERTICAL, false);
 memoList.setLayoutManager(layoutManager);
 memoAdapter = new MemoAdapter(this,memos);
 memoList.setAdapter(memoAdapter);
}
0

Try putting some null check fail safe conditions for this line in the MemoAdapter Class.

holder.listitem.setText(memo.get(position).getTitle());

because if the memo ArrayList is empty and yet you are still trying to get Title means you will face error. So put an if condition and check if its empty or not.

And for the RecyclerView set a layout manager as well. Check if this works out.

If you are facing error again try pasting the error logs from logical here. We can get a clear picture of the issue from that.

Thanks and I hope this helps you out.

0

Did you initialise memos ?

ArrayList<Memo> memos; will be null, and memo.size(); will give NPE.

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126