简体   繁体   English

为什么我的公共变量显示为“'this'is not available”?

[英]Why is my public variable being shown as “ 'this' is not available”?

I am trying to populate a listview with data from a database but it won't allow me to assign a string variable. 我正在尝试使用数据库中的数据填充listview,但不允许我分配字符串变量。

i have read some other articles on this but i cannot for the life of me figure out why my variable is shown as " 'this' is not available " When i use the debugger. 我已经阅读了其他文章,但是我无法终生弄清楚为什么我的变量显示为“'this'is not available”(当我使用调试器时)。

public class InventoryActivity extends AppCompatActivity
{
private RecyclerView varRecyclerView;
private RecyclerView.Adapter varAdapter;
private RecyclerView.LayoutManager varLayoutManager;

private static String URL_FindInventory = "MyPHPFile";

//IM TRYING TO SET THESE TWO VARIABLES
public String itemOneName, itemOneEffect;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inventory);

    String characterID = getIntent().getStringExtra("characterID");

    ArrayList<LayoutItem> inventoryList = new ArrayList<>();

    FindInventory(characterID);

    inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, itemOneName, itemOneEffect));
    inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, "Item Two Name", "Item Two's Effect"));

    varRecyclerView = findViewById(R.id.recyclerView);
    varRecyclerView.setHasFixedSize(true);
    varLayoutManager = new LinearLayoutManager(this);
    varAdapter = new LayoutAdapter(inventoryList);

    varRecyclerView.setLayoutManager(varLayoutManager);
    varRecyclerView.setAdapter(varAdapter);
}


private void FindInventory(final String characterID)
{
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_FindInventory,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);

                        String result = jsonObject.getString("result");

                        if (result != null)
                        {
                            JSONArray jsonArray = jsonObject.getJSONArray("result");

                            for(int i = 0; i < jsonArray.length(); i++)
                            {
                                JSONObject object = jsonArray.getJSONObject(i);

           //IM TRYING TO USE THESE TWO VARIABLES TO SET THE PUBLIC ONES.
                                String itemName = object.getString("Name").trim(); // this has a value of "Cap of Thinking"
                                String itemEffect = object.getString("Effect").trim(); // this has a value of "Helps the user to think +2 Intelligence"

                                itemOneName = itemName;  // THIS IS SHOWN AS "ItemOneName = 'this' is not available "
                                itemOneEffect = itemEffect; // THIS IS SHOWN AS "ItemOneEffect = 'this' is not available "

                            }

                        }
                        else if ((result.equals("error")))
                        {
                            Toast.makeText(InventoryActivity.this, "Cannot find Inventory", Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e)
                    {
                        e.printStackTrace();
                        Toast.makeText(InventoryActivity.this, "Exception Error " + e.toString(), Toast.LENGTH_LONG).show();

                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(InventoryActivity.this, "Error " + error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("characterid", characterID);

            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

When I'm trying to set the value of the 2 public strings they are being set as null, I can't for the life of me figure out why it won't allow me to set there value to the variables I read from the JSON object. 当我尝试将2个公共字符串的值设置为null时,我一生都无法弄清楚为什么它不允许我将其中的值设置为从JSON对象。

They are null because your web request happens after you added the items to the lists. 它们为null,因为在将项目添加到列表后,您的Web请求就会发生。

Make inventoryList a field and remove the two string fields you're trying to set inventoryList中列出一个字段并删除您要设置的两个字符串字段

Move the two inventoryList.add methods into the onResponse, then you need to notify the RecyclerView adapter that new data needs to be displayed 将两个inventoryList.add方法添加到onResponse中,然后您需要通知RecyclerView适配器需要显示新数据

The reason they are null is because when the compiler executes below two lines(let's call it line 1 and line 2): 它们为null的原因是因为当编译器在两行以下执行时(我们将其称为行1和行2):

FindInventory(characterID);//line 1

inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, itemOneName, itemOneEffect));//line 2

-At line 1, the method gets executed asynchronously(means that it will not block the execution of line 2, line 2 will be executed either after or before line 1). -在第1行,该方法被异步执行(意味着它不会阻塞第2行的执行,第2行将在第1行之后或之前执行)。 This causes the variables itemOneName and itemOneEffect to be null, since line 2 was executed before line 1, remember line 1 and line 2 are being executed in parallel or same time. 这将导致变量itemOneName和itemOneEffect为null,因为第2行是在第1行之前执行的,请记住第1行和第2行是在并行或同时执行的。

To fix this: 要解决此问题:

-You have to do below: -您必须执行以下操作:

inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, itemOneName, itemOneEffect));
inventoryList.add(new LayoutItem(R.drawable.ic_add_circle, "Item Two Name", "Item Two's Effect"));

...and other dependencies

After you invoke these lines within onResponse(): 在onResponse()中调用这些行之后:

String itemName = object.getString("Name").trim(); // this has a value of "Cap of Thinking"
String itemEffect = object.getString("Effect").trim();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM