简体   繁体   English

不知道为什么我得到“无法解析方法'getSystemService(java.lang.String)”

[英]not sure why im getting “Cannot resolve method 'getSystemService(java.lang.String)”

im trying to make a ListView refresh every button click 我试图使ListView刷新每个按钮单击

public class MainActivity extends AppCompatActivity {
    //private TextView txtPrayerTimes;
    /* **********GPS********** */
    Context mContext;

    /* **********ListView********** */
    ListView myPrayerList;
    double latitude = 21.6001;
    double longitude = 39.136;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //txtPrayerTimes = (TextView) findViewById(R.id.txtPrayerTimes);
        //Button getTime = (Button) findViewById(R.id.getTime);
        Button gpsBtn = (Button) findViewById(R.id.gpsBtn);
        /* **********ListView********** */
        myPrayerList = (ListView) findViewById(R.id.myPrayerList);


        /* **********GPS********** */
        mContext = this;

        ///BUTTON
        gpsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ContextCompat.checkSelfPermission(mContext,
                        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED

                        && ActivityCompat.checkSelfPermission(mContext,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

                } else {
                    Toast.makeText(mContext, "You need have granted permission", Toast.LENGTH_SHORT).show();
                    GPSTracker gps = new GPSTracker(mContext, MainActivity.this);

                    // Check if GPS enabled

                    if (gps.canGetLocation()) {

                        latitude = gps.getLatitude();
                        longitude = gps.getLongitude();

                        // \n is for new line

                        Toast.makeText(getApplicationContext(),
                                "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    } else {

                        // Can't get location.

                        // GPS or network is not enabled.

                        // Ask user to enable GPS/network in settings.

                        gps.showSettingsAlert();
                    }
                }
                //////listView
                String[] prayerNamez;
                String[] prayerTimez;

                double timezone = (Calendar.getInstance().getTimeZone()
                        .getOffset(Calendar.getInstance().getTimeInMillis()))
                        / (1000 * 60 * 60);
                PrayTime prayers = new PrayTime();

                prayers.setTimeFormat(prayers.Time12);
                prayers.setCalcMethod(prayers.Makkah);
                prayers.setAsrJuristic(prayers.Shafii);
                prayers.setAdjustHighLats(prayers.AngleBased);
                int[] offsets = { 0, 0, 0, 0, 0, 0, 0 }; // {Fajr,Sunrise,Dhuhr,Asr,Sunset,Maghrib,Isha}
                prayers.tune(offsets);

                Date now = new Date();
                Calendar cal = Calendar.getInstance();
                cal.setTime(now);

                ArrayList prayerTimes = prayers.getPrayerTimes(cal, latitude,
                        longitude, timezone);
                ArrayList prayerNames = prayers.getTimeNames();

                /* **********ListView********** */
                prayerNamez = new String[5];
                prayerTimez = new String[5];

                for (int i = 0,j = 0;(i+j) < prayerNames.size();i++){
                    if ((i + j) == 1 ||  (i + j) == 4)
                        j++;
                    prayerNamez[i] = (String) prayerNames.get(i+j);
                    prayerTimez[i] = (String) prayerTimes.get(i+j);
                }
                ///ADAPTER
                ItemAdapter itemAdapter = new ItemAdapter(this,prayerNamez,prayerTimez);
                myPrayerList.setAdapter(itemAdapter);
            }
        });

    }
}

above is my main activity, the issue I'm having is where it is commented "ADAPTER" near the button of the code. 上面是我的主要活动,我遇到的问题是在代码按钮旁边被注释为“适配器”的地方。 the item adapter's code will be shown below 项目适配器的代码如下所示

package com.example.majidalashari.myfirstapp2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;



class ItemAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private String[] prayerNamez;
    private String[] prayerTimez;

//    ItemAdapter(Context c, String[] N, String[] T){
//        prayerNamez = N;
//        prayerTimez = T;
//        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//    }

    ItemAdapter(View.OnClickListener c, String[] N, String[] T) {

        prayerNamez = N;
        prayerTimez = T;             /*vvvvvvvvvvvvvvvv*/
        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                     /*^^^^^^^^^^^^^^^^*/
    }

    @Override
    public int getCount() {
        return prayerNamez.length;
    }

    @Override
    public Object getItem(int i) {
        return prayerNamez[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View View, ViewGroup parent) {
        View v = mInflater.inflate(R.layout.my_prayer_detail,null);
        TextView prayerNameTextView = v.findViewById(R.id.prayerNameTextView);
        TextView prayerTimeTextView = v.findViewById(R.id.prayerTimeTextView);

        String name = prayerNamez[i];
        String time = prayerTimez[i];

        prayerNameTextView.setText(name);
        prayerTimeTextView.setText(time);

        return v;
    }
}

marked, using comments, is where I'm getting the error mentioned in the title at exactly "getSystemService". 使用注释标记的位置是我在标题中确切地在“ getSystemService”处得到的错误的地方。

when I moved the itemAdapter inside of the onClickListener Android Studio suggested that I change a parameter in my ItemAdapter from "Context" to "View.OnClickListener" thus generating the presented error in the title. 当我将itemAdapter移到onClickListener的内部时,Android Studio建议我将ItemAdapter中的参数从“上下文”更改为“ View.OnClickListener”,从而在标题中产生显示的错误。

thank you in advance for your help I am new to java and to android studio so forgive me if any dumb mistakes were found. 预先感谢您的帮助我是java和android studio的新手,如果发现任何愚蠢的错误,请原谅我。

When you do new View.OnClickListener() { you create a new anonymous inner class and this when used inside of it is a reference to an instance of it. 当你new View.OnClickListener() {创建一个新的匿名内部类和this里面它是它的一个实例的引用时使用。

You should change 你应该改变

new ItemAdapter(this ,prayerNamez,prayerTimez);

to: 至:

new ItemAdapter(mContext ,prayerNamez,prayerTimez);

And change your constructor back to accept Context . 并将您的构造方法改回接受Context

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图获得蓝牙适配器; 无法解析方法“ GetSystemService(java.lang.String)” - Trying to get BluetoothAdapter; Cannot resolve method 'GetSystemService(java.lang.String)' How to solve this error “cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'”? - How to solve this error “cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'”? 为什么我收到此错误? java.lang.ClassCastException:android.text.SpannableStringBuilder 无法转换为 java.lang.String - Why Im getting this error? java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.lang.String 无法解析方法put(java.lang.string,java.lang.string) - Cannot resolve method put(java.lang.string, java.lang.string) 无法解析方法“ List.of(java.lang.String,java.lang.String)” - Cannot resolve method 'List.of(java.lang.String, java.lang.String)' 无法解析方法“get(java.lang.String)” - Cannot resolve method 'get(java.lang.String)' 无法解析方法“setText(java.lang.String[])” - Cannot resolve method 'setText(java.lang.String[])' Android:无法解析方法“ query(java.lang.string)” - Android: cannot resolve method “query(java.lang.string)” “无法解析方法'openFileOutput(Java.lang.String,int)'” - “Cannot resolve method 'openFileOutput(Java.lang.String, int)'” 无法解析方法&#39;displayMessage(java.lang.String)&#39; - Cannot resolve method 'displayMessage(java.lang.String)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM