Java 将onclick设置为listview/json时出错
当我尝试运行代码时,我遇到了一个json异常。 问题发生在onclick侦听器上。我试图让应用程序在点击时转到rss提要的文章 这里是主要活动Java 将onclick设置为listview/json时出错,java,android,json,rss,Java,Android,Json,Rss,当我尝试运行代码时,我遇到了一个json异常。 问题发生在onclick侦听器上。我试图让应用程序在点击时转到rss提要的文章 这里是主要活动 public class Blocku extends ListActivity { private RssListAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstance
public class Blocku extends ListActivity {
private RssListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<JSONObject> jobs = new ArrayList<JSONObject>();
try {
jobs = BlockuReader.getLatestRssFeed();
} catch (Exception e) {
Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
}
adapter = new RssListAdapter(this,jobs);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String link = null;
try {
String url = Article.getUrl().toString();
link = adapter.getItem(position).getString(url).toString();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(link));
startActivity(i);
} catch (JSONException e) {
Context context = getApplicationContext();
CharSequence text = "error";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
e.printStackTrace();
}
}
}
当解析出现问题时,通常会抛出JSONException。检查如何解析JSON。这表示您正在尝试为名为“then it lists the site i want”的键获取一个值。当我运行调试器时,logcat不再显示历史记录,因此它只显示当前行。我怎样才能看到整个历史。对不起,我还在学习。我运行的是EclipseBtwforMe,当通过eclipse查看logcat时,在日志达到某个大小限制后,它会执行您所描述的操作:只显示最后一行。如果我按下按钮清除当前日志,事情就会恢复正常。类似地,重新启动eclipse或切换到其他模拟器(或真实设备)也可以解决问题。好吧,它只是说07-11 22:02:12.377:WARN/System.err(4914):org.json.JSONException:没有“then it list the site I want”的值,错误实际从哪里抛出?您有调用堆栈吗?07-11 22:30:31.738:WARN/System.err(11342):org.json.JSONException:没有“我想要的url”的值07-11 22:30:31.748:WARN/System.err(11342):at org.json.JSONObject.get(JSONObject.java:354)07-11 22:30:31.748:WARN/System.err(11342):at org.json.JSONObject.getString(JSONObject.java:510)07-11 22:30:31.748:WARN/System.err(11342):在com.my7h1c.utes.Blocku.onListItemClick(Blocku.java:51)07-11 22:30:31.748:WARN/System.err(11342):在android.app.ListActivity$2.onItemClick(ListActivity.java:342)上,我发现了如何通过将onclick中的内容替换为“String link=Article.getUrl().toString()”来消除错误;Intent i=new Intent(Intent.ACTION_VIEW);i.setData(Uri.parse(link));startActivity(i);“但现在我的问题是如何获得位置?它只是获取最后一篇文章urlink=adapter.getItem(position).getString(url.toString();?您需要从列表适配器获取位置处的项。重写该方法或为列表创建一个getter。项目位置需要包含在代码中的某个位置。我现在在您提供的字符串上发现一个错误,url无法解析为变量。有什么想法吗?你的代码有点乱。我真的不知道你在干什么。处理HTTP调用和json响应的正确方法是将返回的json对象实际解析为数组或其他内容,然后在将json解析为数组后使用该数组除去所有json对象。此外,您还应该尝试阅读有关使用静态变量及其作用的内容。您似乎有一个错误。您正在静态访问变量,若您有一个类对象数组,这有点错误;将json响应解析为一个数组,使用该数组生成一个适配器。将适配器设置为列表。单击获取分配给适配器的数组中的第n项并使用其内容。
public class RssListAdapter extends ArrayAdapter<JSONObject> {
public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) {
super(activity, 0, imageAndTexts);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
// Inflate the views from XML
View rowView = inflater.inflate(R.layout.image_text_layout, null);
JSONObject jsonImageText = getItem(position);
//////////////////////////////////////////////////////////////////////////////////////////////////////
//The next section we update at runtime the text - as provided by the JSON from our REST call
////////////////////////////////////////////////////////////////////////////////////////////////////
TextView textView = (TextView) rowView.findViewById(R.id.job_text);
try {
Spanned text = (Spanned)jsonImageText.get("text");
textView.setText(text);
} catch (JSONException e) {
textView.setText("JSON Exception");
}
return rowView;
}
}
public class Article {
private long articleId;
private long feedId;
private String title;
private String description;
private String pubDate;
private static URL url;
private String encodedContent;
private static String link;
/**
* @return the articleId
*/
public long getArticleId() {
return articleId;
}
/**
* @param articleId the articleId to set
*/
public void setArticleId(long articleId) {
this.articleId = articleId;
}
/**
* @return the feedId
*/
public long getFeedId() {
return feedId;
}
/**
* @param feedId the feedId to set
*/
public void setFeedId(long feedId) {
this.feedId = feedId;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the url
*/
public static URL getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(URL url) {
Article.url = url;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param pubDate the pubDate to set
*/
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
/**
* @return the pubDate
*/
public String getPubDate() {
return pubDate;
}
/**
* @param encodedContent the encodedContent to set
*/
public void setEncodedContent(String encodedContent) {
this.encodedContent = encodedContent;
}
/**
* @return the encodedContent
*/
public String getEncodedContent() {
return encodedContent;
}
}