我创建了一个简单的类来获取两个整数值并将它们一起添加并将输出作为整数返回。这是我在plussum.java类中的类:
package com.mycompany.myapp;
public class plusSum
{
// variables
// empty constructor
public plusSum(){}
// constructor
public int sumIt(int a, int b){
int c = a + b;
return c;
}
}
这是我的mainactivity.java:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.lang.*;
public class MainActivity extends Activity
{
EditText val1;
EditText val2;
int rslt,finalval1,finalval2;
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
val1 = (EditText) findViewById(R.id.num1);
val2 = (EditText) findViewById(R.id.num2);
String fv = val1.getText().toString();
finalval1 = Integer.parseInt(fv);
finalval2 = Integer.parseInt(val2.getText().toString());
int summed = plusSum.sumIt(finalval1,finalval2);
TextView rslt = (TextView) findViewById(R.id.rslt);
rslt.setText(summed);
}
});
}
}
错误这里没有对sumIt这样的引用... 任何帮助...? 我是一个菜鸟,刚刚开始学习如何为Android编写脚本......
答案 0 :(得分:0)
您需要将方法设置为静态,以便在不实例化类的情况下调用它:
public static int sumIt(int a, int b){
int c = a + b;
return c;
}
或者实例化你的课程然后再打电话:
plusSum myClass = new plusSum();
int summed = myClass.sumIt(finalval1,finalval2);
答案 1 :(得分:0)
您必须先创建类的实例,然后才能使用它:
plusSum sp = new plusSum();
int summed = sp.sumIt(finalval1,finalval2);
答案 2 :(得分:0)
试试这个:
plusSum plussum=new plusSum();
int summed = plussum.sumIt(finalval1,finalval2);
或:
int summed = new plusSum().sumIt(finalval1,finalval2);