无法解析方法 'show(android.support.v4.app.FragmentTransaction, java.lang.String)'

Cannot resolve method 'show(android.support.v4.app.FragmentTransaction, java.lang.String)'

我正在尝试使用视图寻呼机创建图片库。但是我收到一条错误消息,说 无法为 newFragment.show(ft, "slideshow"); 解析方法 'show(android.support.v4.app.FragmentTransaction, java.lang.String)' 我已经阅读了与 Fragment Manager 相关的类似问题,但它们对我帮助不大新手和我使用的是 FragmentTransaction 而不是 FragmentManager。谁能帮我修好它?

import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

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

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();
    private static final String endpoint = "http://demos.vetbossel.in/ajson/image.json";
    private ArrayList<Image1> images;
    private ProgressDialog pDialog;
    private GalleryAdapter mAdapter;
    private RecyclerView recyclerView;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        pDialog = new ProgressDialog(this);
        images = new ArrayList<>();
        mAdapter = new GalleryAdapter(getApplicationContext(), images);

        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new GalleryAdapter.ClickListener() {
            @Override
            public void onClick(View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("images", images);
                bundle.putInt("position", position);

                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
                newFragment.setArguments(bundle);
                newFragment.show(ft,"tag");
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));

        fetchImages();
    }

    private void fetchImages() {

        pDialog.setMessage("Loading Images...");
        pDialog.show();

        JsonArrayRequest req = new JsonArrayRequest(endpoint,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        pDialog.hide();

                        images.clear();
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject object = response.getJSONObject(i);
                                Image1 image = new Image1();


                                JSONObject url = object.getJSONObject("url");
                                image.setSmall(url.getString("small"));
                                image.setMedium(url.getString("medium"));
                                image.setLarge(url.getString("large"));


                                images.add(image);

                            } catch (JSONException e) {
                                Log.e(TAG, "Json parsing error: " + e.getMessage());
                            }
                        }

                        mAdapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Error: " + error.getMessage());
                pDialog.hide();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
    }
}

这样试试;

  Fragment fragment = new SlideshowDialogFragment();
  FragmentManager fm =getActivity().getSupportFragmentManager();
  FragmentTransaction ft = fm.beginTransaction();
  ft.replace(R.id.content_frame, fragment);
  ft.commit();

  Bundle bundle = new Bundle();
  bundle.putSerializable("images", images);
  bundle.putInt("position", position);

 fragment.setArguments(bundle);

您 class 顶部的导入是什么?你应该导入 android.support.v4.app.Fragment;android.support.v4.app.FragmentTransaction; 像这样:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

实际上问题出在您的 SlideshowDialogFragment class 中。在 Android 中有两个 DialogFragment

  1. android.support.v4.app.DialogFragment
  2. android.app.DialogFragment

作为您的 SlideshowDialogFragment class 扩展第二个,您不能使用 android.support.v4.app.FragmentTransaction

解决方案是转到 SlideshowDialogFragment class 并将 import android.app.DialogFragment; 替换为 import android.support.v4.app.DialogFragment;。现在一切正常了。