我为android应用程序制作了一个java方法,该应用程序返回应用程序应该在外部目录中工作的目录。
我在处理返回时遇到了问题,我不知道如何修复“丢失返回语句”的错误。
public String getpath() {
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
String extdir = Environment.getExternalStorageDirectory().getAbsolutePath();
File path = new File(extdir, "App");
if(path.exists()) {
return path.getAbsolutePath();
}
else {
checkandmakepath();
getpath();
}
}
else {
Toast.makeText(this, "Could not access External Directory!", Toast.LENGTH_LONG).show();
Intent to_main = new Intent(this, MainActivity.class);
startActivity(to_main);
MainActivity.this.finish();
}
}发布于 2013-07-25 04:05:08
public String getpath()这意味着您的方法将返回一个String对象,这个对象只在您的if语句中执行。将其放置在方法的末尾,以确保无论经过何种条件,始终返回某些内容。(由于您已经返回到if分支,因此不需要在所有其他部分的末尾执行此操作。)
return null;如果您不想在返回路径之前退出该方法,您可以为它执行一个while循环,但是这可能会导致它是无限的,所以最好用调用方法的任何方法来处理。
发布于 2013-07-25 04:04:49
您的问题的一个可能的解决方案(还有更多)返回有评论的内容:
public String getpath() {
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
String extdir = Environment.getExternalStorageDirectory().getAbsolutePath();
File path = new File(extdir, "App");
if(path.exists()) {
return path.getAbsolutePath();
}
else {
checkandmakepath();
return getpath(); // Return the recursive call's value
}
}
else {
Toast.makeText(this, "Could not access External Directory!", Toast.LENGTH_LONG).show();
Intent to_main = new Intent(this, MainActivity.class);
startActivity(to_main);
MainActivity.this.finish();
// RETURN SOMETHING HERE (return ""; or the like)
return null;
}
}发布于 2013-07-25 04:06:43
在两个return中添加else blocks语句
https://stackoverflow.com/questions/17848782
复制相似问题