-3

settings.java

public class settingsActivity extends MainActivity  {


    private TextView mTextMessage;
    Intent i=new Intent();


    Button saveButton;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            goalDate = findViewById(R.id.goaldinput);
            goalWeight = findViewById(R.id.goalwinput);
            nameIn = findViewById(R.id.nameinput);
            fT=findViewById(R.id.feet);
            iNches=findViewById(R.id.inches);
            mAle=findViewById(R.id.maleButton);
            feMale=findViewById(R.id.othersButton);
            oThers=findViewById(R.id.femaleButton);
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Intent intent1 = new Intent(settingsActivity.this, MainActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.navigation_history:
                    mTextMessage.setText(R.string.title_history);
                    Intent intent2 = new Intent(settingsActivity.this, historyActivity.class);
                    startActivity(intent2);

                    break;
                case R.id.navigation_progress:
                    mTextMessage.setText(R.string.title_progress);
                    Intent intent3 = new Intent(settingsActivity.this, progressActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.navigation_settings:
                    mTextMessage.setText(R.string.title_settings);
                    Intent intent4 = new Intent(settingsActivity.this, settingsActivity.class);
                    startActivity(intent4);
                    break;
            }
            return false;
        }
    };

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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        saveButton=findViewById(R.id.saveButton);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText goalD = ((EditText)findViewById(R.id.goaldinput));
                EditText goalW = ((EditText)findViewById(R.id.goalwinput));
                EditText currentW = ((EditText)findViewById(R.id.currentWinput));
                i = new Intent(settingsActivity.this,historyActivity.class);
                gD=goalD.getText().toString();
                gW=goalW.getText().toString();
                cW=currentW.getText().toString();
                i.putExtra("goalDate",gD);
                i.putExtra("goalWeight",gW);
                i.putExtra("currentWeight",cW);
                startActivity(i);

/*
                arrayList1.add(goalD.toString());
                arrayList2.add(goalW.toString());
                arrayList3.add(currentW.toString());
*/


//                adapter1.notifyDataSetChanged();
              //  adapter2.notifyDataSetChanged();
               // adapter3.notifyDataSetChanged();

            }
        });


    }


history.java

   public class historyActivity extends MainActivity {
    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {



            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Intent intent1 = new Intent(historyActivity.this, MainActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.navigation_history:
                    mTextMessage.setText(R.string.title_history);
                    Intent intent2 = new Intent(historyActivity.this, historyActivity.class);
                    startActivity(intent2);
                    break;
                case R.id.navigation_progress:
                    mTextMessage.setText(R.string.title_progress);
                    Intent intent3 = new Intent(historyActivity.this, progressActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.navigation_settings:
                    mTextMessage.setText(R.string.title_settings);
                    Intent intent4 = new Intent(historyActivity.this, settingsActivity.class);
                    startActivity(intent4);
                    break;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        listView1= findViewById(R.id.goaldlistView);
        listView2= findViewById(R.id.goalwlistView);
        listView3= findViewById(R.id.currentwlistView);

        EditText goalD = ((EditText)findViewById(R.id.goaldinput));
        EditText goalW = ((EditText)findViewById(R.id.goalwinput));
        EditText currentW = ((EditText)findViewById(R.id.currentWinput));

       gDate = getIntent().getExtras().getString("goalDate");
        gWeight = getIntent().getExtras().getString("goalWeight");
        cWeight = getIntent().getExtras().getString("currentWeight");

        String addArray1[] = {"3/14/1992"};
        String addArray2[] = {"152"};
        String addArray3[] = {"160"};

        arrayList1=new ArrayList<String>(Arrays.asList(addArray1));
        arrayList2=new ArrayList<String>(Arrays.asList(addArray2));
        arrayList3=new ArrayList<String>(Arrays.asList(addArray3));
        arrayList1.add(gDate);
        arrayList2.add(gWeight);
        arrayList3.add(cWeight);
        adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList1);
        adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList2);
        adapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList3);

        listView1.setAdapter(adapter1);
        listView2.setAdapter(adapter2);
        listView3.setAdapter(adapter3);



    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    EditText goalDate ;
    EditText goalWeight ;
    EditText nameIn ;
    EditText fT;
    EditText iNches;
    EditText currentWeight;
    RadioButton mAle;
    RadioButton feMale;
    RadioButton oThers;

    public ArrayList<String>arrayList1=new ArrayList<String>();
    public ArrayList<String>arrayList2=new ArrayList<String>();
    public ArrayList<String>arrayList3=new ArrayList<String>();

    public ArrayAdapter<String> adapter1;
    public ArrayAdapter<String> adapter2;
    public ArrayAdapter<String> adapter3;

    Intent intent =new Intent();

    public ListView listView1;
    public ListView listView2;
    public ListView listView3;


    String gD=" ";
    String gW=" ";
    String cW=" ";
    String gDate=" ";
    String gWeight=" ";
    String cWeight=" ";

    ;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Intent intent1 = new Intent(MainActivity.this, MainActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.navigation_history:
                    mTextMessage.setText(R.string.title_history);
                    Intent intent2 = new Intent(MainActivity.this, historyActivity.class);
                    startActivity(intent2);


                    break;
                case R.id.navigation_progress:
                    mTextMessage.setText(R.string.title_progress);
                    Intent intent3 = new Intent(MainActivity.this, progressActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.navigation_settings:
                    mTextMessage.setText(R.string.title_settings);
                    Intent intent4 = new Intent(MainActivity.this, settingsActivity.class);
                    startActivity(intent4);
                    break;
            }
            return false;
        }
    };

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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }

I'm getting the following error when passing data from settings to history:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

gDate is where it is erroring out for nullException error for bundle and I think it might go for gWeight and cWeight

I have tried initializing the intent in MainActivity, Setting and History files. Still it is not passing data.

Need to pass the inputs from Settings and display in listView in History

1 Answers1

0

You don't need to write

Intent intent = new Intent(); in you History class.

Instead of this write.

Intent intent = getIntent();

gDate = intent.getExtras().getString("goalDate");
gWeight = intent.getExtras().getString("goalWeight");
cWeight = intent.getExtras().getString("currentWeight");

--UPDATE--

Remove Intent i = new Intent(); from Settings activity parent.

And in onCreate() do like this.

saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent i = new Intent(settingsActivity.this,historyActivity.class);
            String gD = goalD.getText().toString();
            String gW = goalW.getText().toString();
            String cW = currentW.getText().toString();
            i.putExtra("goalDate",gD);
            i.putExtra("goalWeight",gW);
            i.putExtra("currentWeight",cW);
            startActivity(i);
        }
    });

And in History activity:

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

    Intent intent = getIntent();
    String gDate = intent.getExtras().getString("goalDate");
    String gWeight = intent.getExtras().getString("goalWeight");
    String cWeight = intent.getExtras().getString("currentWeight");

    // OR

    Intent intent = getIntent();
    String gDate = intent.getStringExtra("goalDate");
    String gWeight = intent.getStringExtra("goalWeight");
    String cWeight = intent.getStringExtra("currentWeight");

}

I recommend you to add check too.

if(intent.getExtras() != null) {
   //Your code here.
}

--UPDATE 2---

You said in your comment "but when I hit the history button in the navigation toolbar, the program quits on the nullPointerException". It is normal, because when you click the history button in the navigation toolbar, after that works this code in settings fragment.

case R.id.navigation_history:
     mTextMessage.setText(R.string.title_history);
     Intent intent2 = new Intent(settingsActivity.this, historyActivity.class);
     startActivity(intent2);

     break;

You see you're not calling any putExtra() here, so when it will open history it will through nullPointerException, because you call getExtra() in history class, but as you don't add any extra, getExtra() will return null. So the only way to prevent this is to add putExtra() in that case or to check before getting extra.

Intent intent = getIntent();

if(intent.getExtras() != null) {  //Also there is hasExtra() check too, but I'd like to check like this.
  gDate = intent.getExtras().getString("goalDate");
  gWeight = intent.getExtras().getString("goalWeight");
  cWeight = intent.getExtras().getString("currentWeight");
}
Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61
  • I tried. Still same error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at com.example.fftest.historyActivity.onCreate(historyActivity.java:74) – user11390420 Apr 21 '19 at 15:32
  • tried using bundle but comes with same error in the gDate portion as above. – user11390420 Apr 21 '19 at 15:34
  • Are you debug your app? Where it gives null pointer exception? getExtras is null? – Hayk Mkrtchyan Apr 21 '19 at 15:35
  • I have added the full coding for Main.java, history.java and settings.java – user11390420 Apr 21 '19 at 15:45
  • First of all I don't see `putExtra()` in you Settings activity. – Hayk Mkrtchyan Apr 21 '19 at 15:48
  • My bad. can you refresh the page again? It should show – user11390420 Apr 21 '19 at 15:50
  • Why you call `Intent i = new Intent();` in you Settings activity and onCreate() you call `i = new Intent(activity, class)` too? You can remove the intent you wrote in parent. And yes if you're not using intent in MainActivity, remove it from MainAcitivty too. – Hayk Mkrtchyan Apr 21 '19 at 15:53
  • Intent i is used to set strings for gD, gW and cW. That is why I call Intent I. But should I put it inside onCreate or outside? – user11390420 Apr 21 '19 at 15:58
  • I edited answer. If you done everything right, it has to work :) – Hayk Mkrtchyan Apr 21 '19 at 16:06
  • I did follow what you did and put it in. It still errors out in gDate for nullExceptionError. Not sure where I m getting wrong. – user11390420 Apr 21 '19 at 16:07
  • Try to add straight values in putExtra. Example `i.putExtra("goalDate", "Text");` Try is it work? – Hayk Mkrtchyan Apr 21 '19 at 16:10
  • Ok. I got the saveButton to pass the data after initializing the string inside the onCreate in the history.java. After I click on save button, it displays the inputs and displays but when I hit the history button in the navigation toolbar, the program quits on the nullPointerException on gD – user11390420 Apr 21 '19 at 16:47
  • Can I connect to your computer or see your full code. I think the problem occurs from the navigation view, where you initialize intent on every navigation item click. – Hayk Mkrtchyan Apr 21 '19 at 17:16
  • I think now my answer can help you solve your problem. – Hayk Mkrtchyan Apr 21 '19 at 17:59
  • It works. Finally. Thanks Hayk. You are awesome bro. – user11390420 Apr 21 '19 at 18:26
  • Oh that's cool :) Thank you too. Please accept my answer and give vote too :))) Good luck. – Hayk Mkrtchyan Apr 21 '19 at 19:22