簡體   English   中英

定義為接受String的構造函數,但方法調用顯示錯誤“期望的數組類型為java.lang.String”

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

我有一個注冊活動,該活動取決於用戶的選定復選框,從而異步調用服務器以獲取與選定字段相關的技能。 我使用相同的改造。 我定義了一個SQLQuery類,其構造函數帶有String參數。 現在的問題是,當我使用String參數調用構造函數時,它顯示了Array type expected found java.lang.String錯誤。 請有人幫我解決這個問題。

在此先感謝,這是我的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());
        }
    }
}

這是getRelevantSkills.java file

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

您正在嘗試將field[i]傳遞給SQLQuery構造函數,但是fieldString而不是數組。 您應該改為傳遞field

編輯:

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

    ...

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

您有一個String類型的本地field變量,該變量隱藏了同名的實例變量(其類型為String[] )。

如果打算使用實例變量( String[] field = {...}; ),則應編寫:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM