0
votes

I am trying to fetch an array list using volley and display it in a spinner but my app crashes. here is the main activity of the app

package com.example.shivadeeps.webservice;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Config;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import org.json.JSONObject;

import java.util.ArrayList;
import static java.lang.Character.getName;

public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private ArrayList<String> trips;
private JSONArray result;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    trips= new ArrayList<String>();
    this.spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
    loadtrip();
}
public void loadtrip() {
    StringRequest stringRequest = new StringRequest(config.DATA_URL,
            new Response.Listener<String>() {
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        j = new JSONObject(response);
                        result = j.getJSONArray(config.JSON_ARRAY);
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
private void getStudents(JSONArray j){
    for(int i=0;i<j.length();i++){
        try {
            JSONObject json = j.getJSONObject(i);
            trips.add(json.getString(config.DATA_URL));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner
    spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,trips));
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getItemAtPosition(position).toString();
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {


}


}

XML code:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.shivadeeps.webservice.MainActivity">
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="23" />

<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="80dp"

    android:layout_marginTop="100dp" ></Spinner>
<TextView
    android:id="@+id/text_id"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:textColor="@android:color/holo_blue_dark"
    android:textColorHighlight="@android:color/primary_text_dark"
    android:layout_centerVertical="true"
    android:textSize="50dp"/>
   </RelativeLayout>

here is the logcat

01-10 11:29:25.226 12404-12404/com.example.shivadeeps.webservice
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.shivadeeps.webservice, PID: 12404 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shivadeeps.webservice/com.example.shivadeeps.webservice.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class uses-permission at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3190) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300) at android.app.ActivityThread.access$1000(ActivityThread.java:211) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6946) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class uses-permission at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) at android.view.LayoutInflater.inflate(LayoutInflater.java:511) at android.view.LayoutInflater.inflate(LayoutInflater.java:415) at android.view.LayoutInflater.inflate(LayoutInflater.java:366) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) at com.example.shivadeeps.webservice.MainActivity.onCreate(MainActivity.java:40) at android.app.Activity.performCreate(Activity.java:6575) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)  at android.app.ActivityThread.access$1000(ActivityThread.java:211)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:6946)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)  Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.uses-permission" on path: DexPathList[[zip file "/data/app/com.example.shivadeeps.webservice-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at android.view.LayoutInflater.createView(LayoutInflater.java:578) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:65) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)  at android.view.LayoutInflater.inflate(LayoutInflater.java:511)  at android.view.LayoutInflater.inflate(LayoutInflater.java:415)  at android.view.LayoutInflater.inflate(LayoutInflater.java:366)  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)  at com.example.shivadeeps.webservice.MainActivity.onCreate(MainActivity.java:40)  at android.app.Activity.performCreate(Activity.java:6575)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)  at android.app.ActivityThread.access$1000(ActivityThread.java:211)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:6946)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)  Suppressed: java.lang.ClassNotFoundException: Didn't find class "android.view.uses-permission" on path: DexPathList[[dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-support-annotations-25.0.1_485a49a20ae18735d596aaa75d964eacffc01e9a-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.shivadeeps.webservice/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.exa 01-10 11:29:25.231 2827-3392/? W/ActivityManager: Force finishing activity 1 com.example.shivadeeps.webservice/.MainActivity 01-10 11:29:25.241 2827-3392/? D/InputDispatcher: Focused application set to: xxxx 01-10 11:29:25.311 12691-12691/? D/Mms/MmsApp: [start] initCountryIso consume time = 103.6345 01-10 11:29:25.316 2827-3621/? D/CountryDetector: The first listener is added 01-10 11:29:25.321 12691-12691/? D/Mms/MmsApp: [end] initCountryIso consume time = 9.456961 01-10 11:29:25.416 2156-11978/? I/display: [PrimaryDisplay] [DYNAMIC_RECOMP] GLES_2_HWC by BW check 01-10 11:29:25.436 11562-11587/? I/Icing: Indexing 7491238841BD1692167B469A759AF4A2F8E28FA8 from com.google.android.googlequicksearchbox 01-10 11:29:25.486 2827-3114/? E/Watchdog: !@Sync 5200 [01-10 11:29:25.488] 01-10 11:29:25.531 12691-12691/? D/Mms/MmsConfig: [start] MmsConfig.init() consume time = 210.034154 01-10 11:29:25.546 2827-3392/? I/WindowManager: Screenshot max retries 4 of Token{1ad8e437 ActivityRecord{3b472d36 u0 com.example.shivadeeps.webservice/.MainActivity t440 f}} appWin=Window{2e5a232f u0 d0 Starting com.example.shivadeeps.webservice} drawState=4 01-10 11:29:25.551 12691-12691/? D/EasySignUpManager_1.15.0305: isAuth is false 01-10 11:29:25.551 2827-12720/? E/android.os.Debug: ro.product_ship = true 01-10 11:29:25.551 2827-12720/? E/android.os.Debug: ro.debug_level = 0x4f4c 01-10 11:29:25.556 12691-12691/? D/Mms/MmsConfig: getEasySignUpStatus sIsAuthEasySignUp=false, TelephonyUtils.getSimState(0)= 1 01-10 11:29:25.556 2827-2852/? D/PhoneWindow: FMB installDecor mIsFloating : true 01-10 11:29:25.556 2827-2852/? D/PhoneWindow: FMB installDecor flags : 8519682 01-10 11:29:25.556 12691-12691/? D/EasySignUpManager_1.15.0305: serviceId : 1, features : -1 01-10 11:29:25.556 12691-12691/? D/EasySignUpManager_1.15.0305: isAuth is false 01-10 11:29:25.556 12691-12691/? D/EasySignUpManager_1.15.0305: getServiceStatus : serviceId (1) is OFF 01-10 11:29:25.556 12691-12691/? D/Mms/MmsConfig: setFreeMessageEnabled, getSupportedFeatures = -1 sIsAuthEasySignUp = false sIsFreeMessageServiceStatus = false 01-10 11:29:25.556 12691-12691/? D/Mms/MmsConfig: Load Resize quality : 80 01-10 11:29:25.561 12691-12691/? E/CscParser: mps_code.dat does not exist 01-10 11:29:25.566 12691-12691/? E/CscParser: customer_path =/system/csc/customer.xml 01-10 11:29:25.566 12691-12691/? E/CscParser: fileName + /system/csc/customer.xml

2
error in your xml code. post it hereDivyesh Patel
Problem is in XML fileρяσѕρєя K
Remove that permission form xml and add it in manifest...Whats Going On

2 Answers

1
votes

Please remove this from xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="23" />

and add them to Manifest file. This will solve your problem. Permissions are always added in Manifest file not in XML file.

0
votes

check this line in your code spinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);

Your setting click listener as "this", but your activity did not implement "AdapterView.OnItemSelectedListener"

Your activity should implement AdapterView.OnItemSelectedListener