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();
}
}
}
Hopefully you can shine some light on this for me.
I’m trying to create the following AsyncTask in Java for an Android app. When I enter the below code, Android Studio highlights both the ‘void‘ params with the error:
«Illegal Type: Void».
After a number of google searches I still can’t understand what the problem is here (and in honesty, I’m quite confused about the whole thing!).
private class BackgroundTask extends AsyncTask<URL, void, void> {
protected void doInBackground(URL... inputURL) {
URL searchURL = inputURL[0];
//Step 2: Get Data
String JSONdata = getJSONdata(searchURL);
//Step 3: Parse JSON
ArrayList books = parseJSON(JSONdata);
updateUI(books);
}
}
Appreciate any info you can send my way!
OneCricketeer
176k18 gold badges130 silver badges239 bronze badges
asked Jul 31, 2016 at 19:18
3
use the class instead, capital V
private class BackgroundTask extends AsyncTask<URL, Void, Void> {
answered Jul 31, 2016 at 19:22
ΦXocę 웃 Пepeúpa ツΦXocę 웃 Пepeúpa ツ
47.2k17 gold badges69 silver badges95 bronze badges
AsyncTask uses generics. So use Void instead of void. Similarly, if you want to use integer use Integer and not int.
answered Jul 31, 2016 at 19:21
EmbydextrousEmbydextrous
1,5911 gold badge11 silver badges20 bronze badges
Dobriy_Bobr 1 / 1 / 0 Регистрация: 16.09.2017 Сообщений: 53 |
||||
1 |
||||
13.10.2017, 11:06. Показов 3131. Ответов 3 Метки нет (Все метки)
Вот код
Придирается к void
0 |
Kukstyler 1236 / 851 / 262 Регистрация: 02.04.2009 Сообщений: 3,204 |
||||
13.10.2017, 11:58 |
2 |
|||
Dobriy_Bobr, запятую после
Добавлено через 56 секунд Не по теме: Что за IDE у Вас, что на запятую не ругается?
0 |
xoraxax |
13.10.2017, 15:08
|
Не по теме:
Dobriy_Bobr, запятую после end уберите а ниче, что методы не в классе?
0 |
1236 / 851 / 262 Регистрация: 02.04.2009 Сообщений: 3,204 |
|
13.10.2017, 20:01 |
4 |
а ниче, что методы не в классе? Точняк. На это даже не смотрел
0 |
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();
}
}
}
Hopefully you can shine some light on this for me.
I’m trying to create the following AsyncTask in Java for an Android app. When I enter the below code, Android Studio highlights both the ‘void‘ params with the error:
«Illegal Type: Void».
After a number of google searches I still can’t understand what the problem is here (and in honesty, I’m quite confused about the whole thing!).
private class BackgroundTask extends AsyncTask<URL, void, void> {
protected void doInBackground(URL... inputURL) {
URL searchURL = inputURL[0];
//Step 2: Get Data
String JSONdata = getJSONdata(searchURL);
//Step 3: Parse JSON
ArrayList books = parseJSON(JSONdata);
updateUI(books);
}
}
Appreciate any info you can send my way!
OneCricketeer
175k18 gold badges130 silver badges239 bronze badges
asked Jul 31, 2016 at 19:18
3
use the class instead, capital V
private class BackgroundTask extends AsyncTask<URL, Void, Void> {
answered Jul 31, 2016 at 19:22
ΦXocę 웃 Пepeúpa ツΦXocę 웃 Пepeúpa ツ
47.2k17 gold badges69 silver badges95 bronze badges
AsyncTask uses generics. So use Void instead of void. Similarly, if you want to use integer use Integer and not int.
answered Jul 31, 2016 at 19:21
EmbydextrousEmbydextrous
1,5911 gold badge11 silver badges20 bronze badges
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();
}
}
}
Name: dkC59003 Date: 03/09/98
The section «8.3 Field Declarations» of JLS says:
«The variables of a class type are introduced by field declarations:
FieldDeclaration:
FieldModifiers(opt) Type VariableDeclarators ;»
The section «8.4.1 Formal Parameters» of JLS says:
«Each parameter specifier consists of a type and an identifier (optionally followed by brackets) that specifies
the name of the parameter:
…
FormalParameter:
Type VariableDeclaratorId»
Two tests below use void[] in the place of Type in FieldDeclaration and
FormalParameter.
JDK 1.1.1 through 1.1.6 compilers do not detect any errors
whereas JDK 1.2beta3K compiler correctly reports compile-time error
in each case:
——————————————————-
public class clss02109 {
void[] x; // compile-time error
}
——————————————
clss02109.java:3: Instance variables can’t be void: x
void[] x; // compile-time error
^
1 error
——————————————————-
public class clss02707 {
Object m( void[] ob ) { // compile time error
return ob ;
}
}
——————————————
clss02707.java:3: Argument can’t have type void: ob
Object m( void[] ob ) { // compile time error
^
1 error
——————————————————-
======================================================================
- What Is the
void type not allowed here
Error? - Fix Void Type Not Allowed Here Error in Java — Don’t Print in
main()
Method - Fix Void Type Not Allowed Here Error in Java — Return a String Instead of Printing in
printMessage1()
We use many functions when creating big programs in Java, and sometimes errors might appear. One of the errors that the compiler can throw is the void type not allowed here
error discussed in this article.
What Is the void type not allowed here
Error?
We create a function in Java by writing the access modifier, a return type, a function name with parentheses, and the function body is enclosed with curly braces. We can return several types of data from a function, but when we don’t want to return any, we use the keyword void
to tell the compiler that we don’t want to return anything from the method.
In the program below, we have a class JavaExample
that contains two methods, first is the main()
function, and the second is the printMessage1()
that has a print statement System.out.println()
that prints a message that printMessage1()
receives as a parameter.
The function printMessage1()
doesn’t return anything and just prints a message; we use void
type as the return type. We use another print statement but in the main()
method and call the printMessage1()
function inside it with String 1
as an argument.
When we run the code, the output throws an error, void type not allowed here
. It happens because printMessage1()
already has a print statement that prints the value
, and it doesn’t return anything when we call the function in a print statement; there’s nothing to print in the main
method.
public class JavaExample {
public static void main(String[] args) {
System.out.println(printMessage1("String 1"));
}
static void printMessage1(String value) {
System.out.println(value);
}
}
Output:
java: 'void' type not allowed here
Fix Void Type Not Allowed Here Error in Java — Don’t Print in main()
Method
The first solution to this error is that we do not call the function printMessage1()
in a print statement because there is already a System.out.println()
statement in the method itself, and it does not return anything.
In this code, we write the printMessage1()
function’s body as a println()
statement. We call the printMessage1()
method in main()
with a string as an argument.
public class JavaExample {
public static void main(String[] args) {
printMessage1("String 1");
}
static void printMessage1(String value) {
System.out.println(value);
}
}
Output:
Fix Void Type Not Allowed Here Error in Java — Return a String Instead of Printing in printMessage1()
The second solution is to specify a return type in the function, return a value, and print it wherever we call the function.
We write the method printMessage1()
but with a return type String
. Inside the method’s body, we use the return
keyword with the value
we want to return when called. In the main()
method, we call the function printMessage1()
into a print statement, but there will be no error as the method returns a value.
public class JavaExample {
public static void main(String[] args) {
System.out.println(printMessage1("How are you doing today?"));
System.out.println(printMessage1("String 2"));
}
static String printMessage1(String value) {
return value;
}
}
Output:
How are you doing today?
String 2
Go to BukkitCoding
How to fix «Illegal type: ‘void'»?
The IDE I use keeps saying that void is illegal, any idea how to fix this? is it a syntax error, or what?
Here’s my code:
package me.voxelwashere.mobghosts; public void onEntityDeath(EntityDeathEvent event) if(event.getEntity().getType().name().toLowerCase().contains("Vex")) return; { Entity e = event.getEntity(); if(e.getLastDamageCause() instanceof EntityDamageByEntityEvent) { EntityDamageByEntityEvent nEvent = (EntityDamageByEntityEvent) e.getLastDamageCause(); if(nEvent.getDamager() instanceof Player) { Player p=(Player)damager; Location loc=p.getLocation(); p.getWorld().spawnEntity(loc,EntityType.VEX); p.sendMessage("You shouldn't have done that..."); } } }