gpt4 book ai didi

java - 如何修复空 fragment ?

转载 作者:行者123 更新时间:2023-11-29 22:44:55 24 4
gpt4 key购买 nike

我是 Android 开发的新手,正在尝试创建自己的应用。它应该使用 YouTube 数据 API 显示特定的 YouTube channel 。我从 Android Studio 中的标准底部导航模板开始,并使用 Github 上的以下项目来获得一些启动帮助。 https://github.com/stressGC/Remake-YouTube-Android

我必须更改一些内容,例如代码中已弃用的 http 调用,以使其与新的 Android APK 一起运行。从我的角度来看,一切似乎都很好:我可以看到 API 内容看起来不错,并且每个标题/描述/发布日期都放在相应的变量中。日志中也没有错误消息。当我启动模拟器时,应用程序运行良好。但是一旦我切换到“仪表板” fragment (放置代码的地方),它就是空的。

DashboardFragment.java

public class DashboardFragment extends Fragment {
private static String API_KEY = "hidden"; //normaler API key ohne limits, kein oauth
private static String CHANNEL_ID = "hidden";
private static String CHANNEL_GET_URL = "https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&channelId="+CHANNEL_ID+"&maxResults=20&key="+API_KEY+"";

private RecyclerView mList_videos = null;
private VideoPostAdapter adapter = null;
private ArrayList<YouTubeDataModel> mListData = new ArrayList<>();

public DashboardFragment () {

}

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

View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
mList_videos = (RecyclerView) view.findViewById(R.id.mList_videos);
initList(mListData);
new RequestYouTubeAPI().execute();
return view;
}


private void initList(ArrayList<YouTubeDataModel> mListData) {
mList_videos.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new VideoPostAdapter(getActivity(), mListData);
mList_videos.setAdapter(adapter);
}

