4

I got an error with notifyDataSetChanged() on RecyclerView when I press back button. This is the code:

MainActivity.java

class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    public List<Article> articleList;
    public RecyclerView recyclerView;
    public RecyclerViewAdapter adapter;

    ArrayList<String> my_list = new ArrayList<String>();

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

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        initData();
        adapter = new RecyclerViewAdapter(articleList, MainActivity.this);
        recyclerView.setAdapter(adapter);
    }

      private void initData() {
        articleList = new ArrayList<>();
        queryArticle();
    }

      public void saveArticle(String title, String desc) {
        //ab is a instance for class Article_Bmob
        ab.setTitle(title);
        ab.setDesc(desc);

        ab.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {

                if(e == null) {
                } else {
                    e.printStackTrace();
                }

            }
        });

     }
}

WriteArticle.java (Error File)

public class WriteArticle extends AppCompatActivity {
    MainActivity mainActivity;

    private String art_title, art_desc;
    private EditText edt_title, edt_desc;

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

        edt_title = (EditText) findViewById(R.id.w_art_title);
        edt_desc = (EditText) findViewById(R.id.w_art_desc);
        Button btn_send = (Button) findViewById(R.id.send_article);

        mainActivity = new MainActivity();

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                art_title = edt_title.getText().toString();
                art_desc = edt_desc.getText().toString();

                if (art_title.isEmpty()) {
                    Snackbar.make(view, "R.String.xxx", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                } else if (art_desc.isEmpty()) {
                    Snackbar.make(view, "R.String.xxx", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                } else {
                    mainActivity.saveArticle(art_title, art_desc);
                    mainActivity.adapter.notifyDataSetChanged();
                    finish();
                }

            }
        });

    }

And the error:

java.lang.NullPointerException: Attempt to invoke virtual method 
'void com.myapplication.RecyclerViewAdapter.notifyDataSetChanged()' on a null object reference
at com.myapplication.WriteArticle$1.onClick(WriteArticle.java:52)

How can I solve this?

  • 1
    You can't simply create an Activity by calling its Constructor as you do `mainActivity = new MainActivity();` here – J j Sep 03 '16 at 15:08
  • So what should i do? – Yi Corleone Sep 03 '16 at 15:20
  • 1
    What do you want to achieve with `mainActivity = new MainActivity();` ? Do you want to open the activity? – J j Sep 03 '16 at 15:30
  • Possible duplicate of [How to manage \`startActivityForResult\` on Android?](http://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) – OneCricketeer Sep 03 '16 at 15:42
  • Basically, don't make an instance of an Activity that you don't need. You can't just pass around data like this. Alternatively, maybe check this post. http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android – OneCricketeer Sep 03 '16 at 15:43
  • Other options include having the Write activity store things in a SQLite database that you read in the other Activity to a RecyclerView – OneCricketeer Sep 03 '16 at 15:56

1 Answers1

3

In your MainActivity

public static MainActivity mactivity;
public static MainActivity getinstance(){
    return mactivity;
}

In your WriteArticle activity

MainActivity mainActivity;

And use it as

mainActivity.getinstance.saveArticle(art_title, art_desc);
mainActivity.getinstance.adapter.notifyDataSetChanged();
user6657161
  • 361
  • 1
  • 8
  • This might work, but definitely isn't recommended. The Activity could be killed by the OS, so the exception will still occur – OneCricketeer Sep 03 '16 at 15:41
  • And that'll freeze the app for half a second. Why does that help? – OneCricketeer Sep 03 '16 at 15:45
  • There is no need for it. But you said that the activity could be killed by OS. You can alternatively pack data in intent and receive it before setting adapter. – user6657161 Sep 03 '16 at 15:48
  • Wait. What is the purpose of notifyDataSetChanged when there is no data in adapter? Post your full code including saveArticle and whole MainActivity. – user6657161 Sep 03 '16 at 16:58
  • Who are you asking? The saveArticle method is attempting to put data in the adapter – OneCricketeer Sep 03 '16 at 17:12
  • Yes, the saveArticle is putting data in the adapter – Yi Corleone Sep 04 '16 at 07:54
  • Now I got a similarly error: `java.lang.NullPointerException: Attempt to invoke virtual method 'void com.myapplication.MainActivity.saveArticle(java.lang.String, java.lang.String)' on a null object reference` Did you mean that `return mactivity = new MainActivity();` ? – Yi Corleone Sep 04 '16 at 08:33