मुझे स्थानीयहोस्ट से जेसन डेटा को खोजने योग्य स्पिनर को खिलाने की ज़रूरत है। डेटा ठीक से आ रहा है, लेकिन एडॉप्टर त्रुटियों को फेंक रहा है।

    import com.toptoche.searchablespinnerlibrary.SearchableSpinner;

    public class SearchActivity extends AppCompatActivity {
        private static final String TAG = "SearchActivity";

        TextView nameTextView;
        TextView dosaTextView;

        SearchableSpinner nameSpinner;
        SearchableSpinner dosageSpinner;

        String[] name = new String[5000];


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

            nameSpinner = findViewById(R.id.nameSpinner);
            dosageSpinner = findViewById(R.id.dosageSpinner);

            final String nameApiLink = "http://192.168.10.50:80/xxx/xxx.php";



            class FetchJsonData extends AsyncTask<Void, Void, Void> {

                @Override
                protected Void doInBackground(Void... voids) {

                    try {
                        URL url = new URL(nameApiLink);
                        HttpURLConnection con = (HttpURLConnection) url.openConnection();
                        con.setConnectTimeout(5000);
                        //ArrayList<String> sb = new ArrayList<>();
                        StringBuilder sb = new StringBuilder();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        String jsonString;
                        while ((jsonString = bufferedReader.readLine()) != null) {
                            //sb.add(jsonString);
                            sb.append(jsonString);
                        }
                        JSONArray jsonArray = new JSONArray(sb.toString());


                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonName = jsonArray.getJSONObject(i);


                            name[i] = jsonName.getString("PRODUCT_COMPOUND_NAME");
                        }


                        //return sb;
                    } catch (Exception e) {
                        e.getCause();

                    }
                    return null;
                }
            }
            FetchJsonData fetchJsonDataForName = new FetchJsonData();
            fetchJsonDataForName.execute();


            ArrayAdapter spinnerAdapterForName = new ArrayAdapter<>(SearchActivity.this, android.R.layout.simple_spinner_dropdown_item, name);
            nameSpinner.setAdapter(spinnerAdapterForName);
        }


    }

डेटा कुछ इस तरह दिखता है।

[{"PRODUCT_COMPOUND_CODE":"000001","PRODUCT_COMPOUND_NAME":"---"},{"PRODUCT_COMPOUND_CODE":"1","PRODUCT_COMPOUND_NAME":"LUTEINIZING HORMONE"},{"PRODUCT_COMPOUND_CODE":"100","PRODUCT_COMPOUND_NAME":"EMBLICA OFFICINALIS           "},{"PRODUCT_COMPOUND_CODE":"1000","PRODUCT_COMPOUND_NAME":"ISBUFYLLINE                   "},{"PRODUCT_COMPOUND_CODE":"1002","PRODUCT_COMPOUND_NAME":"LEVEMOPAMIL                   "},{"PRODUCT_COMPOUND_CODE":"1003","PRODUCT_COMPOUND_NAME":"LEVETIRACETAM                 "}

जब मैं कोड चलाता हूं, तो मुझे निम्न त्रुटि मिलती है।

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.kalashtech.DDA, PID: 30787
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:453)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:415)
        at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:204)
        at android.widget.Spinner.onMeasure(Spinner.java:602)
        at android.view.View.measure(View.java:23196)
        at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
        at android.view.View.measure(View.java:23196)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:23196)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23196)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23196)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23196)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:720)
        at android.view.View.measure(View.java:23196)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2814)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1662)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1946)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1550)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7437)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1089)
        at android.view.Choreographer.doCallbacks(Choreographer.java:885)
        at android.view.Choreographer.doFrame(Choreographer.java:816)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1075)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6821)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

कृपया मदद कीजिए। मैं इस पर नया हूं इसलिए अगर सवाल में कुछ गलत है तो क्षमा करें।

2
Samuel Maharjan 14 मार्च 2020, 10:46

1 उत्तर

सबसे बढ़िया उत्तर

आपको एक ArrayList की आवश्यकता होगी और इस तरह मेमोरी को इनिशियलाइज़ और आवंटित करना होगा, जहां AsyncTask में उनके कुछ callback methods.

final ArrayList<String> arrayList = new ArrayList<>();

final ArrayList<String> arrayList = new ArrayList<>();
        class FetchJsonData extends AsyncTask<Void, Void, ArrayList<String>> {

            @Override
            protected ArrayList<String> doInBackground(Void... voids) {

                try {
                    URL url = new URL(nameApiLink);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setConnectTimeout(5000);
                    //ArrayList<String> sb = new ArrayList<>();
                    StringBuilder sb = new StringBuilder();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String jsonString;
                    while ((jsonString = bufferedReader.readLine()) != null) {
                        //sb.add(jsonString);
                        sb.append(jsonString);
                    }
                    JSONArray jsonArray = new JSONArray(sb.toString());
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonName = jsonArray.getJSONObject(i);
                        arrayList.add(jsonName.getString("PRODUCT_COMPOUND_NAME"));
                    }

                    return arrayList;
                    //return sb;
                } catch (Exception e) {
                    e.getCause();

                }
                return null;
            }

            @Override
            protected void onPostExecute(ArrayList<String> stringList) {
                super.onPostExecute(stringList);

                if(stringList!=null && stringList.size()>0)
                {
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, stringList );
                    nameSpinner.setAdapter(adapter);

                    nameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                            String product_name = (String) parent.getSelectedItem();
                            Toast.makeText(view.getContext(), "PRODUCT_NAME--"+product_name , Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                        }
                    });
                }
            }
        }

        FetchJsonData fetchJsonDataForName = new FetchJsonData();
        fetchJsonDataForName.execute();

आप dataclass बना सकते हैं और फिर ArrayAdapter के साथ उपयोग कर सकते हैं जैसे यह

2
ॐ Rakesh Kumar 14 मार्च 2020, 08:13