我在Android应用程序初学者。我目前正在为我大学的学生做一个应用程序。
首先,我首先在MainActivity启动此CGPA计算器。 它工作正常,然后我继续这样做。 但是我的老板要求我在这个页面之前做一个导航页面。
然后我发现我需要另一个MainActivity是android将启动的第一个页面。 我尝试改变Manifest中的某些东西,但它不起作用
然后我创建一个新项目并首先在MainActivity上构建导航页面。 我创建另一个类文件名计算器,然后我复制整个代码并放入内部。
现在我使用intent从MainActivity导航到Calculator类,它会崩溃。 但它适用于其他页面。所以我怀疑这堂课有些不对劲。 当我尝试点击按钮导航到计算器时,我在日志中发现它说Fatal Error Main。
请帮我看看。 对不起,我的语言不好。
这是我的MainActivity Intent代码
public void calc(View view) {
Intent intent = new Intent(this, Calculator.class);
startActivity(intent);
}
这是我的calculator.class
package com.example.mobileassignment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
@SuppressLint("UseValueOf")
public class Calculator extends Activity implements OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
EditText editDec = (EditText) findViewById(R.id.editLastSem);
setSpinner();
sGrade1.setOnItemSelectedListener(this);
sGrade2.setOnItemSelectedListener(this);
sGrade3.setOnItemSelectedListener(this);
sGrade4.setOnItemSelectedListener(this);
sGrade5.setOnItemSelectedListener(this);
sCredit1.setOnItemSelectedListener(this);
sCredit2.setOnItemSelectedListener(this);
sCredit3.setOnItemSelectedListener(this);
sCredit4.setOnItemSelectedListener(this);
sCredit5.setOnItemSelectedListener(this);
}
Spinner sGrade1,sGrade2,sGrade3,sGrade4,sGrade5,sCredit1,sCredit2,sCredit3,sCredit4,sCredit5;
Double result1,result2,result3,result4,result5,cresult1,cresult2,cresult3,cresult4,cresult5;
String ans;
public void setSpinner(){
sGrade1 = (Spinner) findViewById(R.id.spinGrade1);
sGrade2 = (Spinner) findViewById(R.id.spinGrade2);
sGrade3 = (Spinner) findViewById(R.id.spinGrade3);
sGrade4 = (Spinner) findViewById(R.id.spinGrade4);
sGrade5 = (Spinner) findViewById(R.id.spinGrade5);
sCredit1 = (Spinner) findViewById(R.id.spinCredit1);
sCredit2 = (Spinner) findViewById(R.id.spinCredit2);
sCredit3 = (Spinner) findViewById(R.id.spinCredit3);
sCredit4 = (Spinner) findViewById(R.id.spinCredit4);
sCredit5 = (Spinner) findViewById(R.id.spinCredit5);
ArrayAdapter<CharSequence> adapterGrade = ArrayAdapter.createFromResource(this,R.array.gradeList, android.R.layout.simple_spinner_item);
adapterGrade.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<CharSequence> adapterCredit = ArrayAdapter.createFromResource(this,R.array.creditList, android.R.layout.simple_spinner_item);
adapterCredit.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sGrade1.setAdapter(adapterGrade);
sGrade2.setAdapter(adapterGrade);
sGrade3.setAdapter(adapterGrade);
sGrade4.setAdapter(adapterGrade);
sGrade5.setAdapter(adapterGrade);
sCredit1.setAdapter(adapterCredit);
sCredit2.setAdapter(adapterCredit);
sCredit3.setAdapter(adapterCredit);
sCredit4.setAdapter(adapterCredit);
sCredit5.setAdapter(adapterCredit);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
public double checkPoint(String sub1){
double res = 0;
if(sub1.equals("A")){
res = 4.0;
}else if(sub1.equals("A-")){
res = 3.75;
}else if(sub1.equals("B+")){
res = 3.5;
}else if(sub1.equals("B")){
res = 3.0;
}else if(sub1.equals("B-")){
res = 2.75;
}else if(sub1.equals("C+")){
res = 2.5;
}else if(sub1.equals("C")){
res = 2.0;
}else if(sub1.equals("C-")){
res = 1.75;
}else if(sub1.equals("D")){
res = 1.0;
}else if(sub1.equals("F")){
res = 0.0;
}else if(sub1.equals("-")){
res = 0.0;
}else{
res = 123;
}
return res;
}
public double checkCredit(String cre){
double resu = 0;
if(cre.equals("1")){
resu = 1.0;
}else if(cre.equals("2")){
resu = 2.0;
}else if(cre.equals("3")){
resu = 3.0;
}else if(cre.equals("4")){
resu = 4.0;
}else if(cre.equals("5")){
resu = 5.0;
}else if(cre.equals("-")){
resu = 0.0;
}else {
resu = 123;
}
return resu;
}
public void calculateGPA(View view) {
sGrade1.setOnItemSelectedListener(this);
sGrade2.setOnItemSelectedListener(this);
sGrade3.setOnItemSelectedListener(this);
sGrade4.setOnItemSelectedListener(this);
sGrade5.setOnItemSelectedListener(this);
sCredit1.setOnItemSelectedListener(this);
sCredit2.setOnItemSelectedListener(this);
sCredit3.setOnItemSelectedListener(this);
sCredit4.setOnItemSelectedListener(this);
sCredit5.setOnItemSelectedListener(this);
result1 = checkPoint((String)sGrade1.getSelectedItem());
result2 = checkPoint((String)sGrade2.getSelectedItem());
result3 = checkPoint((String)sGrade3.getSelectedItem());
result4 = checkPoint((String)sGrade4.getSelectedItem());
result5 = checkPoint((String)sGrade5.getSelectedItem());
cresult1 = checkCredit((String)sCredit1.getSelectedItem());
cresult2 = checkCredit((String)sCredit2.getSelectedItem());
cresult3 = checkCredit((String)sCredit3.getSelectedItem());
cresult4 = checkCredit((String)sCredit4.getSelectedItem());
cresult5 = checkCredit((String)sCredit5.getSelectedItem());
EditText editDec = (EditText) findViewById(R.id.editLastSem);
double lastSem = 0;
if(editDec.getText().toString().matches("")||editDec.getText().toString().matches(".") ){
}else{
// lastSem = Double.parseDouble(editDec.getText().toString());
}
double totalcre = cresult1 + cresult2 + cresult3 + cresult4 + cresult5;
double total = (result1 * cresult1) + (result2 * cresult2) + (result3 * cresult3) + (result4 * cresult4) + (result5 * cresult5);
double gpa = total / totalcre;
if(editDec.getText().toString().matches("")||editDec.getText().toString().matches(".") ){
lastSem = gpa;
}
double cgpa = (gpa + lastSem) / 2;
//Toast.makeText(this, ans +"" + result1 +""+ cresult1 +""+ result2 +""+ cresult2 +""+ result3 +""+ cresult3 , Toast.LENGTH_LONG).show();
Toast.makeText(this, "Your GPA : " + gpa + "\n" + "Your CGPA : " + cgpa , Toast.LENGTH_LONG).show();
}
}
这是我发现的Log。
08-13 21:43:55.925: E/AndroidRuntime(11192): FATAL EXCEPTION: main
08-13 21:43:55.925: E/AndroidRuntime(11192): java.lang.IllegalStateException: Could not execute method of the activity
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.view.View$1.onClick(View.java:3725)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.view.View.performClick(View.java:4262)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.view.View$PerformClick.run(View.java:17351)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.os.Handler.handleCallback(Handler.java:615)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.os.Handler.dispatchMessage(Handler.java:92)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.os.Looper.loop(Looper.java:137)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.app.ActivityThread.main(ActivityThread.java:4935)
08-13 21:43:55.925: E/AndroidRuntime(11192): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 21:43:55.925: E/AndroidRuntime(11192): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 21:43:55.925: E/AndroidRuntime(11192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
08-13 21:43:55.925: E/AndroidRuntime(11192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-13 21:43:55.925: E/AndroidRuntime(11192): at dalvik.system.NativeStart.main(Native Method)
08-13 21:43:55.925: E/AndroidRuntime(11192): Caused by: java.lang.reflect.InvocationTargetException
08-13 21:43:55.925: E/AndroidRuntime(11192): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 21:43:55.925: E/AndroidRuntime(11192): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.view.View$1.onClick(View.java:3720)
08-13 21:43:55.925: E/AndroidRuntime(11192): ... 11 more
08-13 21:43:55.925: E/AndroidRuntime(11192): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mobileassignment/com.example.mobileassignment.MainActivity$Calculator}; have you declared this activity in your AndroidManifest.xml?
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1556)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.app.Activity.startActivityForResult(Activity.java:3446)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.app.Activity.startActivityForResult(Activity.java:3407)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.app.Activity.startActivity(Activity.java:3617)
08-13 21:43:55.925: E/AndroidRuntime(11192): at android.app.Activity.startActivity(Activity.java:3585)
08-13 21:43:55.925: E/AndroidRuntime(11192): at com.example.mobileassignment.MainActivity.calc(MainActivity.java:47)
08-13 21:43:55.925: E/AndroidRuntime(11192): ... 14 more
答案 0 :(得分:0)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mobileassignment/com.example.mobileassignment.MainActivity$Calculator}; have you declared this activity in your AndroidManifest.xml?
表示您尚未在AndroidManifest.xml中声明Calculator Activity
<activity
android:name=".Calculator " >
</activity>