Skip to content Skip to sidebar Skip to footer

Showing Error Caused By: Java.lang.reflect.invocationtargetexception

When I press the button(named 'current location') it should shows current location(lat & long) on the textview(named 'tvAddress') .But it's not working as I expect. It's giving

Solution 1:

You are calling findViewById before setting the View, so when you call tvAddress.setText, tvAddress is null. Start with this code instead :

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.map);

    manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    tvAddress = (TextView) findViewById(R.id.tvaddress);
    btncurrent= (Button)findViewById(R.id.btncurrent);

    locationClient = newLocationClient(this, this, this);


}

Also, you got the error wrong, the real cause is this :

Caused by: java.lang.NullPointerException
    at com.mamun.tasktest.MapActivity.marker(MapActivity.java:129)

Take your time to read the stack trace carefully and look for places where it points to classes you wrote, it is always a good place to start for investigating bugs ;)

Solution 2:

Your first two lines in onCreate method should be these ones:

super.onCreate(savedInstanceState);
setContentView(R.layout.map);

Solution 3:

Just you have wrongly initialized your views. As you can only access the views after setContentView() method and you have done that totally reverse that is why its throwing error.

Just change your onCreate() as below:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     setContentView(R.layout.map);
   manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    tvAddress = (TextView) findViewById(R.id.tvaddress);
    btncurrent= (Button)findViewById(R.id.btncurrent);

Solution 4:

Not related to this question but this error also comes due to data binding errors.

1. Check if your variable is pointing to the correct data class2. Check if your fields due for your views (text view, visibility are correct)
3. You have imported the correct imports (for eg for visibility related operations)

Post a Comment for "Showing Error Caused By: Java.lang.reflect.invocationtargetexception"