1

ich Anfänger sind in Android Studio,RecyclerView innerhalb Fragmen gibt es keinen Fehler mit meinem Code

ich mit RecyclerView irgendein Problem habe innerhalb Fragment.I nicht in der Lage ist, den Fehler in meinem code.When ich meine app laufen zu finden es läuft, aber wenn ich bestimmte Fragment öffnen zeigt es click me for screen shot,

meinen Code hinzufügen ist als

ListFragment.java

public class ListFragment extends Fragment { 
//this is the JSON Data URL 
//make sure you are using the correct ip else it will not work 
final String URL_PRODUCTS = "http://192.168.0.12/Api.php"; 

//a list to store all the products 
List<Person> personList; 

//the recyclerview 
RecyclerView recyclerView; 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Toast.makeText(MainActivity.appContext, "Created", Toast.LENGTH_SHORT).show(); 
} 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_list, container, false); 

    Toast.makeText(MainActivity.appContext, "Created in View", Toast.LENGTH_SHORT).show(); 
    //getting the recyclerview from xml 
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recylcerView); 



    //initializing the productlist 
    personList = new ArrayList<>(); 

    //this method will fetch and parse json 
    //to display it in recyclerview 
    loadProducts(); 
    return rootView; 

} 

private void loadProducts() { 

    /* 
    * Creating a String Request 
    * The request type is GET defined by first parameter 
    * The URL is defined in the second parameter 
    * Then we have a Response Listener and a Error Listener 
    * In response listener we will get the JSON response as a String 
    * */ 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_PRODUCTS, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         //converting the string to json array object 
         JSONArray array = new JSONArray(response); 

         //traversing through all the object 
         for (int i = 0; i < array.length(); i++) { 

          //getting product object from json array 
          JSONObject product = array.getJSONObject(i); 

          //adding the product to product list 
          personList.add(new Person(
            product.getInt("id"), 
            product.getString("title"), 
            product.getString("shortdesc"), 
            product.getDouble("rating"), 
            product.getDouble("price"), 
            product.getString("image") 
          )); 
         } 

         //creating adapter object and setting it to recyclerview 
         PersonsAdapter adapter = new PersonsAdapter(MainActivity.appContext, personList); 
         recyclerView.setAdapter(adapter); 
         recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.appContext)); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 

    //adding our stringrequest to queue 

    Volley.newRequestQueue(MainActivity.appContext).add(stringRequest); 


} 


} 
folgt

der folgende Code, den ich verwenden, um das Fragment das das sollte die Fragmente seine zweite Parameter

fragmentManager.beginTransaction().replace(R.id.frame_container, new MyListFragment(), "MyListFragment").commit(); 

-Code

fragmentManager.beginTransaction().replace(R.id.frame_container, new ListFragment(), "ListFragment").commit(); 
+0

versuchen, Debugging. Wir können nicht debuggen auf Ihrem Code ohne den Server –

+0

Wie kann ich tun? @ Tomin B –

+0

Add Beakpoint und Debug-Anwendung –

Antwort

1

ändern zu öffnen.

Der 3. Paramter ist der TAG, mit dem das Fragment identifiziert wird.

Der erste Parameter ist der FragmeLayout oder Container in der Aktivität, in der das Fragment aufgebläht werden soll.

Sie können nicht verwenden Name Liste Fragment Da Itz ein Standardfragment von Android Itself. Manchmal, wenn Sie ListFragment anrufen diese Funktion aufgerufen werden kann

+0

Ja, ich verwende diesen Code nur, um das Fragment zu öffnen. –

+0

Ich habe dich nicht bekommen. Wenn Sie ListFragment nicht öffnen, wie erhalten Sie ListFragment in Info-Fragment –

+0

bearbeitet meine Frage. –

Verwandte Themen