无法在适配器中使用intent

时间:2018-03-20 06:01:55

标签: android android-intent android-activity adapter

在解雇@Database(entities = {PlaceSaved.class},version = 1) public abstract class PlaceDatabase extends RoomDatabase { private static PlaceDatabase INSTANCE; public static PlaceDatabase getDatabase(Context context){ if (INSTANCE == null){ INSTANCE = Room.databaseBuilder(context.getApplicationContext(), PlaceDatabase.class, "places_db").build(); } return INSTANCE; } public abstract DatabaseInterface PlacedatabaseInterface(); Intent活动时,我收到此错误

finish

此行导致错误

03-20 11:05:16.991 8566-8566/com.example.jenya1.ebilling E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.jenya1.ebilling, PID: 8566
                                                                       java.lang.ClassCastException: droidninja.filepicker.FilePickerDelegate cannot be cast to android.app.Activity
                                                                           at com.example.jenya1.ebilling.adapter.MaterialAdapter$1$1.onClick(MaterialAdapter.java:142)
                                                                           at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                           at android.os.Looper.loop(Looper.java:164)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

调用apapter类

((Activity)mContext).finish();    //error

当用户点击提示对话框是按钮

时,我正在尝试private void loadMaterialRequest() { final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); //getting the progressbar //making the progressbar visible progressBar.setVisibility(View.VISIBLE); albumList.clear(); //creating a string request to send request to the url StringRequest stringRequest = new StringRequest(Request.Method.GET, HttpUrl+token+"&type="+type, new Response.Listener<String>() { @Override public void onResponse(String response) { //hiding the progressbar after completion progressBar.setVisibility(View.INVISIBLE); try { //getting the whole json object from the response JSONObject obj = new JSONObject(response); JSONArray heroArray = obj.getJSONArray("response"); //now looping through all the elements of the json array for (int i = 0; i < heroArray.length(); i++) { //getting the json object of the particular index inside the array JSONObject heroObject = heroArray.getJSONObject(i); //creating a hero object and giving them the values from json object Material hero = new Material( heroObject.getInt("Request_id"), heroObject.getString("Site_name"), heroObject.getString("User_id"), heroObject.getString("Item_name"), heroObject.getString("Quantity"), heroObject.getString("Country"), heroObject.getString("Request_date"), heroObject.getString("Request_status")); //adding the hero to herolist albumList.add(hero); } //Log.e("list", String.valueOf(albumList)); //creating custom adapter object adapter = new MaterialAdapter(getApplicationContext(),albumList); //adding the adapter to listview recyclerView.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("error", String.valueOf(error)); } }); //creating a request queue RequestQueue requestQueue = Volley.newRequestQueue(this); //adding the string request to request queue requestQueue.add(stringRequest); }

这是我的适配器类:

Intent

任何帮助都将受到高度赞赏。

4 个答案:

答案 0 :(得分:3)

让我们看看:

((Activity)mContext).finish(); 

日志说mContext是一种FilePickerDelegate。所以检查你的Adapter init函数。我认为你将上下文作为“this”传递,但是“this”引用了FilePickerDelegate。因此将其更改为YourActivity.this(带活动)或getActivity(带片段)

答案 1 :(得分:1)

您正在传递应用程序上下文而不是适配器中的活动上下文。

getApplicationContext(); 无论你在哪里传递它都会传递这个或ActivityName.this。

因为你试图将你传递的上下文(应用程序不是你想象的活动)转换为带有

的活动

(活动) 你得到这个例外是因为你不能将应用程序转换为Activity,因为Application不是Activity的子类。

答案 2 :(得分:0)

而不是Context使用适配器中的Activity。由于Activity可以转换为上下文,但上下文不能转换为活动。

Activity avtivity;

public MaterialAdapter(Activity activity, List<Material> albumList) {
this.activity = activity;
this.albumList = albumList;
}

初始化适配器时的调用活动如下所示调用此适配器。

new MaterialAdapter(<your calling activity>.this, yourList);

答案 3 :(得分:0)

view.getContext().startActivity(new Intent(mContext, MaterialHistoryActivity.class));