我试图从类中调用位于片段中的方法。现在我收到错误:无法从静态内容引用非静态方法 checkWriteStoragePermission()。我实在是没主意了。我用谷歌搜索了大约两天,没有任何结果。
我尝试调用该方法的类:
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();
}
}
如何轻松解决此问题?因为我无法将其转换为静态方法。
亲切的问候,
最佳答案
但是您使用此方法就像它是静态方法一样(通过类名称调用它),创建具有此方法的类的实例并在该实例上调用它。你说它是“Fragment”类,那么:
Fragment fragment = new Fragment();
fragment.checkWriteStoragePermission();
关于java - 无法从静态内容引用非静态方法 checkWriteStoragePermission(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39747812/