【问题标题】:Android Studio Error "Cannot resolve method for method .add"Android Studio 错误“无法解析方法 .add 的方法”
【发布时间】:2018-11-13 04:31:34
【问题描述】:

我知道这个问题被问了很多,但我真的很难过,我看过其他方法,并尝试了一切...... 我的编码问题如下,我在java中有一个公共类,在全局范围内做了一个字符串数组。我尝试使用 array.add("somestringhere") 添加到它,但它吐出一个错误说(标题)......还有一点需要注意的是这个类在文件的主类中。所以让我们看看我是否可以在这里展示它:

public class see_schedule_activity extends AppCompatActivity {
.
..
.
.
.

// you can make this class as another java file so it will be separated from your main activity.
    public class AsyncTaskParseJson extends AsyncTask<String, String, String> {



        final String TAG = "AsyncTaskParseJson.java";
        // set your json string url here
        String yourJsonStringUrl = "http://www.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxphp";
        // contacts JSONArray
        JSONArray dataJsonArr = null;

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected String doInBackground(String... arg0) {
            try {

                // I have a intent passed from teh main class
                Bundle extras = getIntent().getExtras();
                String[] tasks = extras.getStringArray("tasks");


                // instantiate our json parser
                JsonParser jParser = new JsonParser();

                // get json string from url
                JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);

                // get the array of users
                dataJsonArr = json.getJSONArray("schedule");

                // loop through all users
                for (int i = 0; i < dataJsonArr.length(); i++) {

                    JSONObject c = dataJsonArr.getJSONObject(i);

                    // Storing each json item in variable

                    activityid = c.getString("activityid");

                    String yesno = c.getString("yesno");
                    String start = c.getString("start");
                    String stop = c.getString("stop");
                    String plusminus = c.getString("plusminus");


                    Log.e("Hash Map", "Routine ID #" + activityid);
                    if (!TextUtils.isEmpty(activityid) && TextUtils.isDigitsOnly(activityid)) {
                        routine = hm.get(Integer.valueOf(activityid));
                    } else {
                        routine = "";
                    }
                    // TODO: Make the PHP work with a variable residentID
                    //TODO: only make a string if the yes is checked
                    //TODO: make the routine name

//                    Log.e(TAG, "Value for yesno:" + yesno);

                    if (c.getString("yesno").equals("Yes")) {
                        schedule = routine + " starts at " + start + " and ends at " + stop + " with plusorminus " +
                                plusminus + " minutes ";
                        tasks.add(schedule);// This lines yeilds the problem
                        Log.e(TAG, "" + schedule);
                    }

//                    // show the values in our logcat
//                    Log.e(TAG, "activityid: " + activityid
//                            + ", yesno: " + yesno
//                            + ", start: " + start
//                            + ", stop: " + stop
//                            + ", plusminus: " + plusminus);


                }
                Intent intent = new Intent(getApplicationContext(),see_schedule_activity.class);
                intent.putExtra("tasks_filled",tasks);
                startActivity(intent);


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

            return null;
        }


        @Override
        protected void onPostExecute(String strFromDoInBg) {
        }
    }

我会很感激任何帮助,我是编程和 android studio 的新手,而且 php..so 是的,在此先感谢贾斯汀

【问题讨论】:

    标签: java arrays android-studio


    【解决方案1】:

    tasks 是您定义的字符串数组

    String[] tasks = extras.getStringArray("tasks");
    

    而且 Array 没有任何 add 方法。 您需要将tasks 转换为如下列表

    List<String> list = Arrays.asList(tasks);
    

    那么你可以使用list.add(schedule)

    【讨论】:

    • 等等,什么是 .add 的等价物?
    • 没关系,你不能添加到列表中,我必须创建一个列表字符串,然后将其转换为字符串数组!
    • 我无法将您的答案标记为“6 分钟”,然后我会这样做,谢谢 md!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多