【问题标题】:not sure why im getting "Cannot resolve method 'getSystemService(java.lang.String)"不知道为什么我得到“无法解析方法'getSystemService(java.lang.String)”
【发布时间】:2017-10-14 04:27:38
【问题描述】:

我试图让 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);
            }
        });

    }
}

以上是我的主要活动,我遇到的问题是代码按钮附近的“ADAPTER”注释。 item 适配器的代码如下所示

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;
    }
}

标记,使用 cmets,是我在“getSystemService”处得到标题中提到的错误的地方。

当我将 itemAdapter 移动到 onClickListener 中时,Android Studio 建议我将 ItemAdapter 中的参数从“Context”更改为“View.OnClickListener”,从而在标题中生成显示的错误。

提前感谢您的帮助我是 java 和 android studio 的新手,如果发现任何愚蠢的错误,请原谅我。

【问题讨论】:

标签: java android


【解决方案1】:

当您执行new View.OnClickListener() { 时,您将创建一个新的匿名内部类,而this 在其中使用时是对其实例的引用。

你应该改变

new ItemAdapter(this ,prayerNamez,prayerTimez);

到:

new ItemAdapter(mContext ,prayerNamez,prayerTimez);

然后将你的构造函数改回接受Context

【讨论】:

  • 谢谢你的工作,你能详细说明 mContext 实际代表什么吗?
  • @MajidAlashari 它在您的代码中...mContext = this; 它是MainActivity 的一个实例
  • "this" 在 OnClickListerner() 之外初始化将指代主要活动而不是匿名内部类,再次感谢!
  • @MajidAlashari 是的,经过您的编辑,我看到您明白了。另请参阅 Pavneet 链接的答案,了解获取外部 this 的另一种方法。
猜你喜欢
  • 2019-02-13
  • 2019-09-05
  • 1970-01-01
  • 2017-09-04
  • 2019-03-30
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多