类型未定义的方法

Method undefined by the type

我正在尝试将字符串从 "mainActivity" 传递到 class "GuassAlgorithm" 中的方法 "getDate(String s)",在同一个包中找到。

方法returns一个字符串。

但是,我在两次调用该方法时都得到了 "The method getDate(String) is undefined for the type MainActivity"。 (String date = getDate("date") 多此一举,是为了验证错误是否可重复)

我已经在相关代码行上方添加了注释。

下面是主要内容 Activity:

package dayOfWeekApp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.example.com.piercestudio.R;

import dayOfWeekApp.GuassAlgorithm;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//此行Activity类型的MainActivity未定义方法getDate(String)

String date = getDate("date");

    final Button calculateButton = (Button) findViewById(R.id.button1);
    final TextView resultView = (TextView) findViewById(R.id.textView1);
    final EditText editTextField = (EditText) findViewById(R.id.editText1);


    calculateButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){

//getDate(String) 方法未定义类型 new View.OnClickListener(){}

            resultView.setText(getDate("10/25/1980"));
        }

    });        
}

下面是高斯算法class.

}

package dayOfWeekApp;

public class GuassAlgorithm {

public String getDate(String s){

    s = "10/25/1980";

    int day = Integer.parseInt(s);
    String weekDay = "error";

    //
    int centuries[][] = {{4, 1700}, {2,1800}, {0,1900}, {6,2000}, {4,2100}, {2,2200}, {0, 2300}, {6,2400}, {4, 2600}};
    Object months[][] = {{1, "January", 0, 6}, {2, "February", 3, 2},{3, "March", 3, 3}, {4, "April", 6, 6}, {5, "May", 1, 1}, {6, "June", 4, 4}, {7, "July", 6, 6}, {8, "August", 2, 2}, {9, "September", 5, 5}, {10, "October", 0, 0}, {11, "November", 3, 3}, {12, "December", 5, 5}};
    Object days[][] = {{0, "Sunday"}, {1, "Monday"}, {2, "Tuesday"},{3, "Wednesday"}, {4, "Thursday"}, {5, "Friday"}, {6, "Saturday"}};

    //t.charAt(0)[]
    int yearInt = Integer.parseInt(s.substring(6, 8)) * 1000;
    weekDay = Integer.toString(yearInt);

        return weekDay;
}
}

不要因为我使用数组而取笑我。我习惯了Excel.

您要么需要在对象的实例上访问它:

GuassAlgorithm ga = new GuassAlgorithm();
ga.getDate("10/25/1980");

或者将算法更改为静态并以这种方式访问​​它:

public static String getDate(String s) { ... }

GuassAlgorithm.getDate("10/25/1980")

发生的事情是它正在当前上下文中寻找 getDate 方法,但它不存在。所以你必须提供上下文。