[英]Cannot make a static reference to the non-static method getVideoURL() from the type Cmd
I'm getting an error stating: 我收到一条错误说明:
"Cannot make a static reference to the non-static method getVideoURL() from the type Cmd CustomListViewAdapter
" on the line: String txt=Cmd.getVideoURL();
"Cannot make a static reference to the non-static method getVideoURL() from the type Cmd CustomListViewAdapter
对行上的"Cannot make a static reference to the non-static method getVideoURL() from the type Cmd CustomListViewAdapter
: String txt=Cmd.getVideoURL();
The issue is being cause by this line: 问题是由这条线引起的:
public String getVideoURL() {
return videourl;
}
in Cmd.java
在
Cmd.java
I've tried setting the strings to Static - but it prevents my data from being updated. 我已经尝试将字符串设置为静态 - 但它会阻止我的数据更新。
How might this situation be avoided? 怎么可能避免这种情况?
public class CustomListViewAdapter extends ArrayAdapter<Cmd> {
Activity context;
List<Cmd> videos;
public CustomListViewAdapter(Activity context, List<Cmd> videos) {
super(context, R.layout.list_item2, videos);
this.context = context;
this.videos = videos;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtSuccess;
TextView txtCmd;
TextView txtPrice;
}
public void run() {
Intent intent = new Intent(context, ViewVideo.class);
String txt=Cmd.getVideoURL();
intent.putExtra("videofilename", txt);
context.startActivity(intent);
}
public Cmd getItem(int position) {
return videos.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item2, null);
holder = new ViewHolder();
holder.txtSuccess = (TextView) convertView
.findViewById(R.id.success);
holder.txtCmd = (TextView) convertView.findViewById(R.id.cmd);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Cmd cmd = (Cmd) getItem(position);
holder.txtSuccess.setText(cmd.getVideoName());
holder.txtSuccess.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run();
}
});
holder.txtCmd.setText(cmd.getCmd());
holder.txtCmd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run();
}
});
holder.txtPrice.setText(cmd.getVideoURL() + "");
holder.txtPrice.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run();
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run();
}
});
return convertView;
}
}
public class Cmd implements ListAdapter {
private String success;
private String cmd;
List<Cmd> videos;
private String video;
private String numberofvideos;
private String videoname;
private String videourl;
private LayoutInflater mInflater;
Button fav_up_btn1;
Button fav_dwn_btn1;
Context my_context;
Bitmap imageBitmap;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If convertView wasn't null it means we have already set it to our
// list_item_user_video so no need to do it again
if (convertView == null) {
// This is the layout we are using for each row in our list
// anything you declare in this layout can then be referenced below
convertView = mInflater.inflate(R.layout.list_item_user_video,
parent, false);
}
// We are using a custom imageview so that we can load images using urls
ImageView thumb = (ImageView) convertView
.findViewById(R.id.userVideoThumbImageView);
//thumb.setScaleType(ScaleType.FIT_XY);
TextView title = (TextView) convertView
.findViewById(R.id.userVideoTitleTextView);
TextView uploader = (TextView) convertView
.findViewById(R.id.userVideouploaderTextView);
TextView viewCount = (TextView) convertView
.findViewById(R.id.userVideoviewsTextView);
uploader.setText(videos.get(position).getTitle());
viewCount.setText(videos.get(position).getviewCount() + " views");
fav_up_btn1 = (Button) convertView.findViewById(R.id.fav_up_btn1);
fav_up_btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean favIsUp = fav_up_btn1
.getBackground()
.getConstantState()
.equals(my_context.getResources()
.getDrawable(R.drawable.fav_up_btn1)
.getConstantState());
// set the background
fav_up_btn1
.setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
: R.drawable.fav_up_btn1);
}
});
// Get a single video from our list
final Cmd video = videos.get(position);
// Set the image for the list item
// / thumb.setImageDrawable(video.getThumbUrl());
//thumb.setScaleType(ScaleType.FIT_XY);
// Set the title for the list item
title.setText(video.getTitle());
uploader.setText("by " + video.getUploader() + " | ");
return convertView;
}
public String getUploader() {
// TODO Auto-generated method stub
return null;
}
public String getviewCount() {
// TODO Auto-generated method stub
return null;
}
public CharSequence getTitle() {
// TODO Auto-generated method stub
return null;
}
public String getCmd() {
return cmd;
}
public void setCmd(String cmd) {
this.cmd = cmd;
}
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
public String getNumberOfVideos() {
return numberofvideos;
}
public void setNumberOfVideos(String numberofvideos) {
this.numberofvideos = numberofvideos;
}
public List<Cmd> getVideos() {
return videos;
}
public void setVideos(List<Cmd> videos) {
this.videos = videos;
}
public String getVideo() {
return video;
}
public void setVideo(String video) {
this.video = video;
}
public String getVideoName() {
return videoname;
}
public void setVideoName(String videoname) {
this.videoname = videoname;
}
public String getVideoURL() {
return videourl;
}
public void setVideoURL(String videourl) {
this.videourl = videourl;
}
@Override
public int getCount() {
return videos.size();
}
@Override
public Object getItem(int position) {
return videos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
public String getId() {
// TODO Auto-generated method stub
return null;
}
}
The error message says it all: you cannot call a non-static method from a static context. 错误消息说明了一切:您无法从静态上下文调用非静态方法。 You need to create an instance of the class that holds that particular method and call the method on this object.
您需要创建包含该特定方法的类的实例,并在此对象上调用该方法。 That way you don't have to make fields static either.
这样你就不必将字段设为静态。
You're trying to call getVideoUrl()
on Cmd
, but you need to create an instance first: 你试图在
Cmd
上调用getVideoUrl()
,但是你需要先创建一个实例:
Cmd cmd = new Cmd();
String txt = cmd.getVideoURL();
I strongly encourage to read through the official tutorial on this subject ! 我强烈建议您阅读有关此主题的官方教程 !
You're almost there. 你快到了。 Try this:
尝试这个:
public View getView(int position, View convertView, ViewGroup parent) {
. . .
final Cmd cmd = (Cmd) getItem(position);
holder.txtSuccess.setText(cmd.getVideoName());
holder.txtSuccess.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run(cmd);
}
});
. . .
}
public void run(Cmd cmd) {
Intent intent = new Intent(context, ViewVideo.class);
String txt=cmd.getVideoURL();
intent.putExtra("videofilename", txt);
context.startActivity(intent);
}
It looks like you're referencing the Cmd
object using the static-style syntax because it's not reachable the way you wrote the code. 看起来您正在使用静态样式语法引用
Cmd
对象,因为它无法像您编写代码那样访问。 The code above allows you to pass the Cmd object to the run()
method, where it may be accessed. 上面的代码允许您将Cmd对象传递给
run()
方法,在该方法中可以访问它。
HTH HTH
您应该问自己,要在run
方法中显示哪个视频URL - 数据结构中没有任何static
视频URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.