0

I want to create newsfeed inside my app which takes a data from MySQL. For this purpose, I use LayoutInflate and FOR loop.

If I write lines (specified below bold) inside onCreateView the app does correctly but a screen display only one last news (cotitle, time and section). In case if inside FOR loop in method newsFeedDisplay() app doesn't and indicates error NPE.

LayoutInflater infl_news = getLayoutInflater();

final LinearLayout linearLayout = (LinearLayout)getActivity().findViewById(R.id.linear_newsfeed);

final View view1 = infl_news.inflate(R.layout.newsfront, linearLayout, false);

I checked via Log and saw that a data entering from DB is correctly (DB has four news).

What must I do that the screen displays a right number of news? How correctly to make that to display multiple LayoutInflater in sequence (one under another)?

Below code which bold lines writing inside FOR loop.

NewsFeed.java

public class NewsFeed extends Fragment {

public NewsFeed() {
}

TextView newsTime;
TextView newsTitle;
TextView newsSection;

String id_news;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rv = inflater.inflate(R.layout.newsfeed, container, false);

    newsTime = (TextView)rv.findViewById(R.id.newsTime);
    newsTitle = (TextView)rv.findViewById(R.id.newsTitle);
    newsSection = (TextView)rv.findViewById(R.id.newsSection);
/*        Typeface GothamProFont = 
Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamPro.ttf");
        newsTime.setTypeface(GothamProFont);
        newsTitle.setTypeface(GothamProFont);
        newsSection.setTypeface(GothamProFont);*/

    newsFeedDisplay();

/*        view1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), NewsBody.class);
            intent.putExtra("id_news", id_news);
            startActivity(intent);
        }
    });*/
    return rv;
}

void newsFeedDisplay () {

    LayoutInflater infl_news = getLayoutInflater();
    final LinearLayout linearLayout = (LinearLayout)getActivity().findViewById(R.id.linear_newsfeed);
    final View view1 = infl_news.inflate(R.layout.newsfront, linearLayout, false);

    Response.Listener<String> responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                JSONArray jsonMainNode = jsonResponse.optJSONArray("news");
                    Log.i(C.T, String.valueOf(jsonMainNode.length()));

                HashMap<String,String> map;

                for(int i=0; i<jsonMainNode.length(); i++){
                    JSONObject c = jsonMainNode.getJSONObject(i);

                    id_news = c.getString("news_id");
                    String time = c.getString("time");
                    String title = c.getString("title");
                    String section = c.getString("section");
                    Log.i (C.T, id_news);

                    String time1 = new SimpleDateFormat("HH:mm").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));

                    newsTime.setText(time1);
                    newsTitle.setText(title);
                    newsSection.setText(section);

                    linearLayout.addView(view1);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    };
    NewsFeedRequest3 newsFeedRequest3 = new NewsFeedRequest3(responseListener);
    RequestQueue queue = Volley.newRequestQueue(getContext());
    queue.add(newsFeedRequest3);
}
}

newsfeed.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_newsfeed"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="blazhko.sportarena.main_screen.NewsFeed">

</LinearLayout>

NewsFeedRequest3.java

public class NewsFeedRequest3 extends StringRequest{

private static final String REGISTER_REQUEST_URL = "https://blazhko.000webhostapp.com/test.php";
private Map<String, String> params;

public NewsFeedRequest3(Response.Listener<String> listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
      params = new HashMap<>();
      //params.put("section", section);
}

@Override
public Map<String, String> getParams() {
    return params;
}
}

newsfront.xml (I draw face of a news for newsfeed here)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/newsTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/exmpTime"
        android:textSize="14sp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingTop="5dp" />

    <TextView
        android:id="@+id/newsTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="3dp"
        android:paddingStart="10dp"
        android:layout_below="@id/newsTime"
        android:textColor="@color/colorTextTitle"
        android:lineSpacingMultiplier="1.3"
        android:text="@string/exmpTitle"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/newsSection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/newsTime"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="17dp"
        android:layout_marginEnd="13dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingTop="5dp"
        android:text="@string/exmpSection"
        android:textColor="@color/colorWarning"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/linebelownews"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/newsTitle"
        android:paddingBottom="10dp"
        android:layout_marginEnd="13dp"
        android:background="@color/colorAccent"/>
</RelativeLayout>

Error

*FATAL EXCEPTION: main
Process: blazhko.yevgen, PID: 18136
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at blazhko.sportarena.main_screen.NewsFeed$1.onResponse(NewsFeed.java:99)
at blazhko.sportarena.main_screen.NewsFeed$1.onResponse(NewsFeed.java:78)*

Please help me.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Yevgen
  • 93
  • 1
  • 1
  • 8

0 Answers0