我想首先说我是一个大新手,我只在这里工作了大约4天,其中包括阅读java参考书的许多页面。
这个应用程序的目的是从文本文件中获取信息,然后用它做很少的数学运算然后返回一个值。我已经在运行android 5.1.1的计算机上运行模拟器,但是当我在运行6.0.1的笔记4上运行它时,它只是在调试模式下跳过代码行,我不明白为什么。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView output;
EditText food;
EditText block;
EditText ounce;
Button calculate_blocks;
Button calculate_ounces;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//finding elements
output = (TextView) findViewById(R.id.output);
food = (EditText) findViewById(R.id.food);
block = (EditText) findViewById(R.id.blocks);
ounce = (EditText) findViewById(R.id.ounces);
calculate_ounces = (Button) findViewById(R.id.calculate_ounces);
calculate_blocks = (Button) findViewById(R.id.calculate_blocks);
//setting listeners
calculate_blocks.setOnClickListener(this);
calculate_ounces.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final String FILENAME = android.os.Environment.getExternalStorageDirectory().getPath() + "/Foods.txt";
BufferedReader fin = null;
FileReader fr = null;
String currentLine="zeutn", line=null ;
String strfood = food.getText().toString();
double gramsOfNutrientPerOunce=0;
double ounces=0,blocks=0;
try {
ounces = Double.parseDouble(ounce.getText().toString());
} catch(NumberFormatException nfe){
ounces=0;
}
try {
blocks = Double.parseDouble(block.getText().toString());
} catch(NumberFormatException nfe){
blocks=0;
}
//Find refrence value after food in file
try {
File file1 = new File(FILENAME);
file1.createNewFile();
fr = new FileReader(FILENAME);
fin = new BufferedReader(fr);
while ((currentLine = fin.readLine()) != null) {
if ((currentLine.toUpperCase()).equals(strfood.toUpperCase())) {
line = fin.readLine();
fr.close();
fin.close();
} else
output.setText("Add food to file");
}
} catch (IOException e) {
output.setText("Fail");
}
try{
gramsOfNutrientPerOunce=Double.parseDouble(line);
} catch(NumberFormatException nfe){
output.setText("Add food to file");
}
String out;
double tmp;
switch (v.getId()) {
case R.id.calculate_blocks:
tmp = ounces/gramsOfNutrientPerOunce;
out=Double.toString(tmp);
output.setText(out);
break;
case R.id.calculate_ounces:
tmp = blocks*gramsOfNutrientPerOunce;
out=Double.toString(tmp);
output.setText(out);
break;
}
}
}
任何帮助都表示抱歉,如果我这样做也是错误的,那么如果您需要更多信息或任何我愿意提供的信息。请提前感谢您!
答案 0 :(得分:2)
您需要拥有Api等级23(Android 6.0)及更高版本的运行时权限。你可以这样做很简单。
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
然后用户提供的响应可以在回调方法中处理它,就像这样。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
注意:编码样本取自https://developer.android.com
我注意到的另一件事是你有setOnClickListeners到你的按钮,但在你的onClick方法中你没有处理它,所以单击时按钮将执行完全相同的事情。您只需通过切换view.getID或在您的情况下使用v.getID来处理它们,代码就是这样的。
switch (v.getId()){
case R.id.(id_of_firstButton):
//do what you have to do for first button
break;
case R.id.(id_of_secondButton):
//do what you have to do for second button
break;
default:
//do something in the default case
break;
}
答案 1 :(得分:1)