java - Android 编译错误

标签 java android

有人可以帮我解释一下吗?我正在为周五到期的期末项目构建这个应用程序。头。是。油炸。

构建时出现此错误

C:\Users\Gary\AndroidStudioProjects\NatureAll.v2\app\src\main\java\com\example\gary\natureallv2\SearchAnimalActivity.java:58: error: incompatible types: int cannot be converted to String
                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(POST,
                                                                                ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

它告诉我 int 无法转换为字符串,但我看不到 int 在哪里。这是我的代码:

package com.example.gary.natureallv2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

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

/**
 * Created by Gary on 12/09/2016.Go Me
 */
public class SearchAnimalActivity extends AppCompatActivity{



        EditText etSearchName;
        Button btnSearch;
        RequestQueue requestQueue;
        String showUrl = "http://192.168.1.10/myDocs/mainProject/search_animal_and.php";
       // String showUrl = "http://192.168.1.10/tutorial/showStudents.php";
        TextView tvName;
        TextView tvLatinName;
        TextView tvDescription;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.search_animal_activity);
            etSearchName = (EditText) findViewById(R.id.etSearchName);
            tvName = (TextView) findViewById(R.id.tvName);
            tvLatinName = (TextView) findViewById(R.id.tvLatinName);
            tvDescription = (TextView) findViewById(R.id.tvDescription);
            btnSearch = (Button) findViewById(R.id.btnSearch);



            requestQueue = Volley.newRequestQueue(getApplicationContext());

            btnSearch.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                   // System.out.println("ww");
                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                            showUrl, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            System.out.println(response.toString());
                            try {
                                JSONArray animals = response.getJSONArray("animals");
                                for (int i = 0; i < animals.length(); i++) {
                                    JSONObject animal = animals.getJSONObject(i);
//Change here for different string names
                                    String name = animal.getString("name");
                                    String latinName = animal.getString("latinName");
                                    String description = animal.getString("description");

                                    tvName.append(name);
                                    tvLatinName.append(latinName);
                                    tvDescription.append(description);
                                }
                                //result.append("===\n");

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            System.out.append(error.getMessage());

                        }
                    });
                    requestQueue.add(jsonObjectRequest);
                }
            });




        }

    }

提前非常感谢

最佳答案

tvName.append(name);
tvLatinName.append(latinName);
tvDescription.append(description);

您正在尝试将字符串附加到具有 id (int) 值的 View 。这行不通。您应该在 View 上调用 .setText() 并将文本设置为其当前值加上您想要附加的内容。

关于java - Android 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39451899/

相关文章:

java - 从 3 个字符串中找出字典顺序最小的字符串

java - CXF 返回元素列表

java - 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 以及如何防止它?

Java:初始化的内联私有(private)最终字段为空

java - 如何从自定义编辑器 hybris 更新/刷新编辑器区域

java - Android应用架构——浏览一个xml文件

java - android中每个 TextView 的单独事件监听器

android - 在 ViewFlipper 小部件中的各个元素上设置点击监听器

android - Android 中的 HttpURLConnection 工作正常,但 OkHttp 在 Kitkat 上给出 "Network is unreachable"

android - 如何在android中将 View 添加到另一个 View ?