title: How to access a variable value from a method to another method?
body: <p>I need access <code>String items</code> inside <code>onOptionsItemSelected</code> I declared the <code>String items</code> globally inside the class and I can access the variable inside <code>onOptionsItemSelected</code> but I get null as a value. My problem is inside</p>
  <pre><code> case R.id.share_note:
   Share.shareNote(this, items);
  </code></pre>
  <p>When I share the text I get null. I tried and searched on SO a lot but in vain because in my case it is a little different.</p>
  <pre><code>public class AddChecklists extends AppCompatActivity {
  
  Toolbar toolbar;
  Button btnAddItem;
  public LinearLayout linearLayout;
  DbHelper dbHelper;
  String items;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_add_checklist);
  
      Toast.makeText(this, &quot;problem&quot;, Toast.LENGTH_SHORT).show();
  
      toolbar = findViewById(R.id.tool_bar);
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
      btnAddItem = findViewById(R.id.btn_add_item);
      linearLayout = findViewById(R.id.linear_layout);
  
      dbHelper = new DbHelper(this);
  
      // this handler auto clicks btnAddItem button after the provided time;
      new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              btnAddItem.performClick();
          }
      },100);
  
      btnAddItem.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              addView();
          }
      });
  }
  
  public  void addView(){
  
      View checklistView = getLayoutInflater().inflate(R.layout.checklist_view, null, false);
      EditText etChecklistItem = checklistView.findViewById(R.id.et_checklist_item);
      CheckBox checkBox = checklistView.findViewById(R.id.check_box);
  
  
      checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              if(b)
                  etChecklistItem.setText(strikeThrough(etChecklistItem.getText().toString()));
                  //  state = true;
              else
                  etChecklistItem.setText(etChecklistItem.getText().toString());
          }
      });
  
      // This method makes the Action Button add new checkbox instead of press the BtnAdd every time but
      // the editText must have imeOptions=&quot;actionDone in XML file
      etChecklistItem.setOnEditorActionListener(new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
              if (i == EditorInfo.IME_ACTION_DONE) {
                  btnAddItem.performClick();
                  return true;
              }
              else
                  return false;
          }
      });
  
      etChecklistItem.requestFocus();
      linearLayout.addView(checklistView);
  
      // this handler makes the keyboard to popup on the Edittext and also
      // the editText must requestFocus();
  
      new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.showSoftInput(etChecklistItem, InputMethodManager.SHOW_IMPLICIT);
          }
      },200);
  
      ImageView imgDelete = checklistView.findViewById(R.id.img_delete);
  
      imgDelete.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              removeView(checklistView);
          }
      });
  
  
  }
  
  public void removeView(View view){
      linearLayout.removeView(view);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater menuInflater = getMenuInflater();
      menuInflater.inflate(R.menu.save_delete_share, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  
      switch (item.getItemId()) {
          case android.R.id.home:
              saveChecklist();
              return true;
  
          case R.id.btn_save:
  
              saveChecklist();
              break;
  
          case R.id.btn_delete:
  
              //not yet implemented
              Toast.makeText(this, &quot;delete button clicked&quot;, Toast.LENGTH_SHORT).show();
              break;
  
          case R.id.share_note:
              Share.shareNote(this, items);
  
      }
      return true;
  }
  
  public void saveChecklist(){
  
      Intent intent = new Intent();
  
      ArrayList&lt;String&gt; itemList = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; statusList = new ArrayList&lt;&gt;();
      String status;
  
      for (int i = 0; i &lt; linearLayout.getChildCount(); i++) {
          View v = linearLayout.getChildAt(i);
          EditText etChecklistItem = v.findViewById(R.id.et_checklist_item);
          CheckBox checkBox = v.findViewById(R.id.check_box);
  
          if (checkBox.isChecked())
              status = &quot;1&quot;;
  
          else
              status = &quot;0&quot;;
  
          if(etChecklistItem.getText().toString().isEmpty())
              etChecklistItem.setText(null);
          itemList.add(etChecklistItem.getText().toString());
          statusList.add(status);
  
  
      }
  
      StringBuilder stringBuilderItem = new StringBuilder();
      for(String singleItem: itemList) {
          stringBuilderItem.append(singleItem);
          stringBuilderItem.append(&quot;\n&quot;);
      }
  
      items =  stringBuilderItem.toString();
  
  
      StringBuilder stringBuilderStatus = new StringBuilder();
      for(String singleStatus: statusList){
          stringBuilderStatus.append(singleStatus);
          stringBuilderStatus.append(&quot;\n&quot;);
      }
  
      String statuses = stringBuilderStatus.toString();
  
      dbHelper.insertChecklist(statuses, items, DateTime.date(), DateTime.time(), System.currentTimeMillis());
  
      setResult(RESULT_OK,intent);
      Toast.makeText(this, &quot;Checklist saved...&quot;, Toast.LENGTH_SHORT).show();
  
      //  new Handler(Looper.getMainLooper()).postDelayed(() -&gt; finish(), 500);
      finish();
      overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
  }
  
  @Override
  public void onBackPressed() {
  
      super.onBackPressed();
      saveChecklist();
      overridePendingTransition(0, 0);
  }
  
  public SpannableString strikeThrough(String txt){
  
      SpannableString spannableString = new SpannableString(txt);
      StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
      spannableString.setSpan(strikethroughSpan,0, txt.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      return spannableString;
  
    }
  }
  </code></pre>