In an app I want to send a variable String
using it Intent
to another activity
, from the method onOptionsItemSelected
, my problem is that the variable is generated in a method PreferenceFragment
, no matter how many declarations I try to make, it always gives me an error, because PreferenceFragment
the variable mconfigenableecox
is not valid (it is paints red), can someone tell me what my mistake is and how to correct it.
Here is my code:
public class UsbSettingActivity extends AppCompatPreferenceActivity {
public String mconfigenableecox = "";
.
.
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_setup_com);
setHasOptionsMenu( true );
.
.
TwoStatePreference SPconfigenableeco2 = (TwoStatePreference) findPreference("config_enable_eco");
Boolean mconfigenableecoX = SPconfigenableeco2.isChecked();
String mconfigenableeco = String.valueOf(mconfigenableecoX);
mconfigenableecox = mconfigenableeco;
.
.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("EcoSet", mconfigenableecox);
startActivityForResult(intent, 0);
}
return super.onOptionsItemSelected( item );
}
The problem is that you are calling the variable from a static class,
therefore you must define the variable as static