-1

I am getting this error message when I run my android program and was wondering if anyone knew how to fix it...

java.lang.IllegalStateException: Could not find method StartRec(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;


        public class MainActivity extends AppCompatActivity {
            LocationManager locationManager;


            public void StartRec(View view, int[] grantResults) {



                    // Find the root of the external storage.
                    // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal

                    File root = android.os.Environment.getExternalStorageDirectory();


                    // See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

                    File dir = new File(root.getAbsolutePath() + "/download");
                    dir.mkdirs();
                    File file = new File(dir, "myData.txt");

                    try {
                        FileOutputStream f = new FileOutputStream(file);

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }




                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
                        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);


                }


            }


            LocationListener locationListener = new LocationListener() {
                File root = android.os.Environment.getExternalStorageDirectory();


                // See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

                File dir = new File(root.getAbsolutePath() + "/download");
                File file = new File(dir, "myData.txt");


            @Override
            public void onLocationChanged(Location location) {
                ArrayList<Location> list = new ArrayList<>();
                list.add(location);
                GPX.writePath(file,"hello",list);

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }

        };


    public void stopRec(View view) {
        Intent intent = new Intent(this, Statistics.class);
        startActivity(intent);
    }


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


}

1 Answers1

0

You have defined your method as:

public void StartRec(View view, int[] grantResults)

However, it seems as if you also are trying to use this method as a callback to a view click.

When called like this however, the method signature should only have a single argument, the View, ex:

public void StartRec(View view)

When the user clicks the button, only the view is known, the UI does not know how to populate the integer array that your method expects, find a different way of getting this data into the method.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123