// create asynctask to get data from youtube
private class RequestYouTubeAPI extends AsyncTask<Void, String, String>{

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(Void... params) {
URL url = null;
String json = null;
StringBuffer sb = new StringBuffer();

try {
url = new URL(CHANNEL_GET_URL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
//HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
HttpURLConnection urlConnection = NetCipher.getHttpsURLConnection(url);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
json = sb.toString();
return json;

} catch (IOException e) {
e.printStackTrace();
return null;
}
}


@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if(response != null){
try {
JSONObject jsonObject = new JSONObject(response);
Log.e("response", jsonObject.toString());
mListData = parseVideoListFromResponse(jsonObject);
initList(mListData);
//adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}

public ArrayList<YouTubeDataModel> parseVideoListFromResponse(JSONObject jsonObject) {
ArrayList<YouTubeDataModel> mList = new ArrayList<>();

if (jsonObject.has("items")) {
try {
JSONArray jsonArray = jsonObject.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
if (json.has("id")) {
JSONObject jsonID = json.getJSONObject("id");
String video_id = "";
if (jsonID.has("videoId")) {
video_id = jsonID.getString("videoId");
}
if (jsonID.has("kind")) {
if (jsonID.getString("kind").equals("youtube#video")) {
YouTubeDataModel youtubeObject = new YouTubeDataModel();
JSONObject jsonSnippet = json.getJSONObject("snippet");
String title = jsonSnippet.getString("title");
String description = jsonSnippet.getString("description");
String publishedAt = jsonSnippet.getString("publishedAt");
String thumbnail = jsonSnippet.getJSONObject("thumbnails").getJSONObject("high").getString("url");

youtubeObject.setTitle(title);
youtubeObject.setDescription(description);
youtubeObject.setPublishedAt(publishedAt);
youtubeObject.setThumbnail(thumbnail);
youtubeObject.setVideo_id(video_id);
mList.add(youtubeObject);

}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}

return mList;
}
}

VideoPostAdapter.java

public class VideoPostAdapter extends RecyclerView.Adapter<VideoPostAdapter.YouTubePostHolder> {

private ArrayList<YouTubeDataModel> dataSet;
private Context mContext = null;

public VideoPostAdapter(Context mContext, ArrayList<YouTubeDataModel> dataSet) {
this.dataSet = dataSet;
this.mContext = mContext;
}

@NonNull
@Override
public YouTubePostHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.youtube_post_layout,parent,false);
YouTubePostHolder postHolder = new YouTubePostHolder(view);
return postHolder;
}

@Override
public void onBindViewHolder(@NonNull YouTubePostHolder holder, int position) {

// set the views here
TextView textViewTitle = holder.textViewTitle;
TextView textViewDes = holder.textViewDes;
TextView textViewDate = holder.textViewDate;
ImageView ImageThumb = holder.ImageThumb;

YouTubeDataModel object = dataSet.get(position);

textViewTitle.setText(object.getTitle());
textViewDes.setText(object.getDescription());
textViewDate.setText(object.getPublishedAt());
// image will be downloaded from url
}

@Override
public int getItemCount() {
return dataSet.size();
}

public static class YouTubePostHolder extends RecyclerView.ViewHolder{
TextView textViewTitle;
TextView textViewDes;
TextView textViewDate;
ImageView ImageThumb;

public YouTubePostHolder(@NonNull View itemView) {
super(itemView);
this.textViewTitle = (TextView) itemView.findViewById(R.id.textViewTitle);
this.textViewDes = (TextView) itemView.findViewById(R.id.textViewDes);
this.textViewDate = (TextView) itemView.findViewById(R.id.textViewDate);
this.ImageThumb = (ImageView) itemView.findViewById(R.id.ImageThumb);
}
}
}

YouTubeDataModel.java

public class YouTubeDataModel {
private String title = "";
private String description = "";
private String publishedAt = "";
private String thumbnail = "";

public String getVideo_id() {
return video_id;
}

public void setVideo_id(String video_id) {
this.video_id = video_id;
}

private String video_id = "";

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getPublishedAt() {
return publishedAt;
}

public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}

public String getThumbnail() {
return thumbnail;
}

public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
}

youtube_post_layout.xml

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

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">

<ImageView
android:id="@+id/ImageThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"/>

<TextView
android:id="@+id/textViewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="published at"
android:singleLine="true"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:textColor="@android:color/white"
android:textSize="12dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video Title"
android:singleLine="true"
android:textColor="@android:color/white"
android:textSize="22dp"/>

<TextView
android:id="@+id/textViewDes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video description"
android:singleLine="true"
android:textColor="@android:color/white"
android:textSize="12dp"/>

</LinearLayout>

</RelativeLayout>

</LinearLayout>

fragment_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mList_videos"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

不幸的是,我不知道为什么 fragment 仍然是空的。 Android Studio 日志中没有任何错误,我真的希望你能帮助我:/

最佳答案

在您的 RequestYouTubeAPI ASyncTask 中,您有以下错误代码:

        } catch (IOException e) {
e.printStackTrace();
return null;
}

然后在 onPostExecute 中你有以下内容:

    @Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if(response != null){
try {
JSONObject jsonObject = new JSONObject(response);
Log.e("response", jsonObject.toString());
mListData = parseVideoListFromResponse(jsonObject);
initList(mListData);
//adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}

因此,如果您收到错误,您将返回 null,如果 onPostExecute 收到一个 null 响应,它什么也不做。

所以这个地方你可能有错误,因此是一个空白 fragment 。


在你解决这个问题之前,你可以证明这是这样发生的:

    @Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if(response == null){
Log.e("TUT", "We did not get a response, not updating the UI.");
} else {
try {
JSONObject jsonObject = new JSONObject(response);
Log.e("response", jsonObject.toString());
mListData = parseVideoListFromResponse(jsonObject);
initList(mListData);
//adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}

您可以通过两种方式解决此问题:

doInBackground 中将 catch 更改为:

        } catch (IOException e) {
Log.e("TUT", "error", e);
// Change this JSON to match what the parse expects, so you can show an error on the UI
return "{\"yourJson\":\"error!\"}";
}

onPostExecute:

        if(response == null){
List errorList = new ArrayList();
// Change this data model to show an error case to the UI
errorList.add(new YouTubeDataModel("Error");
mListData = errorList;
initList(mListData);
} else {
try {
JSONObject jsonObject = new JSONObject(response);
Log.e("response", jsonObject.toString());
mListData = parseVideoListFromResponse(jsonObject);
initList(mListData);
//adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}

希望对您有所帮助,代码中可能还有其他错误,但如果 API、Json、授权、互联网等出现问题,则可能会发生这种情况。

关于java - 如何修复空 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58626254/

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