0

enter image description hereWhen I open my Android application it crashes. Here is the code:

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private Button requestButton;
    private TextView coordinateText;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private Button buttonMap;
    private Button buttonCurrentLocation;


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

        //reference requestButton
        requestButton = (Button) findViewById(R.id.requestButton);
        //reference coordText
        coordinateText = (TextView) findViewById(R.id.coordinateText);

        // Creating button object for buttonMap
         buttonMap = (Button) findViewById(R.id.buttonMap);

        //Create a button object for buttonCurrentLocation
         buttonCurrentLocation = (Button) findViewById(R.id.buttonCurrentLocation);

        //Action listener for buttonMap
        buttonMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(getApplicationContext(), MapsActivity.class);
                startActivity(intent1);
            }

        });

        // Action listener for buttonCurrentLocation
        buttonCurrentLocation.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(), GPSShow.class);
                startActivity(i);

                }

        });


        //Initialize locationManager
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        //Initialize locationListener
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                coordinateText.append("\n" + location.getLatitude() + " " + location.getLongitude());

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent intent2 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent2);

            }
        };
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET

                }, 10);
            }
            return;
        } else {
            configureButton();
        }


    }

    //OUTSIDE ONCREATE METHOD


    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {

        switch (requestCode) {
            case 10:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    configureButton();
                return;
        }

    }

    private void configureButton() {
        requestButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);

            }
        });

    }


}

Here is the logcat:

01-15 17:10:18.088 2340-2340/com.example.matt.palt E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.matt.palt, PID: 2340
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matt.palt/com.example.matt.palt.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                         at com.example.matt.palt.MainActivity.configureButton(MainActivity.java:124)
                                                                         at com.example.matt.palt.MainActivity.onCreate(MainActivity.java:103)
                                                                         at android.app.Activity.performCreate(Activity.java:6237)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
01-15 17:10:20.435 2340-2340/com.example.matt.palt I/Process: Sending signal. PID: 2340 SIG: 9

The first time I ran the application it was running fine.

It seems that the problem is the requestButton. I have tried looking further for a solution but can't find one.

Main XML file and other XML file

2 Answers2

0

R.id.buttonMap is not found, resulting in null from findViewById(R.id.buttonMap).

The id buttonMap should be in your activity_main xml file. It is not.

0

From logcat its clear that the button requestButton is null. Seems your java code is right and the problem is in your layout. The id requestButton is not exactly the same in your xml file. Do match your xml with java code

S.Aslpour
  • 86
  • 1
  • 1
  • 10
Irfan Raza
  • 2,859
  • 1
  • 22
  • 29