简体   繁体   English

我的 Volley 应用程序崩溃,可能在 listView 代码上

[英]My Volley app crashes, perhaps on the listView code

Volley is working, when I close the listData there are no crash and working good, but without listView: Volley 正在工作,当我关闭 listData 时,没有崩溃并且工作正常,但没有 listView:

package com.example.tronksinc4d;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.PicassoProvider;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Home extends AppCompatActivity {

       public static prefconfig prefcoonfig;
       private TextView helloo;
       RequestQueue requestQueue;
       String url = 
"https://tronksinc4d.000webhostapp.com/show_all_movies.php";
       ArrayList<listitem> listmovies = new ArrayList<listitem>();
       ListView listView;
       TextView text_total, try_data;

      @Override
   `enter code here`   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        prefcoonfig = new prefconfig(this);
        helloo = (TextView) findViewById(R.id.hello);
        helloo.setText(" أهلا بك " + prefcoonfig.readname());


        listView = (ListView) findViewById(R.id.dynmic);
        text_total = (TextView) findViewById(R.id.Tota_text);
        try_data = (TextView) findViewById(R.id.try_data);
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new 
    JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            JSONArray jsonArray = 
    response.getJSONArray("allmovies");
                            text_total.setText("مجموع الدروس : " + 
    jsonArray.length());
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject respons = 
 jsonArray.getJSONObject(i);
                                String id = respons.getString("id");
                                String img = respons.getString("photolink");
                                String Title = respons.getString("Title");
                                String info = respons.getString("info");
                                String link = respons.getString("link");
                                listmovies.add(new listitem(id, img, Title, 
    info, link));
                                try_data.setText("The TItle Is = " + 
    respons.getString("Title"));


                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        ListData();

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", "ERROR");
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

List data :列出数据

    public void ListData() {
        ListAdapter ad = new ListAdapter(listmovies);
        listView.setAdapter(ad);
    }
// class list Adapter

    class ListAdapter extends BaseAdapter {
        ArrayList<listitem> lisitem = new ArrayList<listitem>();

        ListAdapter(ArrayList<listitem> listitem) {
            this.lisitem = listitem;
        }

        @Override
        public int getCount() {
            return lisitem.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return lisitem.get(position).Title;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup parent) {

            LayoutInflater layoutInflater = getLayoutInflater();
            View view = layoutInflater.inflate(R.layout.row_item, null);

            TextView Title = (TextView) findViewById(R.id.text_view_title);
            TextView info = (TextView) findViewById(R.id.text_view_info);
            ImageView img = (ImageView) findViewById(R.id.row_img);

            Title.setText(lisitem.get(i).Title);
            info.setText(lisitem.get(i).info);

            Picasso.get().load("https://tronksinc4d.000webhostapp.com/" + 
lisitem.get(i).img).into(img);
            return view;
        }
    }


    //close the class

    public void performLogout() {
        prefcoonfig.writeloginstatus(false);
        prefcoonfig.writename("user");
        Intent intent = new Intent(Home.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

The code not working and the app crash, I think it from ListView.代码不起作用并且应用程序崩溃,我认为它来自 ListView。 This is the full code.这是完整的代码。 The listitem.java is already exists. listitem.java 已经存在。

The code working without listData, so I think the problem is in ListData.代码在没有 listData 的情况下工作,所以我认为问题出在 ListData 中。

Error:错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.tronksinc4d, PID: 447
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.tronksinc4d.Home$ListAdapter.getView(Home.java:137)
        at android.widget.AbsListView.obtainView(AbsListView.java:2365)
        at android.widget.ListView.makeAndAddView(ListView.java:2052)
        at android.widget.ListView.fillDown(ListView.java:786)
        at android.widget.ListView.fillFromTop(ListView.java:847)
        at android.widget.ListView.layoutChildren(ListView.java:1826)
        at android.widget.AbsListView.onLayout(AbsListView.java:2164)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1780)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1546)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:763)
        at android.view.View.layout(View.java:20322)
        at android.view.ViewGroup.layout(ViewGroup.java:6256)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2609)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2314)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1450)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7022)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:924)
        at android.view.Choreographer.doCallbacks(Choreographer.java:732)
        at android.view.Choreographer.doFrame(Choreographer.java:664)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:910)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6524)
        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:888)

As it is clear from the error message attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference , textview is not initialized properly in getView . As it is clear from the error message attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference , textview is not initialized properly in getView . Correct way for this is正确的方法是

        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.row_item, null);

        TextView Title = (TextView) view.findViewById(R.id.text_view_title);
        TextView info = (TextView) view.findViewById(R.id.text_view_info);
        ImageView img = (ImageView) view.findViewById(R.id.row_img);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM