I have three files: MainActivity.java, MapsActivity.java and MainActivity.xml. I would like to change this TextView object in the MainActivity.xml:
<TextView
android:id="@+id/destination_latlng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:layout_marginBottom="40dp"
android:textColor="@color/black"
android:text="Destination Lat, Lng" />
I would like to change it from within the MapsActivity.java file, but whenever I try to do this by assigning a variable to the "destination_latlng" TextView and using setText() the app crashes.
public class Map extends AppCompatActivity implements OnMapReadyCallback {
// Destination coords
TextView destination_latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_map);
// destination coords
destination_latLng = (TextView) findViewById(R.id.destination_latlng);
destination_latLng.setText("testing...");
How do I change the TextView from the MapsActivity.java file?
var textView = findViewById<TextView>(R.id.destination_latlng)
and then doingtextView.text = "Whatever you want it to be"
should work fine and can be done in theonCreate()
method of your activity's class.desntination_coords = (TextView) findViewById(R.id.destination_latlng)
and then trieddestination_coords.setText("testing...")
within the OnCreate but the app crashes on startup of the second MapsActivity.java.setText()
method on a null object. Seemingly you are fetching the textview component incorrectly as it is null. As I mentioned, show your work in your post. Show all the code you have in your main activity.