Java 从适配器替换时找不到片段id的视图
我正在尝试用另一个片段替换我当前的片段。我在适配器上做这件事,因为我想在点击一张卡时做。这是我在适配器中的代码:Java 从适配器替换时找不到片段id的视图,java,android,fragment,frame,Java,Android,Fragment,Frame,我正在尝试用另一个片段替换我当前的片段。我在适配器上做这件事,因为我想在点击一张卡时做。这是我在适配器中的代码: itemView.setOnClickListener(v -> { if (mMovieCategory.getImageUrl() != null) { try { //TODO FragmentTransaction ft = ((App
itemView.setOnClickListener(v -> {
if (mMovieCategory.getImageUrl() != null) {
try {
//TODO
FragmentTransaction ft = ((AppCompatActivity) v.getContext()).getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.movie_container, new Fragment_Movie());
ft.addToBackStack(null);
ft.commit();
} catch (Exception e) {
Log.e(TAG, "onClick: Image url is not correct");
}
}
});
在replace函数中,我使用了分配给要调用的片段的id。这是XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/movie_container"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector"/>
</RelativeLayout>
这是我要调用的片段:
package com.lab.movietime;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import static com.android.volley.VolleyLog.TAG;
public class Fragment_Movie extends Fragment {
private static final String url = "https://api.androidhive.info/json/movies.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
public Fragment_Movie() {
// Required empty public constructor
}
public static Fragment_Movie newInstance() {
Fragment_Movie fragment = new Fragment_Movie();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_movie, container, false);
listView = (ListView) view.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getContext());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
AppController.getInstance().addToRequestQueue(movieReq);
return view;
}
}
package com.lab.movietime;
导入android.os.Bundle;
导入androidx.fragment.app.fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入java.util.ArrayList;
导入java.util.List;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.graphics.Color;
导入android.graphics.drawable.ColorDrawable;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.widget.ListView;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.VolleyLog;
导入com.android.volley.toolbox.JsonArrayRequest;
导入静态com.android.volley.VolleyLog.TAG;
公共类Fragment\u电影扩展了Fragment{
私有静态最终字符串url=”https://api.androidhive.info/json/movies.json";
私人对话;
private List movieList=new ArrayList();
私有列表视图列表视图;
专用自定义列表适配器;
公共电影(){
//必需的空公共构造函数
}
公共静态片段_Movie newInstance(){
Fragment_Movie Fragment=新片段_Movie();
Bundle args=新Bundle();
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
}
}
@凌驾
公共空间{
super.ondestory();
hidePDialog();
}
私有void hidePDialog(){
如果(pDialog!=null){
pDialog.disclose();
pDialog=null;
}
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u电影,容器,假);
listView=(listView)view.findViewById(R.id.list);
adapter=新的CustomListAdapter(getActivity(),movieList);
setAdapter(适配器);
pDialog=newprogressdialog(getContext());
//在发出http请求之前显示进度对话框
设置消息(“加载…”);
pDialog.show();
//创建截击请求对象
JsonArrayRequest movieReq=新的JsonArrayRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
Log.d(TAG,response.toString());
hidePDialog();
//解析json
对于(int i=0;i
我得到的错误是:
找不到片段电影{1f8531a}的id 0x7f0900ab(com.lab.movietime:id/movie_容器)的视图
为什么??id:容器\电影是否不正确?假设
电影\容器
位于片段
内:您需要在活动
的布局中传递容器视图的id,而不是片段
视图的id
这将是包含
片段的视图
的ID,与片段
中包含的视图
的ID相反。共享您的活动布局您是否尝试过从适配器回调到片段类?