我想在我的应用程序中创建新闻源,它从MySQL获取数据。为此,我使用LayoutInflate
和FOR循环。
如果我在onCreateView
内写下行(在粗体下面指定)该应用程序正确,但屏幕只显示最后一条新闻(cotitle,time和section)。如果方法newsFeedDisplay()
app中的FOR循环内部没有并且指示错误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);
我通过Log检查并看到从DB输入的数据是正确的(DB有四条消息)。
我该怎么做才能让屏幕显示正确数量的新闻?如何正确地按顺序显示多个LayoutInflater
(一个在另一个下)?
在FOR循环内写下粗线的代码。
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 (我在这里为新闻提要面对新闻)
<?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>
错误
*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)*
请帮帮我。