Android Studio:错误:无法应用类RecipeService中的方法getByCourseType

时间:2016-04-03 18:32:59

标签: android android-studio listener

您好我遇到了错误。有一段时间它只是工作正常然后我只是编辑一些布局然后我运行它。它导致了这样的错误,我真的不知道为什么它发生了我试图把东西放回去,但它仍然显示相同的错误。希望你能帮助我。谢谢!

enter image description here

Favorites.java

package com.thesis.heppie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.thesis.heppie.R;
import com.thesis.heppie.adapter.RecipeAdapter;
import com.thesis.heppie.helper.SQLiteHandler;
import com.thesis.heppie.helper.SessionManager;
import com.thesis.heppie.service.RecipeService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Favorites extends AppCompatActivity {
private SessionManager session;
private SQLiteHandler db;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favorites);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    // SqLite database handler
    db = new SQLiteHandler(getApplicationContext());

    // session manager
    session = new SessionManager(getApplicationContext());

    if (!session.isLoggedIn()) {
        logoutUser();
    }

    initNavigationDrawer();
}

public void initNavigationDrawer() {


    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            int id = menuItem.getItemId();

            switch (id) {
                case R.id.home:
                    Intent h = new Intent(Favorites.this, MainActivity.class);
                    startActivity(h);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.mymeals:
                    Toast.makeText(getApplicationContext(), "My Meals", Toast.LENGTH_SHORT).show();
                    drawerLayout.closeDrawers();
                    break;
                case R.id.calories:
                    Intent i = new Intent(Favorites.this, Calories.class);
                    startActivity(i);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.logout:
                    logoutUser();
                    Intent intent = new Intent(Favorites.this, LoginActivity.class);
                    startActivity(intent);
                    finish();

            }
            return true;
        }
    });
    View header = navigationView.getHeaderView(0);

    // Fetching user details from sqlite
    HashMap<String, String> user = db.getUserDetails();
    String name = user.get("name");
    String email = user.get("email");

    TextView tv_name = (TextView) header.findViewById(R.id.tv_name);
    TextView tv_email = (TextView) header.findViewById(R.id.tv_email);
    tv_name.setText(name);
    tv_email.setText(email);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);

    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View v) {
            super.onDrawerClosed(v);
        }

        @Override
        public void onDrawerOpened(View v) {
            super.onDrawerOpened(v);
        }
    };
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
}

private void initViews(int category) {
    final RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recipe_recycler_view);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    RecipeService.getByCourseType(this, category, new Response.Listener<String>() {
        List<com.thesis.heppie.model.Recipe> recipes = new ArrayList<>();

        @Override
        public void onResponse(String response) {
            try {
                JSONObject jObj = new JSONObject(response);
                JSONArray result = jObj.getJSONArray("result");
                for (int i = 0; i < result.length(); i++) {
                    com.thesis.heppie.model.Recipe recipe = new com.thesis.heppie.model.Recipe();
                    JSONObject object = (JSONObject) result.get(i); 
                    String name = (String) object.get("name"); 
                    String image = (String) object.get("image"); 
                    String id = (String) object.get("id");
                    String description = (String) object.get("description");
                    String instruction = (String) object.get("instruction");
                    recipe.setName(name);
                    recipe.setImage(image);
                    recipe.setId(Integer.parseInt(id));
                    recipe.setDescription(description);
                    recipe.setInstruction(instruction);
                    recipes.add(recipe);
                }
                RecipeAdapter adapter = new RecipeAdapter(getApplicationContext(), recipes);
                recyclerView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

}

private void logoutUser() {
    session.setLogin(false);

    db.deleteUsers();

}

}

RecipeService.java

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.thesis.heppie.activity.Filter;
import com.thesis.heppie.app.AppConfig;
import com.thesis.heppie.model.Recipe;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class RecipeService {

public static int COURSE_ID = 0;

public  static List<Recipe> getAllRecipe(Context context){
    RequestQueue queue = Volley.newRequestQueue(context);

    final List<Recipe> list = new ArrayList<>();
    StringRequest stringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_GETRECIPE,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jObj = new JSONObject(response);
                        JSONArray result =  jObj.getJSONArray("result"); 
                            Recipe recipe = new Recipe();
                            JSONObject object = (JSONObject)result.get(i); 
                            String name = (String) object.get("name"); 
                            String image = (String) object.get("image"); 
                            String id = (String) object.get("id");
                            recipe.setName(name);
                            recipe.setImage(image);
                            recipe.setId(Integer.parseInt(id));
                            list.add(recipe);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("That didn't work!", "asd");
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return super.getParams();
        }
    };

    queue.add(stringRequest);
    return list;
}

public static void getByCourseType(Context context, Response.Listener<String> listener){
    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_GETRECIPE,
           listener, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("That didn't work!", "asd");
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<>();
            map.put("courseType", String.valueOf(COURSE_ID)); 
            map.put("disease", Filter.DISEASE != null ?  String.valueOf(Filter.DISEASE.getId()) : "0");
            return map;
        }
    };
    queue.add(stringRequest);
}


public  static void getById(Context context, final int id, Response.Listener<String> listner){
    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_GETRECIPE,
          listner, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("That didn't work!", "asd");
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> map = new HashMap<>();
            map.put("recipeId", String.valueOf(id));
            return map;
        }
    };

    queue.add(stringRequest);

}


}

1 个答案:

答案 0 :(得分:1)

你好,你可以看到问题是关于你的方法的构造函数。这就是您getByCourseType方法定义的方式

public static void getByCourseType(Context context, Response.Listener<String> listener){}

但是您将错误的数据传递给getByCourseType

上的Favourites.java

所以你应该替换

RecipeService.getByCourseType(this, category, new Response.Listener<String>() {//your codes here}

RecipeService.getByCourseType(this, new Response.Listener<String>() {//your codes here}