我是Android应用程序开发的新手,我一直试图解决这个问题很长一段时间但找不到解决方案。我创建了一个简单的应用程序,它接受5个变量中的数据,然后计算这些变量的总和。编译代码时没有错误,但在模拟器上运行应用程序时会出错:
找不到来源错误
<disconnected>DalvikVM[localhost:8633]
DalvikVM[localhost:8610]
Thread [<1> main] (Suspended (exception RuntimeException))
<VM does not provide monitor information>
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2180
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2230
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 141
ActivityThread$H.handleMessage(Message) line: 1234
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 5041
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 793
ZygoteInit.main(String[]) line: 560
NativeStart.main(String[]) line: not available [native method]
Thread [<10> Binder_2] (Running)
Thread [<9> Binder_1] (Running)
package com.example.totalmarks;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
public class MainActivity extends Activity
{
//TextWatcher
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
checkFieldsForEmptyValues();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
EditText regno;
EditText cat1;
EditText cat2;
EditText quiz1;
EditText quiz2;
EditText quiz3;
TextView tt;
Button calculate;
float a=0;
float b=0;
float c=0;
float d=0;
float e=0;
float f=0;
String reg;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
regno=(EditText)findViewById(R.id.regno);
cat1=(EditText)findViewById(R.id.cat1);
cat2=(EditText)findViewById(R.id.cat2);
quiz1=(EditText)findViewById(R.id.quiz1);
quiz2=(EditText)findViewById(R.id.quiz2);
quiz3=(EditText)findViewById(R.id.quiz3);
tt=(TextView)findViewById(R.id.tt);
//set listeners
cat1.addTextChangedListener(textWatcher);
cat2.addTextChangedListener(textWatcher);
quiz1.addTextChangedListener(textWatcher);
quiz2.addTextChangedListener(textWatcher);
quiz3.addTextChangedListener(textWatcher);
regno.addTextChangedListener(textWatcher);
// run once to disable if empty
checkFieldsForEmptyValues();
calculate=(Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
calculate();}});
}
private void calculate()
{
a=Float.parseFloat(cat1.getText().toString());
b=Float.parseFloat(cat2.getText().toString());
c=Float.parseFloat(quiz1.getText().toString());
d=Float.parseFloat(quiz2.getText().toString());
e=Float.parseFloat(quiz3.getText().toString());
f=a+b+c+d+e;
tt.setText(Float.toString(f));
}
private void checkFieldsForEmptyValues(){
Button b = (Button) findViewById(R.id.calculate);
String s1 = cat1.getText().toString();
String s2 = cat2.getText().toString();
String s3 = quiz1.getText().toString();
String s4 = quiz2.getText().toString();
String s5 = quiz3.getText().toString();
String s6 = regno.getText().toString();
String s7=s6.substring(0,2);
int digits=Integer.parseInt(s7);
if(s1.equals("") || s2.equals("") || s3.equals("") || s4.equals("") || s5.equals("") || s6.equals("") )
b.setEnabled(false);
else if(digits<10 || digits>14)
{
regno.setError("Enter Correct Registration number");
b.setEnabled(false);
}
else
b.setEnabled(true);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.totalmarks"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>