Showing error Caused by: java.lang.reflect.InvocationTargetException

前端 未结 3 1995
后悔当初
后悔当初 2021-02-15 11:55

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\

相关标签:
3条回答
  • 2021-02-15 12:33

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

    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    
    0 讨论(0)
  • 2021-02-15 12:35

    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:

     @Override
    protected void onCreate(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);
    
    0 讨论(0)
  • 2021-02-15 12:47

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

    @Override
    protected void onCreate(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 = new LocationClient(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 ;)

    0 讨论(0)
提交回复
热议问题