我在android中使用回收站视图来显示数据列表。我也在视图持有者类中使用链接预览。但我的回收者视图未显示任何数据,该页面为空。
及其显示错误-
android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
我尝试了很多事情,但是没有从那里得到任何输出。我已经检查了我的xml文件代码。哪个是对的。我也检查了代码,但没有发现问题。
自定义行项目-
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
android:paddingBottom="15dp">
<LinearLayout
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<com.pkmmte.view.CircularImageView
android:id="@+id/user_profile_image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:src="@drawable/user_icon"
android:adjustViewBounds="true"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="4"
android:orientation="vertical"
android:layout_marginStart="15dp">
<TextView
android:id="@+id/post_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:textStyle="bold"
android:textColor="#000"
android:padding="3dp"
android:textSize="15sp"/>
<TextView
android:id="@+id/post_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:padding="3dp"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/post_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Text"
android:padding="2dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="20dp"
android:textColor="#000"
android:textSize="13sp"
android:visibility="gone"/>
<FrameLayout
android:id="@+id/media_frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
<com.google.android.youtube.player.YouTubeThumbnailView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:src="@drawable/jellyfish"
android:scaleType="fitXY"
android:visibility="gone"/>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/media_youtube_player_view"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</com.google.android.youtube.player.YouTubePlayerView>
<ImageView
android:id="@+id/media_video_image_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="fitXY"
android:visibility="gone"
/>
<VideoView
android:id="@+id/media_video_view"
android:layout_height="200dp"
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:visibility="gone"
/>
<ImageView
android:id="@+id/media_image_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="fitXY"
android:visibility="gone"
/>
<ImageView
android:id="@+id/video_play_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal|center_vertical"
android:src="@drawable/video_play_button_black"
android:visibility="gone"
android:clickable="true"
android:focusable="true" />
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/link_textView"
android:padding="10dp"
android:textSize="15sp"
android:background="#cdc9c9"
android:clickable="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textColor="#000"
android:visibility="gone"/>
<android.support.v7.widget.CardView
android:id="@+id/twitter_link_card_view"
android:layout_height="150dp"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone"
>
<com.freesoulapps.preview.android.Preview
android:id="@+id/link_preview"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:gravity="center_vertical"
android:clickable="true"
android:focusable="true">
</com.freesoulapps.preview.android.Preview>
</android.support.v7.widget.CardView>
<View
android:layout_width="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_height="1dp"
android:background="#CFD8DC" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<ImageView
android:id="@+id/post_like_image_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:src="@drawable/like_heart_icon"
android:padding="2dp"
android:focusable="true" />
<ImageView
android:id = "@+id/post_liked_image_view"
android:layout_width="0dp"
android:clickable="true"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/like_blue_image_icon"
android:padding="2dp"
android:visibility="gone"
android:focusable="true" />
<ImageView
android:id="@+id/post_comment_image_view"
android:layout_width="0dp"
android:clickable="true"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/comment_icon"
android:padding="2dp"
android:focusable="true" />
<ImageView
android:id="@+id/post_sharing_imageView"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/share_icon"
android:padding="2dp"
android:focusable="true" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/comment_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_text_style1"
android:padding="5dp"
android:layout_margin="10dp"
android:visibility="gone"
android:hint="Type your Comment"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
我的适配器类的代码-
public class AdapterRecyclerView extends RecyclerView.Adapter<MyViewHolder> implements Preview.PreviewListener {
private List<AllDataModel> allDataArrayList;
private Context context;
private int resource;
Preview linkPreview;
public AdapterRecyclerView(TwitterDataTest allDataActivity, int custom_data_main_layout , List<AllDataModel> allDataArrayList) {
this.context = allDataActivity;
this.resource = custom_data_main_layout;
this.allDataArrayList = allDataArrayList;
}
public AdapterRecyclerView(FacebookDataActivity facebookDataActivity, int custom_data_main_layout , List<AllDataModel> allDataArrayList) {
this.context = facebookDataActivity;
this.resource = custom_data_main_layout;
this.allDataArrayList = allDataArrayList;
}
public AdapterRecyclerView(YoutubeDataActivity youtubeDataActivity, int custom_data_main_layout , List<AllDataModel> allDataArrayList) {
this.context = youtubeDataActivity;
this.resource = custom_data_main_layout;
this.allDataArrayList = allDataArrayList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(resource, parent, false);
linkPreview = itemView.findViewById(R.id.link_preview);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int position) {
final AllDataModel allDataModel = allDataArrayList.get(position);
//get values from dataModel
final String username = allDataModel.getUsername();
myViewHolder.UserName.setText(allDataModel.getUsername());
myViewHolder.PostTime.setText(allDataModel.getPost_creation_time());
String post_time = allDataModel.getPost_creation_time();
String post_text = allDataModel.getPost_text();
String status_type = allDataModel.getStatus_type();
final String link_Title = allDataModel.getLink_title();
if(status_type != null) {
Log.e("#444", status_type);
}
// setting source images
final String source = allDataModel.getSource();
String profileimageURL = allDataModel.getProfile_image_url();
String PostImageURL = allDataModel.getPost_image_url();
String postThuumbnailImageURL = allDataModel.getPost_video_thumbnail();
final String videoId = allDataModel.getVideo_id();
final String VideoUrl = allDataModel.getPost_video();
final String link_url = allDataModel.getLink_url();
if(profileimageURL != null){
Picasso.with(getContext()).load(profileimageURL).into(myViewHolder.profileCircularImage);
}
if(post_text != null){
myViewHolder.PostText.setVisibility(View.VISIBLE);
myViewHolder.PostText.setText(allDataModel.getPost_text());
}
if(PostImageURL != null){
myViewHolder.mediaFrameLayout.setVisibility(View.VISIBLE);
myViewHolder.postMediaImage.setVisibility(View.VISIBLE);
Picasso.with(getContext()).load(PostImageURL).into( myViewHolder.postMediaImage);
}
if(postThuumbnailImageURL != null) {
myViewHolder.mediaFrameLayout.setVisibility(View.VISIBLE);
myViewHolder.postVideoThumbnailImage.setVisibility(View.VISIBLE);
Picasso.with(getContext()).load(PostImageURL).into( myViewHolder.postMediaImage);
Picasso.with(getContext()).load(postThuumbnailImageURL).into( myViewHolder.postVideoThumbnailImage);
myViewHolder.play_button_image.setVisibility(View.VISIBLE);
if(videoId != null) {
myViewHolder.mediaFrameLayout.setVisibility(View.VISIBLE);
myViewHolder.play_button_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewHolder.postMediaImage.setVisibility(View.GONE);
myViewHolder.play_button_image.setVisibility(View.GONE);
myViewHolder.postVideoThumbnailImage.setVisibility(View.GONE);
Intent youtubePlayerIntent = new Intent((Activity)context, YoutubePlayerActivity.class);
youtubePlayerIntent.putExtra("video_id", videoId);
context.startActivity(youtubePlayerIntent);
}
}
);
}
if (VideoUrl != null) {
myViewHolder. play_button_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewHolder.postVideoThumbnailImage.setVisibility(View.GONE);
myViewHolder.play_button_image.setVisibility(View.GONE);
myViewHolder.postVideoView.setVisibility(View.VISIBLE);
try {
MediaController mediaController = new MediaController(getContext());
mediaController.setAnchorView( myViewHolder.postVideoView);
Uri video = Uri.parse(VideoUrl);
myViewHolder.postVideoView.setMediaController(mediaController);
myViewHolder.postVideoView.setVideoURI(video);
myViewHolder.postVideoView.requestFocus();
myViewHolder.postVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
myViewHolder.postVideoView.start();
}
});
} catch (Exception e) {
Log.e("Video Play Error :" , e.toString());
}
}
});
}
if(status_type != null){
if(status_type.equals("album") || status_type.equals("share") && source.equals("facebook")){
Log.e("STA2",status_type);
myViewHolder.play_button_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewHolder. play_button_image.setVisibility(View.GONE);
myViewHolder.postVideoThumbnailImage.setVisibility(View.GONE);
Intent webViewIntent = new Intent(context, WebViewActivity.class);
webViewIntent.putExtra("link_url", link_url);
context.startActivity(webViewIntent);
}
});
}
}
}
if(status_type != null){
if(status_type.equals("share") && source.equals("twitter") && link_Title!=null ){
myViewHolder.linkCardView.setVisibility(View.VISIBLE);
myViewHolder.linkPreview.setData(link_Title);
myViewHolder.linkPreview.setListener((Preview.PreviewListener) this);
myViewHolder.linkPreview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent webViewIntent = new Intent(context, WebViewActivity.class);
webViewIntent.putExtra("link_url", link_Title);
context.startActivity(webViewIntent);
}
});
}
}
myViewHolder.shareutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.share_dialogue);
dialog.show();
Button fbShareBtn = (Button)dialog.findViewById(R.id.share_facebook_button);
Button twitterShareBtn = (Button)dialog.findViewById(R.id.share_twitter_button);
Button cancel = (Button)dialog.findViewById(R.id.cancel_share);
final String link_url = allDataModel.getLink_url();
fbShareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent facebookIntent = new Intent(context, FacebookLoginActivity.class);
facebookIntent.putExtra("link_url", link_url);
facebookIntent.putExtra("source", source);
context.startActivity(facebookIntent);
}
});
twitterShareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent twitterLoginIntent = new Intent(context, MainActivity.class);
twitterLoginIntent.putExtra("link_url", link_url);
context.startActivity(twitterLoginIntent);
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
myViewHolder.postComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewHolder.commentET.setVisibility(View.VISIBLE);
}
});
myViewHolder.postLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewHolder.postLiked.setVisibility(View.VISIBLE);
myViewHolder.postLike.setVisibility(View.GONE);
}
});
myViewHolder.postLiked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewHolder. postLike.setVisibility(View.VISIBLE);
myViewHolder.postLiked.setVisibility(View.GONE);
}
});
}
@Override
public int getItemCount() {
return allDataArrayList.size();
}
@Override
public void onDataReady(Preview preview) {
linkPreview .setMessage(preview.getLink());
}
}
主要活动的代码-
public class TwitterDataTest extends AppCompatActivity {
RecyclerView recyclerView;
AdapterRecyclerView adapterRecyclerView;
public List<AllDataModel> allDataArrayList;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_test);
allDataArrayList = new ArrayList<>();
recyclerView = findViewById(R.id.twitter_recycler_view);
recyclerView.setHasFixedSize(true);
pd = new ProgressDialog(this);
pd.setTitle("Loading......");
pd.show();
if(isOnline()){
Api apiService =
ApiClient.getClient().create(Api.class);
Call<ResponseBody> call = apiService.getTwitterData();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.isSuccessful()){
try {
assert response.body() != null;
String s = response.body().string();
LoadIntoListView(s);
recyclerView.setAdapter(adapterRecyclerView);
} catch (IOException e) {
e.printStackTrace();
}
}
else{
Toast.makeText(TwitterDataTest.this, "No response", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
else{
Toast.makeText(TwitterDataTest.this, "Please check your internet connection", Toast.LENGTH_LONG).show();
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapterRecyclerView = new AdapterRecyclerView(TwitterDataTest.this, R.layout.custom_data_main_layout, allDataArrayList);
adapterRecyclerView.notifyDataSetChanged();
recyclerView.setAdapter(adapterRecyclerView);
}
public void LoadIntoListView(String s) {
pd.dismiss();
try {
JSONArray jsonArray1 = new JSONArray(s);
int ja1length = jsonArray1.length();
for (int i = 0; i < ja1length; i++) {
String source = "twitter";
String videoThumbnailImageUrl = null;
String TweetVideoUrl = null;
String TweetImageUrl = null;
String expanded_url = null;
String url = null;
String link_url = null;
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
// String TweetTime = jsonObject1.getString("created_at");
String TweetText = jsonObject1.getString("full_text");
String id = jsonObject1.getString("id");
link_url = "https://twitter.com/AmritDki/status/" + id;
String createdTime = jsonObject1.getString("created_at");
final String TWITTER = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
String TwitterCreationDateIST = String.valueOf(sf.parse(createdTime));
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
String dateIST_customFormat = formatter.format(sf.parse(createdTime));
if (TweetText.contains("http") == true) {
int index = TweetText.indexOf("https");
TweetText = TweetText.substring(0, index );
}
// user json object
JSONObject userJO = jsonObject1.getJSONObject("user");
String UserName = userJO.getString("name");
String ScreenName = userJO.getString("screen_name");
String ProfileImageUrl = userJO.getString("profile_image_url_https");
// getting entities object
if (jsonObject1.has("extended_entities")) {
JSONObject ExtendedEntitiesJO = jsonObject1.getJSONObject("extended_entities");
///////
JSONArray MediaJA = ExtendedEntitiesJO.getJSONArray("media");
for (int j = 0; j < MediaJA.length(); j++) {
JSONObject insideMediaJO = MediaJA.getJSONObject(j);
if (insideMediaJO.has("video_info")) {
videoThumbnailImageUrl = insideMediaJO.getString("media_url_https");
JSONObject VideoInfoJO = insideMediaJO.getJSONObject("video_info");
JSONArray variantsArray = VideoInfoJO.getJSONArray("variants");
JSONObject videoUrlJO = variantsArray.getJSONObject(0);
TweetVideoUrl = videoUrlJO.getString("url");
//allDataArrayList.add(new TwitterDataModel(ProfileImageUrl, UserName,TweetVideoUrl, TweetImageUrl, TweetText));
allDataArrayList.add(new AllDataModel(source, UserName, ProfileImageUrl,
TweetText, TweetImageUrl, videoThumbnailImageUrl, TweetVideoUrl, dateIST_customFormat, null,link_url,null, null));
} else {
TweetImageUrl = insideMediaJO.getString("media_url_https");
allDataArrayList.add(new AllDataModel(source, UserName, ProfileImageUrl,
TweetText, TweetImageUrl, videoThumbnailImageUrl, TweetVideoUrl, dateIST_customFormat, null,link_url,null, null));
}
}
}
else{
JSONObject entitiesJO = jsonObject1.getJSONObject("entities");
if(entitiesJO.has("urls")){
JSONArray urlsArray = entitiesJO.getJSONArray("urls");
if(urlsArray.length() > 0){
JSONObject insideUrlsJO = urlsArray.getJSONObject(0);
if(insideUrlsJO.has("expanded_url")){
expanded_url = insideUrlsJO.getString("expanded_url");
allDataArrayList.add(new AllDataModel(source, UserName, ProfileImageUrl,
TweetText, TweetImageUrl, videoThumbnailImageUrl, TweetVideoUrl, dateIST_customFormat,
null,link_url, "share",expanded_url));
}
}else{
allDataArrayList.add(new AllDataModel(source, UserName, ProfileImageUrl,
TweetText, TweetImageUrl, videoThumbnailImageUrl, TweetVideoUrl, dateIST_customFormat,
null,link_url, null,null
));
}
}
}
Log.e("#**", String.valueOf(allDataArrayList.size()));
// adapterRecyclerView = new AdapterRecyclerView(TwitterDataTest.this, R.layout.custom_data_main_layout, allDataArrayList);
// adapterRecyclerView.notifyDataSetChanged();
//recyclerView.setAdapter(adapterRecyclerView);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
Log.e("parsingError", String.valueOf(e));
}
}
答案 0 :(得分:0)
投资3-4小时后,我的问题得到了解决。我在膨胀布局时使用的是getContext()而不是上下文(通过构造函数来实现)。小愚蠢的错误。