2017-10-14 54 views
0

我嘗試做一個ListView都刷新按鈕,點擊不知道爲什麼即時得到「無法解析法「getSystemService(java.lang.String中)」

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」的地方。 項目適配器的代碼將被顯示在以下

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

,使用的意見,在這裏我得到正好「getSystemService」在標題中提到的錯誤。

當我移動onClickListener Android Studio中的itemAdapter時,建議我將ItemAdapter中的參數從「Context」更改爲「View.OnClickListener」,從而在標題中生成錯誤。

非常感謝您的幫助我是新來的java和android工作室,所以如果發現任何愚蠢的錯誤,請原諒我。

+1

將'View.OnClickListener'更改爲'Context',您的已評論適配器構造函數正常 –

+1

可能重複[如何在非活動類中使用getSystemService?](https://stackoverflow.com/questions/4141555/如何使用,在非活動類的系統服務) –

+1

[Android從匿名類中獲取活動](https://stackoverflow.com/questions/31903355/android-get-activity-from-within -anonymous-class) –

回答

0

當你做new View.OnClickListener() {你創建一個新的匿名內部類和this時使用它是一個實例的引用。

你應該改變

new ItemAdapter(this ,prayerNamez,prayerTimez); 

到:

new ItemAdapter(mContext ,prayerNamez,prayerTimez); 

,改變你的構造函數返回接受Context

+0

謝謝你的工作,你能詳細說明mContext實際代表什麼? –

+0

@MajidAlashari它在你的代碼中......'mContext = this;'它是'MainActivity'的一個實例 – Oleg

+0

「this」被初始化在OnClickListerner()之外將引用主要活動而不是匿名內部類,謝謝再次! –

相關問題