Antonio Gschossmann
Antonio Gschossmann

Reputation: 612

Illegal type 'void' in Android Java Class

I have the error Illegal type 'void' in an Android Java Application.

The error comes on the private void createFolder() and public void onRequestPermissionsResult code block.

I dont know if there is missing some imports or there is an error with the public class Berechtigungen extends AppCompatActivity code-line.

If anyone knows an answer to this, please write it, thanks.

My code:

package barsoftware.suedtirolpointer;

import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.Manifest;
import android.content.pm.PackageManager;


public class Berechtigungen extends AppCompatActivity {

    final int REQ_CODE_EXTERNAL_STORAGE_PERMISSION = 45;

    public void GPS() {
        if(ActivityCompat.checkSelfPermission(Berechtigungen.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
            // Anweisung
        } else {
            ActivityCompat.requestPermissions(Berechtigungen.this,new  String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
        }
    }
};


private void createFolder(){
    File ordner = new File(Environment.getExternalStorageDirectory(), "TestOrdner");
    ordner.mkdirs();
    Toast.makeText(getApplicationContext(), "Ordner erstellt", Toast.LENGTH_SHORT).show();
}

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

    if(requestCode == REQ_CODE_EXTERNAL_STORAGE_PERMISSION && grantResults.length >0 &&grantResults[0] == PackageManager.PERMISSION_GRANTED){
        createFolder();
    }
}
}

Upvotes: 1

Views: 8357

Answers (1)

fdelafuente
fdelafuente

Reputation: 1122

It's a syntax error, it should be like this:

package barsoftware.suedtirolpointer;

import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.Manifest;
import android.content.pm.PackageManager;

public class Berechtigungen extends AppCompatActivity {

    final int REQ_CODE_EXTERNAL_STORAGE_PERMISSION = 45;

    public void GPS() {

        if (ActivityCompat.checkSelfPermission(Berechtigungen.this, 
        Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            //

        } else {

            ActivityCompat.requestPermissions(Berechtigungen.this, 
            new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 
            REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
        }
    }

    private void createFolder() {

        File ordner = new File(Environment.getExternalStorageDirectory(), "TestOrdner");
        ordner.mkdirs();
        Toast.makeText(getApplicationContext(), "Ordner erstellt", Toast.LENGTH_SHORT).show();
    }

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

        if (requestCode == REQ_CODE_EXTERNAL_STORAGE_PERMISSION 
        && grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            createFolder();
        }
    }
}

Upvotes: 3

Related Questions