gpt4 book ai didi

android - 无法移动到 recyclerview 中的另一个 fragment

转载 作者:行者123 更新时间:2023-11-29 15:03:00 24 4
gpt4 key购买 nike

我正在开发一个应用程序。我想在 friend fragment 中列出该项目,当我单击该项目然后转到消息 fragment (另一个 fragment )。我的代码如下。

friend fragment .java

public class FriendsFragment extends android.support.v4.app.Fragment {

CardView cardView;
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;

String GET_JSON_DATA_HTTP_URL = "http://192.168.43.7/work/ecom/2/1.php";
String JSON_IMAGE_TITLE_NAME = "image_title";
String JSON_IMAGE_URL = "image_url";

JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
public FriendsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_friends, container, false);
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview1);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(recyclerViewlayoutManager);



cardView = (CardView) rootView.findViewById(R.id.cardview1);


cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Log.d("LOGTAG", "clicked");
MessagesFragment fragment = new MessagesFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container_body, fragment).commit();


}
});


JSON_DATA_WEB_CALL();



// Inflate the layout for this fragment
return rootView;
}

public void JSON_DATA_WEB_CALL(){

jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {

Log.d("LOGTAG", "Response :"+response);

JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});

requestQueue = Volley.newRequestQueue(getContext());

requestQueue.add(jsonArrayRequest);
}


public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

for(int i = 0; i<array.length(); i++) {

GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

JSONObject json = null;
try {

json = array.getJSONObject(i);

GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));

GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));

} catch (JSONException e) {

e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}

recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, getContext());

recyclerView.setAdapter(recyclerViewadapter);

}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}

@Override
public void onDetach() {
super.onDetach();
}
}

当我添加

  cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Log.d("LOGTAG", "clicked");
/* MessagesFragment fragment = new MessagesFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container_body, fragment).commit();*/


}
});

显示错误

FATAL EXCEPTION: main
Process: com.ecom.fragment2, PID: 21541
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.ecom.fragment2.FriendsFragment.onCreateView(FriendsFragment.java:77)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

fragment_friends.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:orientation="vertical"
tools:context="com.ecom.fragment2.FriendsFragment">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</RelativeLayout>

recyclerview_items.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="3dp"
card_view:contentPadding="3dp"
card_view:cardCornerRadius="3dp"
card_view:cardMaxElevation="3dp"
>


<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<com.android.volley.toolbox.NetworkImageView
android:id="@+id/VollyNetworkImageView1"
android:layout_width="150dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Image Name"
android:id="@+id/textView_item"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/VollyNetworkImageView1"
android:layout_toEndOf="@+id/VollyNetworkImageView1"
android:layout_marginLeft="20dp"/>

</RelativeLayout>

</android.support.v7.widget.CardView>

请帮帮我

最佳答案

问题来自

cardView = (CardView) rootView.findViewById(R.id.cardview1);

行。

因为 CardViewR.id.cardview1 id 位于 RecyclerView 的行布局内,但使用 rootView.findViewById 试图从错误的布局访问它,即 fragment_friends

要解决此问题,请将 setOnClickListener 添加到 RecyclerViewAdapter 类的 onBindViewHolder 方法中的 CardView

关于android - 无法移动到 recyclerview 中的另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41490446/

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