无法从静态内容引用非静态方法checkWriteStoragePermission()

时间:2016-09-28 12:39:02

标签: java android-studio

我正在尝试从类中调用位于Fragment中的方法。现在我收到错误:无法从静态内容中引用非静态方法checkWriteStoragePermission()。我真的没有想法。我现在谷歌搜索大约2天没有任何结果。

我试图调用该方法的类:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static String sRecordDate;

public void onMessageReceived(RemoteMessage remoteMessage) {

    Intent intent = new Intent(this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("FCM NOTIFICATION");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());

    for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        Log.d(":", "key, " + key + " value " + value);

        sRecordDate = value;

        RecordingMode.checkWriteStoragePermission(); //HERE I AM GETIING THE ERROR
    }
}

Fragment类中的方法:

    public void checkWriteStoragePermission() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            mIsRecording = true;
            try {
                createVideoFileName();
            } catch (IOException e) {
                e.printStackTrace();
            }
            startRecord();
            mMediaRecorder.start();
            mChronometer.setBase(SystemClock.elapsedRealtime());
            mChronometer.setVisibility(View.VISIBLE);
            mChronometer.start();
        } else {
            if(shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                Toast.makeText(getActivity(), "app needs to be able to save videos", Toast.LENGTH_SHORT).show();
            }
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT);
        }
    } else {
        mIsRecording = true;
        try {
            createVideoFileName();
        } catch (IOException e) {
            e.printStackTrace();
        }
        startRecord();
        mMediaRecorder.start();
        mChronometer.setBase(SystemClock.elapsedRealtime());
        mChronometer.setVisibility(View.VISIBLE);
        mChronometer.start();
    }
}

我该如何轻松解决这个问题?因为我无法将其转换为静态方法。

亲切的问候,

2 个答案:

答案 0 :(得分:3)

但是您正在使用此方法,就好像它是静态方法(通过类的名称调用它),创建具有此方法的类的实例并在该实例上调用它。你说它是'片段'类,然后:

Fragment fragment = new Fragment();
fragment.checkWriteStoragePermission();

答案 1 :(得分:0)

您的方法checkWriteStoragePermission不是static,但您尝试直接从您的服务中调用它。 尝试编辑这样的方法:

public static void checkWriteStoragePermission() {
....
}