构造函数定义为取String但方法调用显示错误'Array type expected found found java.lang.String'

[英]constructor defined to take String but method invocation shows error 'Array type expected found java.lang.String'


I have a registration activity which depending on user's selected check boxes, makes an asynchronous call to the server to fetch skills relevant to selected field. I am using retrofit for the same. I have defined a SQLQuery class whose constructor takes a String parameter. Now the problem is, when I invoke the constructor with a String argument it shows an error Array type expected found java.lang.String. Please someone help me fix this.

我有一个注册活动,根据用户选择的复选框,对服务器进行异步调用以获取与所选字段相关的技能。我正在使用改造。我已经定义了一个SQLQuery类,其构造函数采用String参数。现在的问题是,当我用String参数调用构造函数时,它显示了一个错误的数组类型,期望找到java.lang.String。请有人帮我解决这个问题。

Thanks in advance here is my java file

在此先感谢这里是我的java文件

package com.example.vishal.internshipseekerapp;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class StudentRegistration extends AppCompatActivity implements View.OnClickListener {

    private final int numFields = 13;
    boolean[] checkField = new boolean[13];
    String[] field = {"computer vision", "content writing", "data mining", "electrical/electronics", "game development", "image processing", "marketing", "mechanical engineering", "mobile app dev", "programming", "software dev", "web dev"};
    Set<Skill> skill = new HashSet<>();

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

        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);

        // register onclick listener for all checkboxes
        ( findViewById(R.id.field0)).setOnClickListener(this);
        ( findViewById(R.id.field1)).setOnClickListener(this);
        ( findViewById(R.id.field2)).setOnClickListener(this);
        ( findViewById(R.id.field3)).setOnClickListener(this);
        ( findViewById(R.id.field4)).setOnClickListener(this);
        ( findViewById(R.id.field5)).setOnClickListener(this);
        ( findViewById(R.id.field6)).setOnClickListener(this);
        ( findViewById(R.id.field7)).setOnClickListener(this);
        ( findViewById(R.id.field8)).setOnClickListener(this);
        ( findViewById(R.id.field9)).setOnClickListener(this);
        ( findViewById(R.id.field10)).setOnClickListener(this);
        ( findViewById(R.id.field11)).setOnClickListener(this);
        //( findViewById(R.id.field12)).setOnClickListener(this);

        // register onclick listener for DONE button
        Button done = (Button) findViewById(R.id.field_select_done);
        done.setOnClickListener(this);
    }

    public void onClick(View v){
        switch(v.getId()){
            case R.id.field0:
                if(((CheckBox) v).isChecked())
                    checkField[0] = true;
            case R.id.field1:
                if(((CheckBox) v).isChecked())
                    checkField[1] = true;
            case R.id.field2:
                if(((CheckBox) v).isChecked())
                    checkField[2] = true;
            case R.id.field3:
                if(((CheckBox) v).isChecked())
                    checkField[3] = true;
            case R.id.field4:
                if(((CheckBox) v).isChecked())
                    checkField[4] = true;
            case R.id.field5:
                if(((CheckBox) v).isChecked())
                    checkField[5] = true;
            case R.id.field6:
                if(((CheckBox) v).isChecked())
                    checkField[6] = true;
            case R.id.field7:
                if(((CheckBox) v).isChecked())
                    checkField[7] = true;
            case R.id.field8:
                if(((CheckBox) v).isChecked())
                    checkField[8] = true;
            case R.id.field9:
                if(((CheckBox) v).isChecked())
                    checkField[9] = true;
            case R.id.field10:
                if(((CheckBox) v).isChecked())
                    checkField[10] = true;
            case R.id.field11:
                if(((CheckBox) v).isChecked())
                    checkField[11] = true;


            case R.id.field_select_done:
                displayRelevantSkills();
        }
    }

    private void displayRelevantSkills() {
        String field = "field";
        String checkBoxName;


        final String SKILL_FIELD_URL = "https://data.outfight74.hasura-app.io/";


//        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        // set request options for all requests
        Retrofit.Builder builder =
                new Retrofit.Builder()
                        .baseUrl(SKILL_FIELD_URL)
                        .addConverterFactory(
                                GsonConverterFactory.create()
                        );

        // create retrofit adapter
        Retrofit retrofit =
                builder
                    /*.client(
                            httpClient.build()
                    )*/
                    .build();

        // create retrofit REST client
        getRelevantSkills skillClient =  retrofit.create(getRelevantSkills.class);

        // for each checkbox do
        for(int i = 0; i < numFields; i++) {
            // if checkbox is ticked
            if(checkField[i]) {
                // fetch relevant skills from server
                SQLQuery skillQuery = new SQLQuery(field[i]);

                Call<List<Skill>> call =
                        skillClient.relevantSkills(skillQuery);

                // Execute the call asynchronously. Get a positive or negative callback.
                call.enqueue(new Callback<List<Skill>>() {
                    @Override
                    public void onResponse(Call<List<Skill>> call, Response<List<Skill>> response) {
                        // The network call was a success and we got a response
                        // add to skills HashSet
                        skill.addAll(response.body());
                        Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<List<Skill>> call, Throwable t) {
                        // the network call was a failure
                        // TODO: handle error
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }

        // display a drop down menu having all elements of HashSet
        for(Skill s : skill)
        {
            CheckBox skillItem = new CheckBox(getApplicationContext());
            skillItem.setText(s.getSkill());
        }
    }
}

and here is the getRelevantSkills.java file

这是getRelevantSkills.java文件

package com.example.vishal.internshipseekerapp;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;


class Where{
    // this will be given by the user
    private String skill;

    public Where(String skill) {
        this.skill = skill;
    }
}

class Args{
    final String table = "skill_field_relation";
    final String[] columns = {"skill"};
    private Where where;

    public Args(String field) {
        where = new Where(field);
    }
}

class SQLQuery{
    final String type = "select";
    private Args args;
    public SQLQuery(java.lang.String field) {
        args = new Args(field);
    }
}

class Skill{
    private String skill;

    public String getSkill() {
        return skill;
    }

    public Skill(String skill) {
        this.skill = skill;
    }
}

public interface getRelevantSkills {
    @POST("/v1/query")
    Call<List<Skill>> relevantSkills(
            @Body SQLQuery fetchSkills
    );
}

1 个解决方案

#1


2  

You are trying to pass field[i] to the SQLQuery constructor, but field is a String, not an array. You should pass field instead.

您正在尝试将field [i]传递给SQLQuery构造函数,但field是String,而不是数组。你应该传递字段。

EDIT:

private void displayRelevantSkills() {
    String field = "field";

    ...

    SQLQuery skillQuery = new SQLQuery(field[i]);
    ...
}

You have a local field variable of type String that hides the instance variable of the same name (whose type is String[]).

您有一个String类型的本地字段变量,它隐藏了同名的实例变量(其类型为String [])。

If you intended to use the instance variable (String[] field = {...};), you should write:

如果您打算使用实例变量(String [] field = {...};),您应该写:

SQLQuery skillQuery = new SQLQuery(this.field[i]);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2017/07/12/9ba56cb68b3d1bde35dc27c1808291e1.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号