我的主要课程是:
package com.myapps.myapp;
import android.os.Bundle;
//...and all the other imports
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
//...
}
MoneyCalc moneyCalculator = new MoneyCalc();
moneyCalculator.MoneyCalculate(this);
}
我的第二堂课:
package com.myapps.myapp;
public class MoneyCalc{
public void MoneyCalculate(MainActivity activity){
TextView textview = (TextView) activity.findViewById(R.id.MoneyResult);
EditText edittext = (EditText) activity.findViewById(R.id.MoneyInput);
Spinner spinner = (Spinner) activity.findViewById(R.id.MoneySpinner1);
Spinner spinner2 = (Spinner) activity.findViewById(R.id.MoneySpinner2);
//...
}
}
第二类不是活动,我只保留一些方法。单击按钮时会调用MoneyCalc方法。我无法阅读MainActivity中的方法,它说:
Multiple markers at this line
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token "this", delete this token
- Syntax error on token "MoneyCalculate", Identifier expected after this
token
logcat错误:
01-24 17:13:31.071: E/AndroidRuntime(3662): FATAL EXCEPTION: main
01-24 17:13:31.071: E/AndroidRuntime(3662): java.lang.IllegalStateException: Could not find a method MoneyCalculate(View) in the activity class com.myapps.myapp.MainActivity for onClick handler on view class android.widget.Button with id 'CalculateButton'
01-24 17:13:31.071: E/AndroidRuntime(3662): at android.view.View$1.onClick(View.java:3584)
01-24 17:13:31.071: E/AndroidRuntime(3662): at android.view.View.performClick(View.java:4202)
01-24 17:13:31.071: E/AndroidRuntime(3662): at android.view.View$PerformClick.run(View.java:17340)
01-24 17:13:31.071: E/AndroidRuntime(3662): at android.os.Handler.handleCallback(Handler.java:725)
01-24 17:13:31.071: E/AndroidRuntime(3662): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 17:13:31.071: E/AndroidRuntime(3662): at android.os.Looper.loop(Looper.java:137)
01-24 17:13:31.071: E/AndroidRuntime(3662): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-24 17:13:31.071: E/AndroidRuntime(3662): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 17:13:31.071: E/AndroidRuntime(3662): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 17:13:31.071: E/AndroidRuntime(3662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-24 17:13:31.071: E/AndroidRuntime(3662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-24 17:13:31.071: E/AndroidRuntime(3662): at dalvik.system.NativeStart.main(Native Method)
01-24 17:13:31.071: E/AndroidRuntime(3662): Caused by: java.lang.NoSuchMethodException: MoneyCalculate [class android.view.View]
01-24 17:13:31.071: E/AndroidRuntime(3662): at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-24 17:13:31.071: E/AndroidRuntime(3662): at java.lang.Class.getMethod(Class.java:915)
01-24 17:13:31.071: E/AndroidRuntime(3662): at android.view.View$1.onClick(View.java:3577)
01-24 17:13:31.071: E/AndroidRuntime(3662): ... 11 more
答案 0 :(得分:1)
试试这个。将您的代码放在onCreate中并检查..
@Override
public void onCreate(Bundle savedInstanceState) {
//...
MoneyCalc moneyCalculator = new MoneyCalc();
moneyCalculator.MoneyCalculate(this);
}
答案 1 :(得分:0)
这应该有效:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
}
MoneyCalc moneyCalculator = new MoneyCalc();
{
moneyCalculator.MoneyCalculate(this);
}
}
但我不确定这是你想要的,也不是一种好的编码方式。也许你应该移动moneyCalculator.MoneyCalculate(this); to onCreate方法或已经设置了活动内容的其他地方。现在它会编译但不会工作)