I am trying to pass the value of matrix_id from MainActivity(extends Activity) to HomeActivity(extends Activity) and ScheduleFragment(extends Fragment).
When I run the apps, it will crash.
Im really amateur in Java Programming.
What should I do ?
Please help me, thank you.
This is part of MainActivity.java
public class MainActivity extends Activity {
public static final String PREFS_NAME="PreferencesValue";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
b = (Button)findViewById(R.id.btnLogin);
matrix_id = (EditText)findViewById(R.id.txtMatric);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("matrix", matrix_id.getText().toString());
editor.commit();
Intent i = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(i);
finish();
}
});
}
This is part of HomeActivity.java
/**
* Displaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0: fragment = new ScheduleFragment();
break;
case 1: fragment = new ReminderFragment();
break;
case 2: fragment = new AboutFragment();
break;
case 3: fragment = new LogoutFragment();
break;
}
This is ScheduleFragment.java
public class ScheduleFragment extends Fragment {
private final Context context;
public static final String PREFS_NAME="PreferencesValue";
public ScheduleFragment(Context context){
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_schedule, container, false);
TextView tvmatrix = (TextView)rootView.findViewById(R.id.textViewMatrix);
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
tvmatrix.setText(settings.getString("matrix", "A324142"));
return rootView;
}
}