简体   繁体   中英

Cannot call RecycleView from other activity

My RecycleView is in my Activity and then I want to set it in my other class. But it always say NullObjectReference with my RecycleView. I dont know why because i have initialize it. Is there something wrong? Here is my code

My Activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

RecyclerView recyclerView;
VolleyGet volleyGet;

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

    initRecycle();
    volleyGet = new VolleyGet();
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    
    Button get = (Button) findViewById(R.id.get);
    get.setOnClickListener(this);

}

public void initRecycle(){
    recyclerView = findViewById(R.id.recycleview);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {

        case R.id.get:
            volleyGet.volleyRequest(this);
            break;
            
        default:
            break;
    }
}

My Other Class (RecycleView must be set here)

public class VolleyGet {
RecyclerView recyclerView;
public RecycleAdapter adapter;
public ArrayList<User> listUser;

public void volleyRequest(final Context context) {
    MainActivity mainActivity = new MainActivity();
    mainActivity.initRecycle();
    listUser = new ArrayList<>();
    adapter = new RecycleAdapter(context, listUser);
    
    String URL = "http://192.168.180.7:8080/get.php";
    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray array = new JSONArray(response);
                        for (int i = 0; i < array.length(); i++) {
                            ArrayList<User> items = new Gson().fromJson(response.toString(), new TypeToken<ArrayList<User>>() {
                            }.getType());
                            listUser.clear();
                            listUser.addAll(items);
                        }
                        recyclerView.setAdapter(adapter);
                        
                    } catch (JSONException e) { e.printStackTrace(); } }},
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) { }}) {};
    queue.add(stringRequest);
}

Looks like the RecycleView init with calling a method. You can try initialize with other way like using Activity like this.

Activity activity;

and this how to get your activity

public VolleyGet(Activity _activity){
    this.activity = _activity;
    recyclerView = (RecyclerView) this.activity.findViewById(R.id.recycleview);
}

or you can do this if you want to use Context

public VolleyGet(Context context){
    recyclerView = (RecyclerView) ((Activity)context).findViewById(R.id.recycleview);
}

dont forget to add this in your MainActivity for sending your activity

volleyGet = new VolleyGet(this);

so the whole code of your other class must be like this

public class VolleyGet {
RecyclerView recyclerView;
public RecycleAdapter adapter;
public ArrayList<User> listUser;
Activity activity;

public VolleyGet(Activity _activity){
    this.activity = _activity;
    recyclerView = (RecyclerView) this.activity.findViewById(R.id.recycleview);
}

public void volleyRequest(final Context context) {
    listUser = new ArrayList<>();
    adapter = new RecycleAdapter(context, listUser);

    String URL = "http://192.168.180.7:8080/get.php";
    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray array = new JSONArray(response);
                        for (int i = 0; i < array.length(); i++) {
                            ArrayList<User> items = new Gson().fromJson(response.toString(), new TypeToken<ArrayList<User>>() {
                            }.getType());
                            listUser.clear();
                            listUser.addAll(items);
                        }
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) { e.printStackTrace(); } }},
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) { }}) {};
queue.add(stringRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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