简体   繁体   中英

Android Studio: Getters for editText

I want to use the value of 2 editTexts from one activity in another. Here is my code so far. I am getting:

java.lang.NullPointerException.

The Code:

public class AddJob extends AppCompatActivity{
    // vars
    private BottomNavigationView bottomNavigationView;
    private EditText editTextLat, editTextLng;

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

        TextView textView = findViewById(R.id.activityTitleAddJob);
        textView.setText("Add a Job");

        editTextLat = findViewById(R.id.editTextLat);
        editTextLng = findViewById(R.id.editTextLng);
    }

    public int getLatitude() {
        return new Integer(editTextLat.getText().toString());
    }

    public int getLongitude() {
        return new Integer(editTextLng.getText().toString());
    }
}

The Stack Trace:

在此处输入图片说明

Here is the code snippet from the map class:

AddJob aj = new AddJob();
int lat = aj.getLatitude();
int lng = aj.getLongitude();
Toast.makeText(aj, lat + " " + lng, Toast.LENGTH_LONG).show();

Please read about the Activity lifecycle . You should never create an Activity directly with

 new MyActivity() 

This will not launch any of the lifecycle events ( onCreate , etc...) or bind it to a context, set a view hierarchy on it, or do any of the regular Activity things you may be expecting. Your program is returning null because onCreate is never called on the activity, and if you were to simply try to call it yourself it would likely crash.

If you want data from one activity to be available in another activity, an easy way to achieve this is to save the data in SharedPreferences in the AddJob activity (whenever the values are updated) and access it in MapActivity from the SharedPreferences . You can also pass the data from one Activity to the next by adding data to the Intent when you launch it.

One advantage to using SharedPreferences here is that the user's choices will be saved from one app session to the next, and if you have multiple things that can launch the MapActivity they don't have to keep passing that data to it.

hello would it not be better using an Intent

Intent i = new Intent(MainActivity.this, NEXTActivity.class);
            i.putExtra("latitude", lat);
            i.putExtra("longitude", lng);
            startActivity(i);

NEXTActivity

 Bundle extras = getIntent().getExtras();
    double latitude = extras.getDouble("latitude");
    double longitude = extras.getDouble("longitude");

You shouldn't create activity objects, you can start activity with Intent and pass data through it. Check out the relevant answer in the link below:

https://stackoverflow.com/a/2091482/10116426

It seems that you are instantiating the AddJob class. This may raise problems on the back-stack of android, which may create lifecycle management problems. So, as mentioned by @NoobAndroid, it is better to use the recommended way not to run into unexpected errors.

use this code .

package com.example.cloudanalogy.introscreen;

import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private EditText editTextLat, editTextLng;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                TextView textView = findViewById(R.id.activityTitleAddJob);
                textView.setText("Add a Job");
                editTextLat = findViewById(R.id.editTextLat);
                editTextLng = findViewById(R.id.editTextLng);
        }

            public int getLatitude() {
                return Integer.parseInt(editTextLat.getText().toString());
            }

            public int getLongitude() {
                return Integer.parseInt(editTextLng.getText().toString());
            }

    public void onclick(View view) {

        int lat = getLatitude();
        int lng = getLongitude();
        Toast.makeText(this, ""+lat+" and "+lng, Toast.LENGTH_SHORT).show();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM