这是日志:
2019-12-30 17:44:20.213 15799-15799/com.funnyappsentertaiment.darellsoundboard E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.funnyappsentertaiment.darellsoundboard, PID: 15799
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.content.ContextWrapper.checkPermission(ContextWrapper.java:738)
at androidx.core.content.ContextCompat.checkSelfPermission(ContextCompat.java:535)
at com.funnyappsentertaiment.darellsoundboard.SoundboardActivity.requestPermissions(SoundboardActivity.java:129)
at com.funnyappsentertaiment.darellsoundboard.SoundboardRecyclerAdapter$3.onClick(SoundboardRecyclerAdapter.java:75)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
这是我在MainActivity.java中定义并在其他类中使用的方法:
// Handles all permission events
public boolean requestPermissions(){
// Check if the users Android version is equal to or higher than Android 6 (Marshmallow)
// Since Android 6 you have to request permissions at runtime to provide a better security
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
// Check if the permission to write and read the users external storage is not granted
// You need this permission if you want to share sounds via WhatsApp or the like
if (ContextCompat.checkSelfPermission(SoundboardActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
// You can log this little text if you want to see if this method works in your Android Monitor
//Log.i(LOG_TAG, "Permission not granted");
// If the permission is not granted request it
ActivityCompat.requestPermissions(SoundboardActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
// Check if the permission to write the users settings is not granted
// You need this permission to set a sound as ringtone or the like
if(!Settings.System.canWrite(SoundboardActivity.this)){
// Displays a little bar on the bottom of the activity with an OK button that will open a so called permission management screen
Snackbar.make(mLayout, "Se necesitan los permisos para poder guardar los sonidos", Snackbar.LENGTH_INDEFINITE).setAction("OK",
new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}).show();
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && Settings.System.canWrite(this)){
return true;
}
else{
return false;
}
}return true;
}
这是类,以及我如何在另一类中调用方法:
public class SoundboardRecyclerAdapter extends RecyclerView.Adapter<SoundboardRecyclerAdapter.SoundboardViewHolder>{
// Declare an ArrayList that will contain all SoundObjects
private ArrayList<SoundObject> soundObjects;
// Demand all needed informations for the RecyclerView
// ArrayList<SoundObject> : Main content provider
public SoundboardRecyclerAdapter(ArrayList<SoundObject> soundObjects){
// Hand over all data to the private ArrayList
this.soundObjects = soundObjects;
}
// Initialises each RecyclerView item
@Override
public SoundboardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Set the default design for a element in the RecyclerView that is based on sound_item.xml
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.sound_item, null);
// Returns a new ViewHolder for each RecyclerView item
return new SoundboardViewHolder(itemView);
}
@Override
public void onBindViewHolder(SoundboardViewHolder holder, int position) {
// Get a SoundObject from the ArrayList that also contains
// Simplifies the set processes
final SoundObject object = soundObjects.get(position);
**final SoundboardActivity save = new SoundboardActivity();*************************************
// Define an id for a simplified committing process
final Integer soundID = object.getItemID();
// Set the name of each sound button that is represented by the (SoundObject)object
holder.itemTextView.setText(object.getItemName());
// Handle actions when the user simply clicks on a sound button
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Calls a method that plays the sound
EventHandlerClass.startMediaPlayer(v, soundID);
}
});
// Handle actions when the user presses a sound button
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Calls a method that creates a PopupMenu where the user can choose between several actions
**save.requestPermissions();***************************************************
EventHandlerClass.popupManager(v, object);
return true;
}
});
holder.itemImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Calls a method that creates a PopupMenu where the user can choose between several actions
**save.requestPermissions();****************************************************
EventHandlerClass.popupManager(v, object);
}
});
}
// Tells the RecyclerView how many items are accessible to be displayed
// Should return the size of the given content provider (here: SoundObject ArrayList)
@Override
public int getItemCount() {
return soundObjects.size();
}
// Gets all accessible areas that are declared by you
public class SoundboardViewHolder extends RecyclerView.ViewHolder{
// TextView to display the name of a sound button
TextView itemTextView;
ImageView itemImage;
public SoundboardViewHolder(View itemView) {
super(itemView);
// Assign itemTextView to the TextView item declared in sound_item.xml
itemTextView = (TextView) itemView.findViewById(R.id.textViewItem);
itemImage = (ImageView) itemView.findViewById(R.id.imageOptions);
}
}
}
上面的代码中的'*****************************************'是指向我在哪里使用方法
当我按下按钮并激活'setOnClickListener'然后调用我的方法requestPermissions()
时,应用程序崩溃我希望一整天都在努力解决这个问题。对不起,我的英语不好。