gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:54:58 25 4
gpt4 key购买 nike

我正在尝试创建一个带有 View 寻呼机的图片库。但我收到一条错误消息,指出 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);
}
}

最佳答案

实际上问题出在你的 SlideshowDialogFragment 上类(class)。在Android中有两个DialogFragment

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

由于您的 SlideshowDialogFragment 类扩展了第二个类,因此您不能使用 android.support.v4.app.FragmentTransaction .

解决方案是转到SlideshowDialogFragment类和替换import android.app.DialogFragment;通过import android.support.v4.app.DialogFragment; 。现在一切都会正常。

关于java - 无法解析方法 'show(android.support.v4.app.FragmentTransaction, java.lang.String)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45793385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com