[英]Cannot call RecycleView from other activity
My RecycleView is in my Activity and then I want to set it in my other class.我的 RecycleView 在我的 Activity 中,然后我想在我的其他 class 中设置它。 But it always say NullObjectReference with my RecycleView.
但它总是用我的 RecycleView 说NullObjectReference 。 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)我的其他 Class (此处必须设置 RecycleView)
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.看起来像调用方法的 RecycleView 初始化。 You can try initialize with other way like using Activity like this.
您可以尝试使用其他方式进行初始化,例如像这样使用 Activity。
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或者如果你想使用 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不要忘记将其添加到您的 MainActivity 以发送您的活动
volleyGet = new VolleyGet(this);
so the whole code of your other class must be like this所以你的其他 class的整个代码必须是这样的
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.