我有一个可搜索的微调器,其中包含首先手动输入的客户端列表。一切都运作良好。 首先,我添加到gradle:compile'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1' 然后我创建了一个fagment和一个布局来测试searcheable spinner,evrything工作。 现在,我想通过json文件将一个客户端列表添加到我可搜索的微调器中。 这是我的代码:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.toptoche.searchablespinnerlibrary.SearchableSpinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class FicheHypothese1 extends Fragment {
SearchableSpinner spinner;
String Url="http://10.0.2.1/ODBC_Connect_INDUS/GET_CLIENTS.php";
public static List<String> lst=null;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new GetData().execute();
spinner= (SearchableSpinner) findViewById(R.id.spinner);
spinner.setTitle("Countries");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
}
class GetData extends AsyncTask<String,String,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
@Override
protected String doInBackground(String... params) {
lst=new ArrayList<String>();
JsonArrayRequest movieReq = new JsonArrayRequest(Url,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
for(int i=0;i<response.length();i++){
try {
//Getting json object
JSONObject json = response.getJSONObject(i);
//Adding the name of the student to array list
lst.add(json.getString("CT_Intitule"));
} catch (JSONException e) {
e.printStackTrace();
}
}
ArrayAdapter adapter=new
ArrayAdapter(getBaseContext(),
R.layout.support_simple_spinner_dropdown_item, lst);
spinner.setAdapter(adapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue =
Volley.newRequestQueue(getApplicationContext());
//Adding request to the queue
requestQueue.add(movieReq);
return null;
}
}
} 这是我的布局:
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
我的应用已停止运行 这是我的退出:
04/12 17:48:22: Launching sample
No apk changes detected since last installation, skipping installation of C:\stage2018\SearchableSpinner-master\sample\build\outputs\apk\sample-debug.apk
$ adb shell am force-stop com.toptoche.sample
$ adb shell am start -n "com.toptoche.sample/com.toptoche.sample.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 10471 on device Nexus_6_API_27 [emulator-5554]
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/Ads: Required XML attribute "adSize" was missing.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.toptoche.sample, PID: 10471
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.toptoche.sample/com.toptoche.sample.MainActivity}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
at com.google.android.gms.ads.internal.client.zzab.zzdg(Unknown Source:16)
at com.google.android.gms.ads.internal.client.zzab.zza(Unknown Source:4)
at com.google.android.gms.ads.BaseAdView.loadAd(Unknown Source:6)
at com.google.android.gms.ads.AdView.loadAd(Unknown Source:0)
at com.toptoche.sample.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
Application terminated.