尝试调用虚方法'void android.widget.Button.setOnClickListener

时间:2016-01-15 22:20:56

标签: android

enter image description here当我打开Android应用程序时,它会崩溃。这是代码:

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);

            }
        });

    }


}

这是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

我第一次运行应用程序时运行正常。

似乎问题是requestButton。我已经尝试进一步寻找解决方案,但找不到一个。

Main XML file and other XML file

2 个答案:

答案 0 :(得分:0)

找不到R.id.buttonMap,导致findViewById(R.id.buttonMap)为null。

id buttonMap应该在您的activity_main xml文件中。事实并非如此。

答案 1 :(得分:0)

从logcat中可以看出按钮requestButton为空。似乎你的java代码是正确的,问题出在你的布局中。您的xml文件中的标识requestButton并不完全相同。将您的xml与java代码匹配