我刚刚开始涉猎Java,所以请暂时保留有关不良代码实践的讲座。
我最近的项目一直遇到很多问题,一开始我根本无法显示任何布局对象。经过几个小时的搜索后,我解决了该问题。
但是,现在我在实际尝试在模拟器上运行项目时遇到问题。我在其他线程上看到过一些建议,例如关闭即时运行,删除.idea和gradle文件夹,然后重建并与gradle等同步。到目前为止,这些都不对我有用。
我只想问一下我的代码是否存在任何问题(除了实践/效率方面不是很好),这会阻止我的代码运行。 (请参见下面的事件日志代码)
我感到困惑的原因是,在构建项目时我没有出现任何错误,但是我无法运行该应用程序(还尝试过构建APK文件并在我的实际手机上运行它,但这是行不通的要么。)
package total.register.registertotal;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Define all of the input variables.
final EditText Fifties = (EditText) findViewById(R.id.Fifties);
final EditText Twenties = (EditText) findViewById(R.id.Twenties);
final EditText Tens = (EditText) findViewById(R.id.Tens);
final EditText Fives = (EditText) findViewById(R.id.Fives);
final EditText Ones = (EditText) findViewById(R.id.Ones);
final EditText Halfs = (EditText) findViewById(R.id.Halfs);
final EditText Fifths = (EditText) findViewById(R.id.Fifths);
final EditText Tenths = (EditText) findViewById(R.id.Tenths);
final EditText Twentieths = (EditText) findViewById(R.id.Twentieths);
final EditText Hundredths = (EditText) findViewById(R.id.Hundredths);
final EditText Total = (EditText) findViewById(R.id.Total);
//Convert them all into strings
final String StrFifties = Fifties.getText().toString();
final String StrTwenties = Twenties.getText().toString();
final String StrTens = Tens.getText().toString();
final String StrFives = Fives.getText().toString();
final String StrOnes = Ones.getText().toString();
final String StrHalfs = Halfs.getText().toString();
final String StrFifths = Fifths.getText().toString();
final String StrTenths = Tenths.getText().toString();
final String StrTwentieths = Twentieths.getText().toString();
final String StrHundredths = Hundredths.getText().toString();
//Convert them all into numbers to be used in addition.
final int IntFifties = Integer.parseInt(StrFifties);
final int IntTwenties = Integer.parseInt(StrTwenties);
final int IntTens = Integer.parseInt(StrTens);
final int IntFives = Integer.parseInt(StrFives);
final int IntOnes = Integer.parseInt(StrOnes);
final int IntHalfs = Integer.parseInt(StrHalfs);
final int IntFifths = Integer.parseInt(StrFifths);
final int IntTenths = Integer.parseInt(StrTenths);
final int IntTwentieths = Integer.parseInt(StrTwentieths);
final int IntHundredths = Integer.parseInt(StrHundredths);
//Create constants
final int Fifty = 50;
final int Twenty = 20;
final int Ten = 10;
final int Five = 5;
final int One = 1;
final Button SummariseRegister = (Button) findViewById(R.id.Calculate);
SummariseRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
double TotFifties = 0;
if(!StrFifties.matches("")){
TotFifties = IntFifties * Fifty;
}
int TotTwenties = 0;
if(!StrTwenties.matches("")){
TotTwenties = IntTwenties * Twenty;
}
int TotTens = 0;
if(!StrTens.matches("")){
TotTens = IntTens * Ten;
}
int TotFives = 0;
if(!StrFives.matches("")){
TotFives = IntFives * Five;
}
int TotOnes = 0;
if(!StrOnes.matches("")){
TotOnes = IntOnes * One;
}
double TotHalfs = 0;
if(!StrHalfs.matches("")){
TotHalfs = IntHalfs * (0.5);
}
double TotTenths = 0;
if(!StrTenths.matches("")){
TotTenths = IntTenths * (0.1);
}
double TotTwentieths = 0;
if(!StrTwentieths.matches("")){
TotTwentieths = IntTwentieths * (0.2);
}
double TotHundredths = 0;
if(!StrHundredths.matches("")){
TotHundredths = IntHundredths * (0.01);
}
//double Total = TotFifties + TotTwenties + TotTens + TotFives + TotOnes + TotHalfs + TotTenths + TotHundredths + TotTwentieths;
double Total = TotFifties;
}catch(Exception e){
Total.setText("Error calculating result.");
}
}
});
}
}
事件日志:
15/10/2018 19:24 IDE和插件更新:Android Studio准备好了 更新。
19:24 Gradle同步开始
19:24项目设置开始
19:24 Gradle同步在1s 313ms(从缓存状态)中完成
19:31 *守护程序未运行;从tcp:5037开始
19:31 *守护程序成功启动
19:31执行任务:[:app:assembleDebug]
19:31模拟器:进程结束,退出代码为-1073741819 (0xC0000005)
19:31 Gradle构建完成于9s 867ms
19:31会话“ app”:安装APK错误
如果你们中的任何一个人如此仁慈,我将不胜感激地回答。
谢谢。