Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的公共变量显示为';这';“不可用”;?_Java_Android - Fatal编程技术网

Java 为什么我的公共变量显示为';这';“不可用”;?

Java 为什么我的公共变量显示为';这';“不可用”;?,java,android,Java,Android,我试图用数据库中的数据填充listview,但它不允许我分配字符串变量 我读过其他一些关于这方面的文章,但我始终无法理解为什么在使用调试器时,我的变量显示为“'this'isnotavailable” public class InventoryActivity extends AppCompatActivity { private RecyclerView varRecyclerView; private RecyclerView.Adapter varAdapter; private Rec

我试图用数据库中的数据填充listview,但它不允许我分配字符串变量

我读过其他一些关于这方面的文章,但我始终无法理解为什么在使用调试器时,我的变量显示为“'this'isnotavailable”

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

}
公共类InventoryActivity扩展了AppCompatActivity
{
私人RecyclerView varRecyclerView;
私有RecyclerView.Adapter变量适配器;
private RecyclerView.LayoutManager varLayoutManager;
私有静态字符串URL_FindInventory=“MyPHPFile”;
//我试图设置这两个变量
公共字符串itemOneName,itemOneEffect;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
字符串characterID=getIntent().getStringExtra(“characterID”);
ArrayList inventoryList=新的ArrayList();
FindInventory(characterID);
inventoryList.add(新布局项(R.drawable.ic_add_circle,itemOneName,itemoneffect));
添加(新布局项目(R.drawable.ic_添加_圈,“项目二名称”,“项目二的影响”);
varRecyclerView=findviewbyd(R.id.recyclerView);
varRecyclerView.setHasFixedSize(true);
varLayoutManager=新的LinearLayoutManager(此);
varAdapter=新布局适配器(inventoryList);
setLayoutManager(varLayoutManager);
设置适配器(varAdapter);
}
私有void FindInventory(最终字符串字符)
{
StringRequest StringRequest=新的StringRequest(Request.Method.POST,URL\u FindInventory,
新的Response.Listener()
{
@凌驾
公共void onResponse(字符串响应)
{
尝试
{
JSONObject JSONObject=新JSONObject(响应);
字符串结果=jsonObject.getString(“结果”);
如果(结果!=null)
{
JSONArray JSONArray=jsonObject.getJSONArray(“结果”);
for(int i=0;i

当我试图将两个公共字符串的值设置为null时,我始终无法理解为什么它不允许我将从JSON对象读取的变量的值设置为null。

它们为null,因为您的web请求是在将项目添加到列表后发生的

inventoryList
设置为一个字段,并删除您试图设置的两个字符串字段


移动两个
inventoryList。将
方法添加到onResponse中,然后需要通知RecyclerView适配器需要显示新数据

它们为空的原因是当编译器在两行以下执行时(我们称之为第1行和第2行):

-在第1行,异步执行该方法(意味着它不会阻止第2行的执行,第2行将在第1行之后或之前执行)。这会导致变量itemOneName和itemOneEffect为null,因为第2行是在第1行之前执行的,请记住,第1行和第2行是并行执行或同时执行的

要解决此问题,请执行以下操作:

-您必须执行以下操作:

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
在onResponse()中调用这些行之后:


现在看起来很明显,哈哈,感觉像是一个绝对的“坏词”…非常感谢你马上修复了它。我已经在这上面呆了一整天了,哈哈。添加过多的日志记录语句从来都没有坏处
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
String itemName = object.getString("Name").trim(); // this has a value of "Cap of Thinking"
String itemEffect = object.getString("Effect").trim();