2017-05-06 51 views
0

我想先说我是一个大新手,我只在这方面工作了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; 
     } 
    } 
} 

任何帮助,如果我这样做都是错的也是如此,如果您需要了解更多信息或者什么,我会很乐意提前多提供it.Thank你理解对不起!

+0

发布错误日志。你什么意思是跳过线? – Kaushal28

回答

2

你需要有API级别23运行权限(安卓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; 
}