2017-10-15 1 views
-1

in dieser App Ich versuche, eine Schaltfläche in ein Fragment verwenden, um Länge und Breite in einem Fragment mit GPS eines Telefons zu erhalten und dann möglicherweise diese Informationen in einem anderen Fragment übergeben.App Absturz nach Anforderung der Erlaubnis von innerhalb eines Fragments

import android.Manifest; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.content.ContextCompat; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Toast; 


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class GalleryFragment extends Fragment { 
    FragmentActivity mContext = getActivity(); 
    double latitude = 0;//21.6001; 
    double longitude = 0;//39.136; 




    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     // Inflate the layout for this fragment 
     final View v = inflater.inflate(R.layout.fragment_gallery, container, false); 


     Button gpsBtn = (Button) v.findViewById(R.id.gpsBtn); 
     gpsBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //VVVVVVVVV// HERE IS WHERE THE CRASH HAPPENS... 
       if (ContextCompat.checkSelfPermission(mContext,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
         && ActivityCompat.checkSelfPermission(mContext,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        ActivityCompat.requestPermissions(mContext,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 

       } else { 
        Toast.makeText(mContext, "You need have granted permission", Toast.LENGTH_SHORT).show(); 
        GPSTracker gps = new GPSTracker(mContext, GalleryFragment.this); 

        // Check if GPS enabled 

        if (gps.canGetLocation()) { 

         latitude = gps.getLatitude(); 
         longitude = gps.getLongitude(); 

         // \n is for new line 

         Toast.makeText(getActivity().getApplicationContext(), 
           "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
        } else { 

         // Can't get location. 

         // GPS or network is not enabled. 

         // Ask user to enable GPS/network in settings. 

         gps.showSettingsAlert(); 
        } 
       } 
       //setMyPrayerList(latitude,longitude,prayerNamez,prayerTimez); 

      } 
     }); 
     return v; 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     switch (requestCode) { 
      case 1: { 
       // If request is cancelled, the result arrays are empty. 

       if (grantResults.length > 0 

         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        // permission was granted, yay! Do the 
        // contacts-related task you need to do. 
        GPSTracker gps = new GPSTracker(mContext, GalleryFragment.this); 

        // Check if GPS enabled 

        if (gps.canGetLocation()) { 

         double latitude = gps.getLatitude(); 
         double longitude = gps.getLongitude(); 

         // \n is for new line 

         Toast.makeText(getActivity().getApplicationContext(), 
           "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
        } else { 
         // Can't get location. 

         // GPS or network is not enabled. 

         // Ask user to enable GPS/network in settings. 

         gps.showSettingsAlert(); 
        } 

       } else { 

        // permission denied, boo! Disable the 

        // functionality that depends on this permission. 
        Toast.makeText(mContext, "You need to grant permission", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } 
    } 



} 

Ich bin neu in Fragmente und es scheint mir, dass sie so anders als Aktivitäten in vielerlei Hinsicht sein kann, wie ich gerade entdeckt haben. Die App funktioniert normal, bis ich auf die Schaltfläche klicke und auf sie klicke, wo sie abstürzt.

unten angehängt ist das Crash-Protokoll.

danke für Ihre Zeit und Hilfe.

10-15 05:52:27.500 26041-26041/com.example.majidalashari.sanad E/AndroidRuntime: FATAL EXCEPTION: main   
                        Process: com.example.majidalashari.sanad, PID: 26041   
                        java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String,   int, int)' on a null object reference 
                         at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:432) 
                         at com.example.majidalashari.sanad.GalleryFragment$1.onClick(GalleryFragment.java:43) 
                         at android.view.View.performClick(View.java:5669) 
                         at android.view.View$PerformClick.run(View.java:22546) 
                         at android.os.Handler.handleCallback(Handler.java:751) 
                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                         at android.os.Looper.loop(Looper.java:154) 
                         at android.app.ActivityThread.main(ActivityThread.java:6334) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
+0

Do Sie haben '' in Ihrer AnroidManifest.xml-Datei? – Thracian

+0

im Manifest ich habe: @FatihOzcan –

+1

Mögliches Duplikat von [Was ist eine NullPointerException und wie behebe ich sie?] (https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-fix-it) –

Antwort

0

How to check permission in fragment

dies mein Krachen Problem in einer Antwort von @ThetNaingMizo erstellt verknüpfen gelöst

er ein ziemlich klares verdummte Beispiel gab, wie die Berechtigung zu überprüfen, in Aktivität Vs Fragmente

Verwandte Themen