我收到以下錯誤我我收到此錯誤,同時實施刷卡刷新佈局
setOnRefreshListener (android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener) 在SwipeRefreshLayout不能應用於 到 (in.com .goalert.activity.MainActivity)
在
swipeRefreshLayout.setOnRefreshListener(this);
全碼低於
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private SessionManager pref;
private SQLiteHandler db;
private ListView listView;
private FeedListAdapter listAdapter;
public static List<FeedItem> feedItems =new ArrayList<>();
private SwipeRefreshLayout swipeRefreshLayout;
private String URL_FEED = "http://api.androidhive.info/feed/feed.json";
static int count = 0;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initNavigationDrawer();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
pref = new SessionManager(getApplicationContext());
db = new SQLiteHandler(getApplicationContext());
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this);
// We first check for cached request
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(URL_FEED);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
parseJsonFeed(new JSONObject(data));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}else {
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
URL_FEED, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
public void onRefresh() {
Log.i("onRefresh","onRefresh");
fetchfeed();
}
private void fetchfeed() {
Log.i("fetchfeed","fetchfeed");
String URL_FEED = "http://www.goalert.in/feed/engineering.json";
swipeRefreshLayout.setRefreshing(true);
JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
URL_FEED, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
VolleyLog.d("Response",response.toString());
if (response != null) {
swipeRefreshLayout.setRefreshing(false);
parseJsonFeed(response);
}
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("", "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonReq);
// Adding request to volley request queue
//AppController.getInstance().addToRequestQueue(jsonReq);
// showing refresh animation before making http call
}
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("feed");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));
item.setName(feedObj.getString("name"));
// Image might be null sometimes
String image = feedObj.isNull("image") ? null : feedObj
.getString("image");
item.setImge(image);
item.setStatus(feedObj.getString("status"));
item.setProfilePic(feedObj.getString("profilePic"));
item.setTimeStamp(feedObj.getString("timeStamp"));
// url might be null sometimes
String feedUrl = feedObj.isNull("url") ? null : feedObj
.getString("url");
item.setUrl(feedUrl);
feedItems.add(item);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
是的我實施了這個 –